Skip to content

Jinja Context

miniset.jinja_context

JinjaTemplateProcessor(*, param_style='format', identifier_quote_character='"', env=None)

Parameters:

Name Type Description Default
param_style ParamStyleType

Parameter style. Defaults to "format".

'format'
identifier_quote_character IdentifierQuoteCharacterType

Identifier for quote character. Defaults to '"'.

'"'
env Optional[Environment]

Jinja2 environment. Defaults to None.

None
Source code in miniset/jinja_context.py
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def __init__(
    self,
    *,
    param_style: types.ParamStyleType = "format",
    identifier_quote_character: types.IdentifierQuoteCharacterType = '"',
    env: Optional[Environment] = None,
) -> None:
    """Initialize the template processor.

    Args:
        param_style (types.ParamStyleType, optional): Parameter style. Defaults to "format".
        identifier_quote_character (types.IdentifierQuoteCharacterType, optional): Identifier for quote character. Defaults to '"'.
        env (Optional[Environment], optional): Jinja2 environment. Defaults to None.
    """
    self._context: dict[str, Any] = {}

    self._env = env or SandboxedEnvironment(undefined=DebugUndefined)
    self._env.autoescape = True
    self._env.add_extension(SqlExtension)
    self._env.filters["bind"] = self._bind
    self._env.filters["where_in"] = self._where_in
    self._env.filters["sql_safe"] = sql_safe
    self._env.filters["identifier"] = build_identifier_filter(
        identifier_quote_character
    )

    self._param_style: types.ParamStyleType = param_style

    self._param_index: int = 0
    self._bind_params: OrderedDict[str, Any] = OrderedDict()

prepare_query(query, **kwargs)

Prepare a query template

Parameters:

Name Type Description Default
query Union[str, Template]

A query string/template

required

Returns:

Name Type Description
query str

A prepared query

bind_params Union[List[Any], Dict[str, Any]]

Bind params

Source code in miniset/jinja_context.py
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
def prepare_query(
    self, query: Union[str, Template], **kwargs: Any
) -> tuple[str, Union[list[Any], dict[str, Any]]]:
    """Prepare a query template

    Args:
        query (Union[str, Template]): A query string/template

    Returns:
        query (str): A prepared query
        bind_params (Union[List[Any], Dict[str, Any]]): Bind params
    """
    template: Template = (
        query if isinstance(query, Template) else self._env.from_string(query)
    )
    return self._prepare_query(template, **kwargs)

set_context(**kwargs)

Set Jinja2 context

Source code in miniset/jinja_context.py
128
129
130
def set_context(self, **kwargs: Any) -> None:
    """Set Jinja2 context"""
    self._context.update(kwargs)