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

1""" 

2Description 

3----------- 

4 

5PythonExtension provides the capability to do actions on the python version string, but also on Python dictionaries. 

6 

7 

8.. autoclass:: PythonExtension 

9 

10 

11.. py:function:: pyversion(lhs) 

12 

13 Return a Python *Major.Minor* version string and return it as a tuple (Major, Minor). 

14 

15 Available as a function and a filter. For the latter case, *lhs* is automatically specified as the left hand side of the filter. 

16 

17 :param lhs: Version string. eg: "3.9" 

18 

19 .. code-block:: jinja 

20 

21 # scaffold.python_min_version is "3.9" for example 

22 {% if (scaffold.python_min_version | pyversion)[1] <= 9 %} 

23 

24 {% if (pyversion("3.9")[1] == 9 %} 

25 

26 

27.. py:function:: pyversion_format(lhs, fmt) 

28 

29 Return a Python *Major.Minor* version string formatted according to format passed as parameter. 

30 

31 Internally uses :py:meth:`str.format`. 

32 

33 Available as a function and a filter. For the latter case, *lhs* is automatically specified as the left hand side of the filter. 

34 

35 :param lhs: Version string. eg: "3.9" 

36 

37 :param fmt: Format string. Available replacements: *major*, *minor* 

38 

39 .. code-block:: jinja 

40 

41 # Returns "3.9-dev" 

42 {{ "3.9" | pyversion_format("Python {major}.{minor}-dev") }} 

43 

44 

45.. py:function:: pyversion_sequence(lhs, stop, sep, fmt) 

46 

47 Return a sequence of Python *Major.Minor* version strings formatted according to format passed as parameter. 

48 

49 Internally uses :py:meth:`str.format`. 

50 

51 Available as a function and a filter. For the latter case, *lhs* is automatically specified as the left hand side of the filter. 

52 

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. 

57 

58 .. code-block:: jinja 

59 

60 # Returns "3.9 3.10 3.11 3.12" 

61 {{ "3.9" | pyversion_sequence(12) }} 

62 

63 

64.. py:function:: to_json(lhs, sort_keys=True, **kwargs) 

65 

66 Converts a Python dictionary to a JSON string. 

67 

68 Internally uses :py:meth:`json.dumps`. 

69 

70 Available as a function and a filter. For the latter case, *lhs* is automatically specified as the left hand side of the filter. 

71 

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` 

75 

76 .. code-block:: jinja 

77 

78 # Returns '{"foobar": 3}' 

79 {{ {'foobar':3} | to_json }} 

80""" 

81 

82import json 

83 

84from jinja2.ext import Extension 

85 

86 

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]) 

93 

94 

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]) 

102 

103 

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 

114 

115 

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) 

121 

122 

123# pylint: disable=abstract-method 

124class PythonExtension(Extension): 

125 """ 

126 Jinja2 extension for python manipulation. 

127 """ 

128 

129 def __init__(self, environment): 

130 """ 

131 Jinja2 Extension constructor. 

132 """ 

133 super().__init__(environment) 

134 

135 environment.filters["pyversion"] = _pyversion 

136 environment.globals.update(pyversion=_pyversion) 

137 

138 environment.filters["pyversion_format"] = _pyversion_format 

139 environment.globals.update(pyversion_format=_pyversion_format) 

140 

141 environment.filters["pyversion_sequence"] = _pyversion_sequence 

142 environment.globals.update(pyversion_sequence=_pyversion_sequence) 

143 

144 environment.filters["to_json"] = _to_json 

145 environment.globals.update(to_json=_to_json)