Interpreter
- class ya_tagscript.interpreter.Context(node: NodeABC, response: Response, interpreter: InterpreterABC, original_message: str)[source]
An internal class used to pass the current processing state around to adapters, blocks, subprocessing, etc.
- interpreter: InterpreterABC
The interpreter instance being used to process this Context
- class ya_tagscript.interpreter.Response(*, variables: Mapping[str, AdapterABC] | None, extra_kwargs: Mapping[str, Any] | None)[source]
A class that bundles the textual output (
Response.body), the defined actions (Response.actions), and variables as well as extra arguments that have been defined during interpretation.- property variables: Mapping[str, AdapterABC]
A mapping of all variables defined during the script processing
For in-progress processing, this represents all variables defined up to the current point in time.
Note
To define/overwrite variables, use
set_variable(). This mapping is read-only.
- set_variable(key: str, adapter: AdapterABC) → None[source]
Stores a variable in the Response’s variables mapping.
Note
Due to a type variance issue, this helper method is required. Access stored variables through the
variablesproperty.- Parameters:
key (str) – The variable’s name
adapter (AdapterABC) – The adapter holding the value that should be stored under
key
- class ya_tagscript.interpreter.TagScriptInterpreter(blocks: Sequence[BlockABC])[source]
Bases:
InterpreterABC- __init__(blocks: Sequence[BlockABC]) → None[source]
Constructs an interpreter with the provided block configurations
- Parameters:
blocks (Sequence[BlockABC]) – The blocks this interpreter should use for interpretation
- work_limit: int | None
The approximate maximum number of characters this interpreter will process before aborting the processing (see also: “work limit” in the Glossary)
- total_work: int
The total number of characters this interpreter has processed so far (This is reset to 0 at the beginning and end of each call to
process())
- process(input_string: str, seed_variables: Mapping[str, AdapterABC] | None = None, extra_kwargs: Mapping[str, Any] | None = None, work_limit: int | None = None) → Response[source]
Instruct the interpreter to process the
input_stringwith the current block configuration.- Parameters:
input_string (str) – The script to process
seed_variables (Mapping[str, AdapterABC] | None) – A mapping of predefined variables (default:
None)extra_kwargs (Mapping[str, Any] | None) – A mapping of extra arguments (default:
None)work_limit (int | None) –
The approximate character limit at which to abort processing and error out (default:
None)Note: This is only an approximate limit because a defined block may be short enough to still be allowed under the limit but then end up outputting a lot more text, shooting past the limit. The interpreter checks the limit after each node has been processed. This check is performed even during nested/recursive blocks, so one massive block with lots of nested blocks should still be cut off sufficiently early.
- Returns:
The output of the script as processed
- Return type:
- class ya_tagscript.interpreter.Node(*, type: NodeType, text_value: str | None, declaration: str | None, parameter: str | None, payload: str | None, output: str | None = None)[source]
Bases:
NodeABCImplementation of the
NodeABCABC.- type: NodeType
The type of Node (either
NodeType.BLOCKorNodeType.TEXT)
- text_value: str | None
The
NodeType.TEXTtype node’s contained string. AlwaysNoneforNodeType.BLOCKnodes.
- declaration: str | None
The
NodeType.BLOCKnode’s declaration. AlwaysNonefor nodes of typeNodeType.TEXT.
- parameter: str | None
The
NodeType.BLOCKnode’s parameter. AlwaysNonefor nodes of typeNodeType.TEXT.
- payload: str | None
The
NodeType.BLOCKnode’s payload. AlwaysNonefor nodes of typeNodeType.TEXT.
- output: str | None
The
NodeType.BLOCKnode’s output once interpreted.Nonebefore interpretation of the node is complete (and alwaysNonefor nodes of typeNodeType.TEXT).
- as_raw_string() → str[source]
Returns the node’s raw string representation (basically the text that this node was created from).
- Returns:
The node’s raw string representation
- Return type:
- classmethod block(*, declaration: str, parameter: str | None, payload: str | None) → Node[source]
Convenience method to create a node of type
NodeType.BLOCKwith its exact required parameters.
- classmethod text(*, text_value: str) → Node[source]
Convenience method to create a node of type
NodeType.TEXTwith its exact required parameters.- Parameters:
text_value (str) – The text node’s string value
- Returns:
The
NodeABCwith atypeofNodeType.BLOCK- Return type: