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.

node: NodeABC

The node currently being processed in this Context

response: Response

The in-progress Response

interpreter: InterpreterABC

The interpreter instance being used to process this Context

original_message: str

The raw input message as provided to the interpreter at the start of processing

interpret_segment(string: str) str[source]

Runs the interpreter on the provided string. Useful for blocks to perform nested string interpretation.

Parameters:

string (str) – The string to interpret

Returns:

The fully interpreted result string

Return type:

str

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.

body: str | None

The text output of the processed script

actions: dict[str, Any]

A dictionary of actions that were defined in the script

extra_kwargs: dict[str, Any]

A dictionary of extra arguments or debugging outputs

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 variables property.

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

blocks: Sequence[BlockABC]

The blocks being used by this interpreter instance

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_string with 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:

Response

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: NodeABC

Implementation of the NodeABC ABC.

type: NodeType

The type of Node (either NodeType.BLOCK or NodeType.TEXT)

text_value: str | None

The NodeType.TEXT type node’s contained string. Always None for NodeType.BLOCK nodes.

declaration: str | None

The NodeType.BLOCK node’s declaration. Always None for nodes of type NodeType.TEXT.

parameter: str | None

The NodeType.BLOCK node’s parameter. Always None for nodes of type NodeType.TEXT.

payload: str | None

The NodeType.BLOCK node’s payload. Always None for nodes of type NodeType.TEXT.

output: str | None

The NodeType.BLOCK node’s output once interpreted. None before interpretation of the node is complete (and always None for nodes of type NodeType.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:

str

classmethod block(*, declaration: str, parameter: str | None, payload: str | None) Node[source]

Convenience method to create a node of type NodeType.BLOCK with its exact required parameters.

Parameters:
  • declaration (str) – The block declaration

  • parameter (str | None) – The block parameter

  • payload (str | None) – The block payload

Returns:

The NodeABC with a type of NodeType.BLOCK

Return type:

NodeABC

classmethod text(*, text_value: str) Node[source]

Convenience method to create a node of type NodeType.TEXT with its exact required parameters.

Parameters:

text_value (str) – The text node’s string value

Returns:

The NodeABC with a type of NodeType.BLOCK

Return type:

NodeABC