Coverage for jinjazzle/ext/version.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-24 22:03 +0000

1""" 

2Description 

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

4 

5VersionExtension provides the capability to handle semver version strings. 

6 

7 

8.. autoclass:: VersionExtension 

9 

10 

11.. py:function:: revpkg(lhs, separator1=".", separator2=".") 

12 

13 Take a version, and reverse it in the most simplistic way not taking into 

14 account anything that would constitute "pre-release" or "build" information in the 

15 context of semver. 

16 

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

18 

19 .. code-block:: jinja 

20 

21 # Returns 3.2.1 

22 {{ revpkg("1.2.3") }} 

23 

24""" 

25 

26from jinja2.ext import Extension 

27 

28 

29def _revpkg(lhs, separator1=".", separator2="."): 

30 lhss = lhs.split(separator1) 

31 lhss = reversed(lhss) 

32 return separator2.join(lhss) 

33 

34 

35# pylint: disable=abstract-method 

36class VersionExtension(Extension): 

37 """ 

38 Jinja2 extension for version manipulation. 

39 """ 

40 

41 def __init__(self, environment): 

42 """ 

43 Jinja2 Extension constructor. 

44 """ 

45 super().__init__(environment) 

46 

47 environment.filters["revpkg"] = _revpkg 

48 environment.globals.update(revpkg=_revpkg)