From: Daan De Meyer Date: Mon, 1 Jan 2024 14:20:16 +0000 (+0100) Subject: Use functools.total_ordering for GenericVersion X-Git-Tag: v20~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8fc53f458bfeee0d2ae75e0cad50458539e3f4fa;p=thirdparty%2Fmkosi.git Use functools.total_ordering for GenericVersion --- diff --git a/mkosi/versioncomp.py b/mkosi/versioncomp.py index 9025a5a7a..85da28147 100644 --- a/mkosi/versioncomp.py +++ b/mkosi/versioncomp.py @@ -1,9 +1,11 @@ # SPDX-License-Identifier: LGPL-2.1+ +import functools +import itertools import string -from itertools import takewhile +@functools.total_ordering class GenericVersion: # These constants follow the convention of the return value of rpmdev-vercmp that are followe # by systemd-analyze compare-versions when called with only two arguments (without a comparison @@ -26,10 +28,10 @@ class GenericVersion: return "" def digit_prefix(s: str) -> str: - return "".join(takewhile(lambda c: c in string.digits, s)) + return "".join(itertools.takewhile(lambda c: c in string.digits, s)) def letter_prefix(s: str) -> str: - return "".join(takewhile(lambda c: c in string.ascii_letters, s)) + return "".join(itertools.takewhile(lambda c: c in string.ascii_letters, s)) while True: # Any characters which are outside of the set of listed above (a-z, A-Z, 0-9, -, ., ~, @@ -145,13 +147,6 @@ class GenericVersion: return False return self.compare_versions(self._version, other._version) == self._EQUAL - def __ne__(self, other: object) -> bool: - if isinstance(other, (str, int)): - other = GenericVersion(str(other)) - elif not isinstance(other, GenericVersion): - return False - return self.compare_versions(self._version, other._version) != self._EQUAL - def __lt__(self, other: object) -> bool: if isinstance(other, (str, int)): other = GenericVersion(str(other)) @@ -159,26 +154,5 @@ class GenericVersion: return False return self.compare_versions(self._version, other._version) == self._LEFT_SMALLER - def __le__(self, other: object) -> bool: - if isinstance(other, (str, int)): - other = GenericVersion(str(other)) - elif not isinstance(other, GenericVersion): - return False - return self.compare_versions(self._version, other._version) in (self._EQUAL, self._LEFT_SMALLER) - - def __gt__(self, other: object) -> bool: - if isinstance(other, (str, int)): - other = GenericVersion(str(other)) - elif not isinstance(other, GenericVersion): - return False - return self.compare_versions(self._version, other._version) == self._RIGHT_SMALLER - - def __ge__(self, other: object) -> bool: - if isinstance(other, (str, int)): - other = GenericVersion(str(other)) - elif not isinstance(other, GenericVersion): - return False - return self.compare_versions(self._version, other._version) in (self._EQUAL, self._RIGHT_SMALLER) - def __str__(self) -> str: return self._version