diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/infini/exceptions.py | 4 | ||||
| -rw-r--r-- | src/infini/generator.py | 11 | ||||
| -rw-r--r-- | src/infini/output.py | 6 |
3 files changed, 19 insertions, 2 deletions
diff --git a/src/infini/exceptions.py b/src/infini/exceptions.py index cb0a9e41..8bc9ceb4 100644 --- a/src/infini/exceptions.py +++ b/src/infini/exceptions.py @@ -4,3 +4,7 @@ class InfiniException(Exception): class KeyError(InfiniException): """键值错误""" + + +class UnknownEvent(InfiniException): + """文本事件不存在""" diff --git a/src/infini/generator.py b/src/infini/generator.py index 81f5a69c..81a6ab14 100644 --- a/src/infini/generator.py +++ b/src/infini/generator.py @@ -1,5 +1,7 @@ from infini.output import Output from infini.typing import Dict, Callable +from infini.exceptions import UnknownEvent +from jinja2 import Template class Generator: @@ -7,4 +9,11 @@ class Generator: global_variables: Dict[str, str | Callable] def output(self, output: Output) -> str: - return output.name + assert output.type != "workflow", "Workflow 事件无法产出文本" + template = self.match(output) + return template.render(output.variables) + + def match(self, output: Output) -> Template: + if context := self.events.get(output.name): + return Template(context) + raise UnknownEvent(f"事件不存在: {output.name}") diff --git a/src/infini/output.py b/src/infini/output.py index 92913b1e..73aa5213 100644 --- a/src/infini/output.py +++ b/src/infini/output.py @@ -1,4 +1,4 @@ -from infini.typing import Literal +from infini.typing import Literal, Dict, Any class Output: @@ -7,6 +7,8 @@ class Output: status: int block: bool + variables: Dict[str, Any] + def __init__( self, type: Literal["null", "text", "workflow"], @@ -14,11 +16,13 @@ class Output: *, status: int = 0, block: bool = False, + variables: Dict[str, Any] = {}, ) -> None: self.type = type self.name = name self.status = status self.block = block + self.variables = variables @classmethod def empty(cls) -> "Output": |
