Changeset View
Changeset View
Standalone View
Standalone View
helpers/helperslib/Version.py
- This file was added.
1 | import functools | ||||
---|---|---|---|---|---|
2 | from typing import List | ||||
3 | | ||||
4 | ''' To run doctests for this module. | ||||
5 | | ||||
6 | python3 -m doctest -v helpers/helperslib/Version.py | ||||
7 | ''' | ||||
8 | | ||||
9 | SUFFIX2NUMBER = { | ||||
10 | "alpha": 70, | ||||
11 | "a": 70, | ||||
12 | "beta": 80, | ||||
13 | "b": 80, | ||||
14 | "rc": 90 | ||||
15 | } | ||||
16 | | ||||
17 | | ||||
18 | @functools.total_ordering | ||||
19 | class Version: | ||||
20 | '''class for Version and correct ordering. ''' | ||||
21 | | ||||
22 | def __init__(self, version: str) -> None: | ||||
23 | if version.startswith("v"): | ||||
24 | self.version = version[1:] | ||||
25 | else: | ||||
26 | self.version = version | ||||
27 | self._parts = None | ||||
28 | self._suffix = None | ||||
29 | | ||||
30 | def parts(self) -> List: | ||||
31 | '''returns a list with different parts of version. | ||||
32 | | ||||
33 | >>> Version("1.2.3-rc1").parts() | ||||
34 | [1, 2, 3] | ||||
35 | ''' | ||||
36 | if self._parts: | ||||
37 | return self._parts | ||||
38 | | ||||
39 | ps = self.version.split(".") | ||||
40 | | ||||
41 | self._parts = [] | ||||
42 | | ||||
43 | for p in ps: | ||||
44 | try: | ||||
45 | self._parts.append(int(p)) | ||||
46 | except ValueError: | ||||
47 | v, suffix = p.split("-") | ||||
48 | self._parts.append(int(v)) | ||||
49 | self._suffix = suffix | ||||
50 | | ||||
51 | return self._parts | ||||
52 | | ||||
53 | def suffix(self) -> int: | ||||
54 | '''transforms suffix into integer using SUFFIX2NUMBER. | ||||
55 | >>> Version("1.2-alpha").suffix() | ||||
56 | 70 | ||||
57 | >>> Version("1.2-beta").suffix() | ||||
58 | 80 | ||||
59 | >>> Version("1.2-rc").suffix() | ||||
60 | 90 | ||||
61 | >>> Version("1.2-rc1").suffix() | ||||
62 | 91 | ||||
63 | ''' | ||||
64 | | ||||
65 | if not self._parts: | ||||
66 | self.parts() | ||||
67 | | ||||
68 | #no suffix so we are higher than any suffix | ||||
69 | if not self._suffix: | ||||
70 | return 100 | ||||
71 | | ||||
72 | for key, base in SUFFIX2NUMBER.items(): | ||||
73 | if self._suffix.startswith(key): | ||||
74 | addition = 0 | ||||
75 | # handle case with rc1 or beta2 etc. | ||||
76 | if len(self._suffix) > len(key): | ||||
77 | addition = int(self._suffix[len(key):]) | ||||
78 | return base + addition | ||||
79 | | ||||
80 | def __eq__(self, other): | ||||
81 | '''returns True if versions are the same. | ||||
82 | | ||||
83 | >>> Version("1.2") == "1.2" | ||||
84 | True | ||||
85 | | ||||
86 | The KDE logic that 5.3.90 == "5.4-rc" is not implemented! | ||||
87 | | ||||
88 | >>> Version("5.4-rc") == Version("5.3.90") | ||||
89 | False | ||||
90 | ''' | ||||
91 | if hasattr(other, "version"): | ||||
92 | return self.version == other.version | ||||
93 | elif type(other) == str: | ||||
94 | return self == Version(other) | ||||
95 | else: | ||||
96 | NotImplemented | ||||
97 | | ||||
98 | def __lt__(self, other): | ||||
99 | '''returns True if self < other. | ||||
100 | | ||||
101 | >>> Version("1.2.3-rc1") < Version("1.2.3") | ||||
102 | True | ||||
103 | >>> Version("1.2.3") < Version("1.2.3-rc1") | ||||
104 | False | ||||
105 | >>> Version("1.2.3") < Version("1.2.4") | ||||
106 | True | ||||
107 | >>> Version("1.2.3") < Version("1.2.3") | ||||
108 | False | ||||
109 | >>> Version("1.2") < Version("1.2.3") | ||||
110 | True | ||||
111 | >>> Version("1.2.3") < Version("1.2") | ||||
112 | False | ||||
113 | | ||||
114 | The KDE logic that 5.3.89 < "5.4-rc" < 5.3.91 is not implemented! | ||||
115 | Currently: | ||||
116 | >>> Version("5.3.89") < "5.4-rc" | ||||
117 | True | ||||
118 | >>> Version("5.4-rc") < "5.3.91" | ||||
119 | False | ||||
120 | >>> Version("5.3.89") < "5.3.91" | ||||
121 | True | ||||
122 | ''' | ||||
123 | if type(other) == str: | ||||
124 | return self < Version(other) | ||||
125 | if hasattr(other, "parts"): | ||||
126 | parts = self.parts() | ||||
127 | otherParts = other.parts() | ||||
128 | | ||||
129 | for i, part in enumerate(parts): | ||||
130 | try: | ||||
131 | if part == otherParts[i]: | ||||
132 | continue | ||||
133 | return part < otherParts[i] | ||||
134 | except IndexError: | ||||
135 | # @other has less parts than @self. | ||||
136 | return False | ||||
137 | if len(parts) < len(otherParts): | ||||
138 | return True | ||||
139 | return self.suffix() < other.suffix() | ||||
140 | | ||||
141 | def __repr__(self): | ||||
142 | parts = self.parts() | ||||
143 | version = ".".join([str(i) for i in parts]) | ||||
144 | if self._suffix: | ||||
145 | version += "-{}".format(self._suffix) | ||||
146 | | ||||
147 | return "<{}>".format(version) |