Modules

wheezy.template

class wheezy.template.Engine(loader: wheezy.template.typing.Loader, extensions: List[Any], template_class: Optional[Callable[[str, Callable[[Mapping[str, Any], Mapping[str, Any], Mapping[str, Any]], str]], wheezy.template.typing.SupportsRender]] = None)[source]

The core component of template engine.

get_template(name: str) → wheezy.template.typing.SupportsRender[source]

Returns compiled template.

remove(name: str) → None[source]

Removes given name from internal cache.

render(name: str, ctx: Mapping[str, Any], local_defs: Mapping[str, Any], super_defs: Mapping[str, Any]) → str[source]

Renders template by name in given context.

class wheezy.template.CodeExtension(token_start: str = '@')[source]

Includes support for embedded python code.

class wheezy.template.CoreExtension(token_start: str = '@', line_join: str = '\')[source]

Includes basic statements, variables processing and markup.

class wheezy.template.DictLoader(templates: Mapping[str, str])[source]

Loads templates from python dictionary.

templates - a dict where key corresponds to template name and value to template content.

list_names() → Tuple[str, ...][source]

List all keys from internal dict.

load(name: str) → Optional[str][source]

Returns template by name.

class wheezy.template.FileLoader(directories: List[str], encoding: str = 'UTF-8')[source]

Loads templates from file system.

directories - search path of directories to scan for template. encoding - decode template content per encoding.

get_fullname(name: str) → Optional[str][source]

Returns a full path by a template name.

list_names() → Tuple[str, ...][source]

Return a list of names relative to directories. Ignores any files and directories that start with dot.

load(name: str) → Optional[str][source]

Loads a template by name from file system.

class wheezy.template.PreprocessLoader(engine: wheezy.template.engine.Engine, ctx: Optional[Mapping[str, Any]] = None)[source]

Performs preprocessing of loaded template.

class wheezy.template.Preprocessor(runtime_engine_factory: Callable[[wheezy.template.typing.Loader], wheezy.template.engine.Engine], engine: wheezy.template.engine.Engine, key_factory: Callable[[Mapping[str, Any]], str])[source]

Preprocess templates with engine and vary runtime templates by key_factory function using runtime_engine_factory.

wheezy.template.builder

wheezy.template.compiler

wheezy.template.console

wheezy.template.engine

class wheezy.template.engine.Engine(loader: wheezy.template.typing.Loader, extensions: List[Any], template_class: Optional[Callable[[str, Callable[[Mapping[str, Any], Mapping[str, Any], Mapping[str, Any]], str]], wheezy.template.typing.SupportsRender]] = None)[source]

The core component of template engine.

get_template(name: str) → wheezy.template.typing.SupportsRender[source]

Returns compiled template.

remove(name: str) → None[source]

Removes given name from internal cache.

render(name: str, ctx: Mapping[str, Any], local_defs: Mapping[str, Any], super_defs: Mapping[str, Any]) → str[source]

Renders template by name in given context.

class wheezy.template.engine.Template(name: str, render_template: Callable[[Mapping[str, Any], Mapping[str, Any], Mapping[str, Any]], str])[source]

Simple template class.

wheezy.template.engine.complement_syntax_error(err: SyntaxError, template_source: str, source: str) → SyntaxError[source]

Complements SyntaxError with template and source snippets, like one below:

File "shared/snippet/widget.html", line 4
    if :

template snippet:
02 <h1>
03     @msg!h
04     @if :
05         sd
06     @end

generated snippet:
02     _b = []; w = _b.append; w('<h1>\n    ')
03     w(h(msg)); w('\n')
04     if :
05         w('        sd\n')
06

    if :
    ^
SyntaxError: invalid syntax

wheezy.template.lexer

class wheezy.template.lexer.Lexer(lexer_rules: List[Tuple[Pattern[AnyStr], Callable[[Match[AnyStr]], Tuple[int, str, str]]]], preprocessors: Optional[List[Callable[[str], str]]] = None, postprocessors: Optional[List[Callable[[List[Tuple[int, str, str]]], str]]] = None, **ignore)[source]

Tokenizes input source per rules supplied.

tokenize(source: str) → List[Tuple[int, str, str]][source]

Translates source accoring to lexer rules into an iteratable of tokens.

wheezy.template.lexer.lexer_scan(extensions: List[Any]) → Mapping[str, Any][source]

Scans extensions for lexer_rules and preprocessors attributes.

wheezy.template.loader

class wheezy.template.loader.AutoReloadProxy(engine: wheezy.template.engine.Engine)[source]
get_template(name: str) → wheezy.template.typing.SupportsRender[source]

Returns compiled template.

remove(name: str) → None[source]

Removes given name from internal cache.

render(name: str, ctx: Mapping[str, Any], local_defs: Mapping[str, Any], super_defs: Mapping[str, Any]) → str[source]

Renders template by name in given context.

class wheezy.template.loader.ChainLoader(loaders: List[wheezy.template.typing.Loader])[source]

Loads templates from loaders until first succeed.

list_names() → Tuple[str, ...][source]

Returns as list of names from all loaders.

load(name: str) → Optional[str][source]

Returns template by name from the first loader that succeed.

class wheezy.template.loader.DictLoader(templates: Mapping[str, str])[source]

Loads templates from python dictionary.

templates - a dict where key corresponds to template name and value to template content.

list_names() → Tuple[str, ...][source]

List all keys from internal dict.

load(name: str) → Optional[str][source]

Returns template by name.

class wheezy.template.loader.FileLoader(directories: List[str], encoding: str = 'UTF-8')[source]

Loads templates from file system.

directories - search path of directories to scan for template. encoding - decode template content per encoding.

get_fullname(name: str) → Optional[str][source]

Returns a full path by a template name.

list_names() → Tuple[str, ...][source]

Return a list of names relative to directories. Ignores any files and directories that start with dot.

load(name: str) → Optional[str][source]

Loads a template by name from file system.

class wheezy.template.loader.PreprocessLoader(engine: wheezy.template.engine.Engine, ctx: Optional[Mapping[str, Any]] = None)[source]

Performs preprocessing of loaded template.

wheezy.template.loader.autoreload(engine: wheezy.template.engine.Engine, enabled: bool = True) → wheezy.template.engine.Engine[source]

Auto reload template if changes are detected in file.

Limitation: master (inherited), imported and preprocessed templates.

It is recommended to use application server that supports file reload instead.

wheezy.template.parser

class wheezy.template.parser.Parser(parser_rules: Dict[str, Callable[[str], Union[str, List[str]]]], parser_configs: Optional[List[Callable[[wheezy.template.typing.ParserConfig], None]]] = None, **ignore)[source]

continue_tokens are used to insert end node right before them to simulate a block end. Such nodes have token value None.

out_tokens are combined together into a single node.

end_continue(tokens: List[Tuple[int, str, str]]) → Iterator[Tuple[int, str, str]][source]

If token is in continue_tokens prepend it with end token so it simulate a closed block.

wheezy.template.preprocessor

class wheezy.template.preprocessor.Preprocessor(runtime_engine_factory: Callable[[wheezy.template.typing.Loader], wheezy.template.engine.Engine], engine: wheezy.template.engine.Engine, key_factory: Callable[[Mapping[str, Any]], str])[source]

Preprocess templates with engine and vary runtime templates by key_factory function using runtime_engine_factory.

wheezy.template.utils

wheezy.template.utils.find_all_balanced(text: str, start: int = 0) → int[source]

Finds balanced ([ with ]) assuming that start is pointing to ( or [ in text.

wheezy.template.utils.find_balanced(text: str, start: int = 0, start_sep: str = '(', end_sep: str = ')') → int[source]

Finds balanced start_sep with end_sep assuming that start is pointing to start_sep in text.

wheezy.template.ext.code

class wheezy.template.ext.code.CodeExtension(token_start: str = '@')[source]

Includes support for embedded python code.

wheezy.template.ext.core

class wheezy.template.ext.core.CoreExtension(token_start: str = '@', line_join: str = '\')[source]

Includes basic statements, variables processing and markup.

wheezy.template.ext.core.rvalue_token(m: Match[str]) → Tuple[int, str, str][source]

Produces variable token as r-value expression.

wheezy.template.ext.core.stmt_token(m: Match[str]) → Tuple[int, str, str][source]

Produces statement token.

wheezy.template.ext.core.var_token(m: Match[str]) → Tuple[int, str, str][source]

Produces variable token.

wheezy.template.ext.determined

class wheezy.template.ext.determined.DeterminedExtension(known_calls: List[str], runtime_token_start: str = '@', token_start: str = '#')[source]

Tranlates function calls between template engines.

Strictly determined known calls are converted to preprocessor calls, e.g.:

@_('Name:')
@path_for('default')
@path_for('static', path='/static/css/site.css')

Those that are not strictly determined are ignored and processed by runtime engine.

wheezy.template.ext.determined.determined(expression: str) → bool[source]

Checks if expresion is strictly determined.

>>> determined("'default'")
True
>>> determined('name')
False
>>> determined("'default', id=id")
False
>>> determined("'default', lang=100")
True
>>> determined('')
True
wheezy.template.ext.determined.parse_args(text: str) → List[str][source]

Parses argument type of parameters.

>>> parse_args('')
[]
>>> parse_args('10, "x"')
['10', '"x"']
>>> parse_args("'x', 100")
["'x'", '100']
>>> parse_args('"default"')
['"default"']
wheezy.template.ext.determined.parse_kwargs(text: str) → Mapping[str, str][source]

Parses key-value type of parameters.

>>> parse_kwargs('id=item.id')
{'id': 'item.id'}
>>> sorted(parse_kwargs('lang="en", id=12').items())
[('id', '12'), ('lang', '"en"')]
wheezy.template.ext.determined.parse_params(text: str) → Tuple[List[str], Mapping[str, str]][source]

Parses function parameters.

>>> parse_params('')
([], {})
>>> parse_params('id=item.id')
([], {'id': 'item.id'})
>>> parse_params('"default"')
(['"default"'], {})
>>> parse_params('"default", lang="en"')
(['"default"'], {'lang': '"en"'})
wheezy.template.ext.determined.str_or_int(text: str) → bool[source]

Ensures text as string or int expression.

>>> str_or_int('"default"')
True
>>> str_or_int("'Hello'")
True
>>> str_or_int('100')
True
>>> str_or_int('item.id')
False