Skip to content

Commit af6adc1

Browse files
authored
Implement own "Version" class to get rid of distutils/packaging dependency. (#561)
Fixes #550.
1 parent b42b9e8 commit af6adc1

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

lib/inputstreamhelper/utils.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from socket import timeout
99
from ssl import SSLError
1010
import struct
11+
from typing import NamedTuple
12+
from functools import total_ordering
1113

1214

1315
try: # Python 3
@@ -22,6 +24,31 @@
2224
from .unicodes import compat_path, from_unicode, to_unicode
2325

2426

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+
2552
def temp_path():
2653
"""Return temporary path, usually ~/.kodi/userdata/addon_data/script.module.inputstreamhelper/temp/"""
2754
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):
350377
rmtree(compat_path(path))
351378

352379

353-
def parse_version(version):
380+
def parse_version(vstring):
354381
"""Parse a version string and return a comparable version object"""
382+
vstring = vstring.strip('v')
383+
vstrings = vstring.split('.')
355384
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

Comments
 (0)