Submodule template
This file originates from the file beets/util/functemplate.py of the beets project.
This module implements a string formatter based on the standard PEP 292 string.Template class extended with function calls. Variables, as with string.Template, are indicated with $ and functions are delimited with %.
This module assumes that everything is Unicode: the template and the
substitution values. Bytestrings are not supported. Also, the templates
always behave like the safe_substitute method in the standard
library: unknown symbols are left intact.
This is sort of like a tiny, horrible degeneration of a real templating engine like Jinja2 or Mustache.
- class tmep.template.Environment(values: Dict[str, Any], functions: Dict[str, Callable[[...], str | int]])[source]
Bases:
objectContains the values and functions to be substituted into a template.
- tmep.template.ex_rvalue(name: str) Name[source]
A variable store expression.
- Parameters:
name – For example
'str','map','__func_alpha'
- tmep.template.ex_literal(val: int | float | bool | str | Call | List | Name | None) Constant[source]
An int, float, long, bool, string, or None literal with the given value.
- Parameters:
val – For example
'abc123'
- tmep.template.ex_call(func: str | Attribute | Name, args: list[Call | List | Name | Any]) Call[source]
A function-call expression with only positional parameters. The function may be an expression or the name of a function. Each argument may be an expression or a value to be used as a literal.
- tmep.template.compile_func(arg_names: list[str], statements: list[Return], name: str = '_the_func', debug: bool = False) Callable[[...], Any][source]
Compile a list of statements as the body of a function and return the resulting Python function. If debug, then print out the bytecode of the compiled function.
- class tmep.template.Symbol(ident: str, original: str)[source]
Bases:
objectA variable-substitution symbol in a template.
- evaluate(env: Environment)[source]
Evaluate the symbol in the environment, returning a Unicode string.
- class tmep.template.Call(ident: str, args: list[Expression], original: str)[source]
Bases:
objectA function call in a template.
- args: list[Expression]
- evaluate(env: Environment) str[source]
Evaluate the function call in the environment, returning a Unicode string.
- class tmep.template.Expression(parts: list[str | Symbol | Call])[source]
Bases:
objectTop-level template construct: contains a list of text blobs, Symbols, and Calls.
- evaluate(env: Environment) str[source]
Evaluate the entire expression in the environment, returning a Unicode string.
- class tmep.template.Parser(string: str, in_argument: bool = False)[source]
Bases:
objectParses a template expression string. Instantiate the class with the template source and call
parse_expression. Theposfield will indicate the character after the expression finished andpartswill contain a list of Unicode strings, Symbols, and Calls reflecting the concatenated portions of the expression.This is a terrible, ad-hoc parser implementation based on a left-to-right scan with no lexing step to speak of; it’s probably both inefficient and incorrect. Maybe this should eventually be replaced with a real, accepted parsing technique (PEG, parser generator, etc.).
- special_chars
- special_char_re
- escapable_chars
- terminator_chars
- parse_expression() None[source]
Parse a template expression starting at
pos. Resulting components (Unicode strings, Symbols, and Calls) are added to thepartsfield, a list. Theposfield is updated to be the next character after the expression.
- parse_symbol() None[source]
Parse a variable reference (like
$fooor${foo}) starting atpos. Possibly appends a Symbol object (or, failing that, text) to thepartsfield and updatespos. The character atposmust, as a precondition, be$.
- parse_call()[source]
Parse a function call (like
%foo{bar,baz}) starting atpos. Possibly appends a Call object topartsand updatepos. The character atposmust be%.
- parse_argument_list() list[Expression][source]
Parse a list of arguments starting at
pos, returning a list of Expression objects. Does not modifyparts. Should leavepospointing to a } character or the end of the string.
- class tmep.template.Template(template: str)[source]
Bases:
objectA string template, including text, Symbols, and Calls.
- expr: Expression
- interpret(values: Dict[str, Any] = {}, functions: Dict[str, Callable[[...], str | int]] = {}) str[source]
Like substitute, but forces the interpreter (rather than the compiled version) to be used. The interpreter includes exception-handling code for missing variables and buggy template functions but is much slower.