From 7ffcfb2a5e9c0430430cfb5066bb381bb9ac98a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= <6774676+eumiro@users.noreply.github.com> Date: Tue, 29 Nov 2022 18:37:53 +0100 Subject: refactor: improve version comparisons --- mastodon/Mastodon.py | 51 ++++++++++++++++++--------------------------------- 1 file changed, 18 insertions(+), 33 deletions(-) (limited to 'mastodon') diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 895fa36..e1d015a 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -69,27 +69,12 @@ except: def parse_version_string(version_string): """Parses a semver version string, stripping off "rc" stuff if present.""" - string_parts = version_string.split(".") - version_parts = [ - int(re.match("([0-9]*)", string_parts[0]).group(0)), - int(re.match("([0-9]*)", string_parts[1]).group(0)), - int(re.match("([0-9]*)", string_parts[2]).group(0)) - ] - return version_parts + return tuple(int(x) for x in re.findall(r"\d+", version_string))[:3] -def bigger_version(version_string_a, version_string_b): - """Returns the bigger version of two version strings.""" - major_a, minor_a, patch_a = parse_version_string(version_string_a) - major_b, minor_b, patch_b = parse_version_string(version_string_b) - - if major_a > major_b: - return version_string_a - elif major_a == major_b and minor_a > minor_b: - return version_string_a - elif major_a == major_b and minor_a == minor_b and patch_a > patch_b: - return version_string_a - return version_string_b +def max_version(*version_strings): + """Returns the maximum version of all provided version strings.""" + return max(version_strings, key=parse_version_string) def api_version(created_ver, last_changed_ver, return_value_ver): @@ -100,8 +85,7 @@ def api_version(created_ver, last_changed_ver, return_value_ver): if self.version_check_mode == "created": version = created_ver else: - version = bigger_version( - last_changed_ver, return_value_ver) + version = max_version(last_changed_ver, return_value_ver) major, minor, patch = parse_version_string(version) if major > self.mastodon_major: raise MastodonVersionError( @@ -236,32 +220,33 @@ class Mastodon: __DICT_VERSION_MEDIA = "3.2.0" __DICT_VERSION_ACCOUNT = "3.3.0" __DICT_VERSION_POLL = "2.8.0" - __DICT_VERSION_STATUS = bigger_version(bigger_version(bigger_version(bigger_version(bigger_version( - "3.1.0", __DICT_VERSION_MEDIA), __DICT_VERSION_ACCOUNT), __DICT_VERSION_APPLICATION), __DICT_VERSION_MENTION), __DICT_VERSION_POLL) - __DICT_VERSION_INSTANCE = bigger_version("3.4.0", __DICT_VERSION_ACCOUNT) + __DICT_VERSION_STATUS = max_version("3.1.0", __DICT_VERSION_MEDIA, __DICT_VERSION_ACCOUNT, + __DICT_VERSION_APPLICATION, __DICT_VERSION_MENTION, __DICT_VERSION_POLL) + __DICT_VERSION_INSTANCE = max_version("3.4.0", __DICT_VERSION_ACCOUNT) __DICT_VERSION_HASHTAG = "2.3.4" __DICT_VERSION_EMOJI = "3.0.0" __DICT_VERSION_RELATIONSHIP = "3.3.0" - __DICT_VERSION_NOTIFICATION = bigger_version(bigger_version("3.5.0", __DICT_VERSION_ACCOUNT), __DICT_VERSION_STATUS) - __DICT_VERSION_CONTEXT = bigger_version("1.0.0", __DICT_VERSION_STATUS) + __DICT_VERSION_NOTIFICATION = max_version("3.5.0", __DICT_VERSION_ACCOUNT, __DICT_VERSION_STATUS) + __DICT_VERSION_CONTEXT = max_version("1.0.0", __DICT_VERSION_STATUS) __DICT_VERSION_LIST = "2.1.0" __DICT_VERSION_CARD = "3.2.0" - __DICT_VERSION_SEARCHRESULT = bigger_version(bigger_version(bigger_version("1.0.0", __DICT_VERSION_ACCOUNT), __DICT_VERSION_STATUS), __DICT_VERSION_HASHTAG) + __DICT_VERSION_SEARCHRESULT = max_version("1.0.0", __DICT_VERSION_ACCOUNT, + __DICT_VERSION_STATUS, __DICT_VERSION_HASHTAG) __DICT_VERSION_ACTIVITY = "2.1.2" __DICT_VERSION_REPORT = "2.9.1" __DICT_VERSION_PUSH = "2.4.0" __DICT_VERSION_PUSH_NOTIF = "2.4.0" __DICT_VERSION_FILTER = "2.4.3" - __DICT_VERSION_CONVERSATION = bigger_version(bigger_version("2.6.0", __DICT_VERSION_ACCOUNT), __DICT_VERSION_STATUS) - __DICT_VERSION_SCHEDULED_STATUS = bigger_version("2.7.0", __DICT_VERSION_STATUS) + __DICT_VERSION_CONVERSATION = max_version("2.6.0", __DICT_VERSION_ACCOUNT, __DICT_VERSION_STATUS) + __DICT_VERSION_SCHEDULED_STATUS = max_version("2.7.0", __DICT_VERSION_STATUS) __DICT_VERSION_PREFERENCES = "2.8.0" - __DICT_VERSION_ADMIN_ACCOUNT = bigger_version("4.0.0", __DICT_VERSION_ACCOUNT) + __DICT_VERSION_ADMIN_ACCOUNT = max_version("4.0.0", __DICT_VERSION_ACCOUNT) __DICT_VERSION_FEATURED_TAG = "3.0.0" __DICT_VERSION_MARKER = "3.0.0" __DICT_VERSION_REACTION = "3.1.0" - __DICT_VERSION_ANNOUNCEMENT = bigger_version("3.1.0", __DICT_VERSION_REACTION) + __DICT_VERSION_ANNOUNCEMENT = max_version("3.1.0", __DICT_VERSION_REACTION) __DICT_VERSION_STATUS_EDIT = "3.5.0" - __DICT_VERSION_FAMILIAR_FOLLOWERS = bigger_version("3.5.0", __DICT_VERSION_ACCOUNT) + __DICT_VERSION_FAMILIAR_FOLLOWERS = max_version("3.5.0", __DICT_VERSION_ACCOUNT) __DICT_VERSION_ADMIN_DOMAIN_BLOCK = "4.0.0" __DICT_VERSION_ADMIN_MEASURE = "3.5.0" __DICT_VERSION_ADMIN_DIMENSION = "3.5.0" @@ -3709,7 +3694,7 @@ class Mastodon: crypto_ver = cryptography.__version__ if len(crypto_ver) < 5: crypto_ver += ".0" - if bigger_version(crypto_ver, "2.5.0") == crypto_ver: + if parse_version_string(crypto_ver) == (2, 5, 0): push_key_pub = push_key_pair.public_key().public_bytes(serialization.Encoding.X962, serialization.PublicFormat.UncompressedPoint) else: push_key_pub = push_key_pair.public_key().public_numbers().encode_point() -- cgit v1.2.3