More robust handling of non-digits in version

class ArchPkgVersion now handles non-digit version subsections by
splitting them based on them being digits or not.

ArchPkgVersion.versions is now a tuple after initialization.
This commit is contained in:
Stephen Seo 2023-02-04 13:12:23 +09:00
parent cdfb371ea9
commit 74133fb363

View file

@ -24,7 +24,7 @@ AUR_GIT_REPO_PATH = "https://aur.archlinux.org"
AUR_GIT_REPO_PATH_TEMPLATE = AUR_GIT_REPO_PATH + "/{}.git" AUR_GIT_REPO_PATH_TEMPLATE = AUR_GIT_REPO_PATH + "/{}.git"
GLOBAL_LOG_FILE = "log.txt" GLOBAL_LOG_FILE = "log.txt"
DEFAULT_EDITOR = "/usr/bin/nano" DEFAULT_EDITOR = "/usr/bin/nano"
ENDS_WITH_DIGIT_REGEX = re.compile("^(.*?)([0-9]+)$") IS_DIGIT_REGEX = re.compile("^[0-9]+$")
class ArchPkgVersion(): class ArchPkgVersion():
@ -40,18 +40,37 @@ class ArchPkgVersion():
version_str = version_str[:end_dash_idx] version_str = version_str[:end_dash_idx]
for sub in version_str.split('.'): for sub in version_str.split('.'):
try: if IS_DIGIT_REGEX.match(sub) is not None:
integer = int(sub) self.versions.append(int(sub))
self.versions.append(integer)
except ValueError:
match = ENDS_WITH_DIGIT_REGEX.match(sub)
if match is not None:
subversion = []
subversion.append(match.groups()[0])
subversion.append(int(match.groups()[1]))
self.versions.append(tuple(subversion))
else: else:
self.versions.append(sub) subversion = []
string = None
integer = None
for char in sub:
if IS_DIGIT_REGEX.match(char) is not None:
if string is not None:
subversion.append(string)
string = None
if integer is None:
integer = int(char)
else:
integer = integer * 10 + int(char)
else:
if integer is not None:
subversion.append(integer)
integer = None
if string is None:
string = char
else:
string = string + char
if string is not None:
subversion.append(string)
string = None
if integer is not None:
subversion.append(integer)
integer = None
self.versions.append(tuple(subversion))
self.versions = tuple(self.versions)
def compare_with(self, other_self): def compare_with(self, other_self):
self_count = len(self.versions) self_count = len(self.versions)
@ -121,7 +140,6 @@ class ArchPkgVersion():
else: else:
return 0 return 0
def __eq__(self, other): def __eq__(self, other):
if isinstance(other, version.Version): if isinstance(other, version.Version):
other = ArchPkgVersion(str(other)) other = ArchPkgVersion(str(other))