Coverage for jinjazzle/ext/python.py: 100%
27 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-24 22:03 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-24 22:03 +0000
1"""
2Description
3-----------
5PythonExtension provides the capability to do actions on the python version string, but also on Python dictionaries.
8.. autoclass:: PythonExtension
11.. py:function:: pyversion(lhs)
13 Return a Python *Major.Minor* version string and return it as a tuple (Major, Minor).
15 Available as a function and a filter. For the latter case, *lhs* is automatically specified as the left hand side of the filter.
17 :param lhs: Version string. eg: "3.9"
19 .. code-block:: jinja
21 # scaffold.python_min_version is "3.9" for example
22 {% if (scaffold.python_min_version | pyversion)[1] <= 9 %}
24 {% if (pyversion("3.9")[1] == 9 %}
27.. py:function:: pyversion_format(lhs, fmt)
29 Return a Python *Major.Minor* version string formatted according to format passed as parameter.
31 Internally uses :py:meth:`str.format`.
33 Available as a function and a filter. For the latter case, *lhs* is automatically specified as the left hand side of the filter.
35 :param lhs: Version string. eg: "3.9"
37 :param fmt: Format string. Available replacements: *major*, *minor*
39 .. code-block:: jinja
41 # Returns "3.9-dev"
42 {{ "3.9" | pyversion_format("Python {major}.{minor}-dev") }}
45.. py:function:: pyversion_sequence(lhs, stop, sep, fmt)
47 Return a sequence of Python *Major.Minor* version strings formatted according to format passed as parameter.
49 Internally uses :py:meth:`str.format`.
51 Available as a function and a filter. For the latter case, *lhs* is automatically specified as the left hand side of the filter.
53 :param lhs: Version string. eg: "3.9"
54 :param stop: Generate sequence from minor version, to this value (included)
55 :param fmt: Format string. Available replacements: *major*, *minor*
56 :param sep: Separator to use for joining the formatted strings.
58 .. code-block:: jinja
60 # Returns "3.9 3.10 3.11 3.12"
61 {{ "3.9" | pyversion_sequence(12) }}
64.. py:function:: to_json(lhs, sort_keys=True, **kwargs)
66 Converts a Python dictionary to a JSON string.
68 Internally uses :py:meth:`json.dumps`.
70 Available as a function and a filter. For the latter case, *lhs* is automatically specified as the left hand side of the filter.
72 :param lhs: Python dictionary
73 :param sort_keys: Output of dictionaries will be sorted by key
74 :param kwargs: Any of the values allowed for :py:meth:`json.dumps`
76 .. code-block:: jinja
78 # Returns '{"foobar": 3}'
79 {{ {'foobar':3} | to_json }}
80"""
82import json
84from jinja2.ext import Extension
87def _pyversion(lhs):
88 """
89 Take a string with format of "Major.Minor" and return it formatted as tuple (Major, Minor)
90 """
91 values = str(lhs).split(".")
92 return int(values[0]), int(values[1])
95def _pyversion_format(lhs, fmt):
96 """
97 Take a string with format of "Major.Minor" and return it formatted as fmt with
98 keys of 'major', and 'minor'
99 """
100 values = _pyversion(lhs)
101 return fmt.format(major=values[0], minor=values[1])
104def _pyversion_sequence(lhs, stop, sep=" ", fmt="{major}.{minor}"):
105 """
106 Take a format value and return it formatted according to `fmt` for the
107 range from minor lfs to minor stop
108 """
109 major, minor = _pyversion(lhs)
110 values = list(range(minor, stop + 1))
111 values = [fmt.format(major=major, minor=i) for i in values]
112 values = sep.join(values)
113 return values
116def _to_json(lhs, sort_keys=True, **kwargs):
117 """
118 Take an object and convert it to a JSON string
119 """
120 return json.dumps(lhs, sort_keys=sort_keys, **kwargs)
123# pylint: disable=abstract-method
124class PythonExtension(Extension):
125 """
126 Jinja2 extension for python manipulation.
127 """
129 def __init__(self, environment):
130 """
131 Jinja2 Extension constructor.
132 """
133 super().__init__(environment)
135 environment.filters["pyversion"] = _pyversion
136 environment.globals.update(pyversion=_pyversion)
138 environment.filters["pyversion_format"] = _pyversion_format
139 environment.globals.update(pyversion_format=_pyversion_format)
141 environment.filters["pyversion_sequence"] = _pyversion_sequence
142 environment.globals.update(pyversion_sequence=_pyversion_sequence)
144 environment.filters["to_json"] = _to_json
145 environment.globals.update(to_json=_to_json)