Interfaces
These are the interfaces / abstract base classes (abc.ABC) used across the
project. Generally, where Adapters/Blocks/Interpreters/Nodes are interacted with, these
ABCs are used as the interface.
For example, the concrete TagScriptInterpreter
(itself a subclass of InterpreterABC) gets passed a sequence of
BlockABC on instantiation and does not “know about” any concrete blocks like
MathBlock.
- class ya_tagscript.interfaces.BlockABC[source]
Abstract base class for all block types.
All blocks must inherit from this class and implement the
_accepted_namesproperty andprocess()method.- requires_any_parameter: bool = False
Indicates that this block requires a non-
Noneparameter (default:False).Note
Subclasses must override this if they require this restriction.
- requires_nonempty_parameter: bool = False
Indicates that this block requires a non-
NoneAND non-empty parameter (default:False).When set to
True, this impliesrequires_any_parameteris alsoTrue.Note
Subclasses must override this if they require this restriction.
- requires_any_payload: bool = False
Indicates that this block requires a non-
Nonepayload (default:False).Note
Subclasses must override this if they require this restriction.
- requires_nonempty_payload: bool = False
Indicates that this block requires a non-None AND non-empty payload (default:
False).When set to
True, this impliesrequires_any_payloadis alsoTrue.Note
Subclasses must override this if they require this restriction.
- abstract property _accepted_names: set[str] | None
A
setof all valid block names (all lowercase) orNoneif no block names can be defined (e.g. variable getter blocks).
- abstractmethod process(ctx: Context) → str | None[source]
Execute the block’s processing logic on the provided Context
- will_accept(ctx: Context) → bool[source]
Checks whether this block can process the provided Context
The default check works like this:
name_match: Current node’sdeclarationis notNoneand its lowercased version appears in the block’s_accepted_namesproperty.param_match: Parameter requirements:If
requires_any_parameteris set toTrue, check thatparameteris notNone.If
requires_nonempty_parameteris set toTrue, check thatparameteris notNoneand thatparameteris not an empty string.If neither is set, set to
Trueto pass the check.
payload_match: Payload requirements:If
requires_any_payloadis set toTrue, check thatpayloadis notNone.If
requires_nonempty_payloadis set toTrue, check thatpayloadis notNoneand thatpayloadis not an empty string.If neither is set, set to
Trueto pass the check.
Return
name_match and param_match and payload_match.
Note
If a subclass requires a different check, it must override this method and provide its own check.
- class ya_tagscript.interfaces.InterpreterABC(blocks: Sequence[BlockABC])[source]
-
- 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())
- abstractmethod __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
- abstractmethod 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.interfaces.NodeABC[source]
Represents a section of the input, either recognized as pure text or a TagScript block.
- 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 = None
The
NodeType.BLOCKnode’s output once interpreted.Nonebefore interpretation of the node is complete (and alwaysNonefor nodes of typeNodeType.TEXT).
- abstractmethod 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:
- abstractmethod classmethod block(*, declaration: str, parameter: str | None, payload: str | None) → NodeABC[source]
Convenience method to create a node of type
NodeType.BLOCKwith its exact required parameters.
- abstractmethod classmethod text(*, text_value: str) → NodeABC[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:
- enum ya_tagscript.interfaces.NodeType(value)[source]
Represents possible types of
NodeABCNodeType.BLOCK— A (possible) TagScript block nodeNodeType.TEXT— A pure text node
Valid values are as follows:
- BLOCK = <NodeType.BLOCK: 1>
- TEXT = <NodeType.TEXT: 2>