Skip to content

Commit 5500b64

Browse files
fix version parsing (#577)
1 parent 8ca0a13 commit 5500b64

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

lib/inputstreamhelper/utils.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from socket import timeout
1212
from ssl import SSLError
1313
from time import time
14-
from typing import NamedTuple
1514
from urllib.error import HTTPError, URLError
1615
from urllib.request import Request, urlopen
1716

@@ -23,29 +22,30 @@
2322

2423

2524
@total_ordering
26-
class Version(NamedTuple):
27-
"""Minimal version class used for parse_version. Should be enough for our purpose."""
28-
major: int = 0
29-
minor: int = 0
30-
micro: int = 0
31-
nano: int = 0
25+
class Version:
26+
def __init__(self, *components):
27+
self.components = list(components)
3228

3329
def __str__(self):
34-
return f"{self.major}.{self.minor}.{self.micro}.{self.nano}"
30+
return '.'.join(map(str, self.components))
3531

3632
def __lt__(self, other):
37-
if self.major != other.major:
38-
return self.major < other.major
39-
if self.minor != other.minor:
40-
return self.minor < other.minor
41-
if self.micro != other.micro:
42-
return self.micro < other.micro
43-
44-
return self.nano < other.nano
33+
# extended comparison that accounts for different lengths by padding with zeros
34+
max_length = max(len(self.components), len(other.components))
35+
# extend both lists with zeros up to the maximum length
36+
extended_self = self.components + [0] * (max_length - len(self.components))
37+
extended_other = other.components + [0] * (max_length - len(other.components))
38+
39+
for self_comp, other_comp in zip(extended_self, extended_other):
40+
if self_comp < other_comp:
41+
return True
42+
elif self_comp > other_comp:
43+
return False
44+
return False # return False if all comparisons are equal
4545

4646
def __eq__(self, other):
47-
return all((self.major == other.major, self.minor == other.minor, self.micro == other.micro, self.nano == other.nano))
48-
47+
# Uses the same logic for equality
48+
return not self < other and not other < self
4949

5050
def temp_path():
5151
"""Return temporary path, usually ~/.kodi/userdata/addon_data/script.module.inputstreamhelper/temp/"""
@@ -376,7 +376,4 @@ def parse_version(vstring):
376376
else:
377377
vnums.append(0) # default to 0 if no numeric part found
378378

379-
# ensure the version tuple always has 4 components
380-
vnums = (vnums + [0] * 4)[:4]
381-
382379
return Version(*vnums)

0 commit comments

Comments
 (0)