|
8 | 8 | from socket import timeout
|
9 | 9 | from ssl import SSLError
|
10 | 10 | import struct
|
| 11 | +from typing import NamedTuple |
| 12 | +from functools import total_ordering |
11 | 13 |
|
12 | 14 |
|
13 | 15 | try: # Python 3
|
|
22 | 24 | from .unicodes import compat_path, from_unicode, to_unicode
|
23 | 25 |
|
24 | 26 |
|
| 27 | +@total_ordering |
| 28 | +class Version(NamedTuple): |
| 29 | + """Minimal version class used for parse_version. Should be enough for our purpose.""" |
| 30 | + major: int = 0 |
| 31 | + minor: int = 0 |
| 32 | + micro: int = 0 |
| 33 | + nano: int = 0 |
| 34 | + |
| 35 | + def __str__(self): |
| 36 | + return f"{self.major}.{self.minor}.{self.micro}.{self.nano}" |
| 37 | + |
| 38 | + def __lt__(self, other): |
| 39 | + if self.major != other.major: |
| 40 | + return self.major < other.major |
| 41 | + if self.minor != other.minor: |
| 42 | + return self.minor < other.minor |
| 43 | + if self.micro != other.micro: |
| 44 | + return self.micro < other.micro |
| 45 | + |
| 46 | + return self.nano < other.nano |
| 47 | + |
| 48 | + def __eq__(self, other): |
| 49 | + return all((self.major == other.major, self.minor == other.minor, self.micro == other.micro, self.nano == other.nano)) |
| 50 | + |
| 51 | + |
25 | 52 | def temp_path():
|
26 | 53 | """Return temporary path, usually ~/.kodi/userdata/addon_data/script.module.inputstreamhelper/temp/"""
|
27 | 54 | tmp_path = translate_path(os.path.join(get_setting('temp_path', 'special://masterprofile/addon_data/script.module.inputstreamhelper'), 'temp', ''))
|
@@ -350,11 +377,18 @@ def remove_tree(path):
|
350 | 377 | rmtree(compat_path(path))
|
351 | 378 |
|
352 | 379 |
|
353 |
| -def parse_version(version): |
| 380 | +def parse_version(vstring): |
354 | 381 | """Parse a version string and return a comparable version object"""
|
| 382 | + vstring = vstring.strip('v') |
| 383 | + vstrings = vstring.split('.') |
355 | 384 | try:
|
356 |
| - from packaging.version import parse |
357 |
| - return parse(version) |
358 |
| - except ImportError: |
359 |
| - from distutils.version import LooseVersion # pylint: disable=deprecated-module |
360 |
| - return LooseVersion(version) |
| 385 | + vnums = tuple(int(v) for v in vstrings) |
| 386 | + except ValueError: |
| 387 | + log(3, f"Version string {vstring} can't be interpreted! Contains non-numerics.") |
| 388 | + return Version(0, 0, 0, 0) |
| 389 | + |
| 390 | + if len(vnums) > 4: |
| 391 | + log(3, f"Version string {vstring} can't be interpreted! Too long.") |
| 392 | + return Version(0, 0, 0, 0) |
| 393 | + |
| 394 | + return Version(*vnums) |
0 commit comments