diff options
author | Lorenz Diener <[email protected]> | 2022-11-30 17:59:37 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2022-11-30 17:59:37 +0200 |
commit | 77e77fa9e8648934ab4c4569e1356dfd3a61abeb (patch) | |
tree | 5ad9bd53ae1e54d7190ec519b654bc78cff1d956 | |
parent | 7635c00d253baf8f48d3a84665178c782b7ef533 (diff) | |
parent | 7ffcfb2a5e9c0430430cfb5066bb381bb9ac98a5 (diff) | |
download | mastodon.py-77e77fa9e8648934ab4c4569e1356dfd3a61abeb.tar.gz |
Merge pull request #285 from eumiro/cmp_versions
refactor: improve version comparisons
-rw-r--r-- | mastodon/Mastodon.py | 51 |
1 files changed, 18 insertions, 33 deletions
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: | |||
69 | 69 | ||
70 | def parse_version_string(version_string): | 70 | def parse_version_string(version_string): |
71 | """Parses a semver version string, stripping off "rc" stuff if present.""" | 71 | """Parses a semver version string, stripping off "rc" stuff if present.""" |
72 | string_parts = version_string.split(".") | 72 | return tuple(int(x) for x in re.findall(r"\d+", version_string))[:3] |
73 | version_parts = [ | ||
74 | int(re.match("([0-9]*)", string_parts[0]).group(0)), | ||
75 | int(re.match("([0-9]*)", string_parts[1]).group(0)), | ||
76 | int(re.match("([0-9]*)", string_parts[2]).group(0)) | ||
77 | ] | ||
78 | return version_parts | ||
79 | 73 | ||
80 | 74 | ||
81 | def bigger_version(version_string_a, version_string_b): | 75 | def max_version(*version_strings): |
82 | """Returns the bigger version of two version strings.""" | 76 | """Returns the maximum version of all provided version strings.""" |
83 | major_a, minor_a, patch_a = parse_version_string(version_string_a) | 77 | return max(version_strings, key=parse_version_string) |
84 | major_b, minor_b, patch_b = parse_version_string(version_string_b) | ||
85 | |||
86 | if major_a > major_b: | ||
87 | return version_string_a | ||
88 | elif major_a == major_b and minor_a > minor_b: | ||
89 | return version_string_a | ||
90 | elif major_a == major_b and minor_a == minor_b and patch_a > patch_b: | ||
91 | return version_string_a | ||
92 | return version_string_b | ||
93 | 78 | ||
94 | 79 | ||
95 | def api_version(created_ver, last_changed_ver, return_value_ver): | 80 | 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): | |||
100 | if self.version_check_mode == "created": | 85 | if self.version_check_mode == "created": |
101 | version = created_ver | 86 | version = created_ver |
102 | else: | 87 | else: |
103 | version = bigger_version( | 88 | version = max_version(last_changed_ver, return_value_ver) |
104 | last_changed_ver, return_value_ver) | ||
105 | major, minor, patch = parse_version_string(version) | 89 | major, minor, patch = parse_version_string(version) |
106 | if major > self.mastodon_major: | 90 | if major > self.mastodon_major: |
107 | raise MastodonVersionError( | 91 | raise MastodonVersionError( |
@@ -236,32 +220,33 @@ class Mastodon: | |||
236 | __DICT_VERSION_MEDIA = "3.2.0" | 220 | __DICT_VERSION_MEDIA = "3.2.0" |
237 | __DICT_VERSION_ACCOUNT = "3.3.0" | 221 | __DICT_VERSION_ACCOUNT = "3.3.0" |
238 | __DICT_VERSION_POLL = "2.8.0" | 222 | __DICT_VERSION_POLL = "2.8.0" |
239 | __DICT_VERSION_STATUS = bigger_version(bigger_version(bigger_version(bigger_version(bigger_version( | 223 | __DICT_VERSION_STATUS = max_version("3.1.0", __DICT_VERSION_MEDIA, __DICT_VERSION_ACCOUNT, |
240 | "3.1.0", __DICT_VERSION_MEDIA), __DICT_VERSION_ACCOUNT), __DICT_VERSION_APPLICATION), __DICT_VERSION_MENTION), __DICT_VERSION_POLL) | 224 | __DICT_VERSION_APPLICATION, __DICT_VERSION_MENTION, __DICT_VERSION_POLL) |
241 | __DICT_VERSION_INSTANCE = bigger_version("3.4.0", __DICT_VERSION_ACCOUNT) | 225 | __DICT_VERSION_INSTANCE = max_version("3.4.0", __DICT_VERSION_ACCOUNT) |
242 | __DICT_VERSION_HASHTAG = "2.3.4" | 226 | __DICT_VERSION_HASHTAG = "2.3.4" |
243 | __DICT_VERSION_EMOJI = "3.0.0" | 227 | __DICT_VERSION_EMOJI = "3.0.0" |
244 | __DICT_VERSION_RELATIONSHIP = "3.3.0" | 228 | __DICT_VERSION_RELATIONSHIP = "3.3.0" |
245 | __DICT_VERSION_NOTIFICATION = bigger_version(bigger_version("3.5.0", __DICT_VERSION_ACCOUNT), __DICT_VERSION_STATUS) | 229 | __DICT_VERSION_NOTIFICATION = max_version("3.5.0", __DICT_VERSION_ACCOUNT, __DICT_VERSION_STATUS) |
246 | __DICT_VERSION_CONTEXT = bigger_version("1.0.0", __DICT_VERSION_STATUS) | 230 | __DICT_VERSION_CONTEXT = max_version("1.0.0", __DICT_VERSION_STATUS) |
247 | __DICT_VERSION_LIST = "2.1.0" | 231 | __DICT_VERSION_LIST = "2.1.0" |
248 | __DICT_VERSION_CARD = "3.2.0" | 232 | __DICT_VERSION_CARD = "3.2.0" |
249 | __DICT_VERSION_SEARCHRESULT = bigger_version(bigger_version(bigger_version("1.0.0", __DICT_VERSION_ACCOUNT), __DICT_VERSION_STATUS), __DICT_VERSION_HASHTAG) | 233 | __DICT_VERSION_SEARCHRESULT = max_version("1.0.0", __DICT_VERSION_ACCOUNT, |
234 | __DICT_VERSION_STATUS, __DICT_VERSION_HASHTAG) | ||
250 | __DICT_VERSION_ACTIVITY = "2.1.2" | 235 | __DICT_VERSION_ACTIVITY = "2.1.2" |
251 | __DICT_VERSION_REPORT = "2.9.1" | 236 | __DICT_VERSION_REPORT = "2.9.1" |
252 | __DICT_VERSION_PUSH = "2.4.0" | 237 | __DICT_VERSION_PUSH = "2.4.0" |
253 | __DICT_VERSION_PUSH_NOTIF = "2.4.0" | 238 | __DICT_VERSION_PUSH_NOTIF = "2.4.0" |
254 | __DICT_VERSION_FILTER = "2.4.3" | 239 | __DICT_VERSION_FILTER = "2.4.3" |
255 | __DICT_VERSION_CONVERSATION = bigger_version(bigger_version("2.6.0", __DICT_VERSION_ACCOUNT), __DICT_VERSION_STATUS) | 240 | __DICT_VERSION_CONVERSATION = max_version("2.6.0", __DICT_VERSION_ACCOUNT, __DICT_VERSION_STATUS) |
256 | __DICT_VERSION_SCHEDULED_STATUS = bigger_version("2.7.0", __DICT_VERSION_STATUS) | 241 | __DICT_VERSION_SCHEDULED_STATUS = max_version("2.7.0", __DICT_VERSION_STATUS) |
257 | __DICT_VERSION_PREFERENCES = "2.8.0" | 242 | __DICT_VERSION_PREFERENCES = "2.8.0" |
258 | __DICT_VERSION_ADMIN_ACCOUNT = bigger_version("4.0.0", __DICT_VERSION_ACCOUNT) | 243 | __DICT_VERSION_ADMIN_ACCOUNT = max_version("4.0.0", __DICT_VERSION_ACCOUNT) |
259 | __DICT_VERSION_FEATURED_TAG = "3.0.0" | 244 | __DICT_VERSION_FEATURED_TAG = "3.0.0" |
260 | __DICT_VERSION_MARKER = "3.0.0" | 245 | __DICT_VERSION_MARKER = "3.0.0" |
261 | __DICT_VERSION_REACTION = "3.1.0" | 246 | __DICT_VERSION_REACTION = "3.1.0" |
262 | __DICT_VERSION_ANNOUNCEMENT = bigger_version("3.1.0", __DICT_VERSION_REACTION) | 247 | __DICT_VERSION_ANNOUNCEMENT = max_version("3.1.0", __DICT_VERSION_REACTION) |
263 | __DICT_VERSION_STATUS_EDIT = "3.5.0" | 248 | __DICT_VERSION_STATUS_EDIT = "3.5.0" |
264 | __DICT_VERSION_FAMILIAR_FOLLOWERS = bigger_version("3.5.0", __DICT_VERSION_ACCOUNT) | 249 | __DICT_VERSION_FAMILIAR_FOLLOWERS = max_version("3.5.0", __DICT_VERSION_ACCOUNT) |
265 | __DICT_VERSION_ADMIN_DOMAIN_BLOCK = "4.0.0" | 250 | __DICT_VERSION_ADMIN_DOMAIN_BLOCK = "4.0.0" |
266 | __DICT_VERSION_ADMIN_MEASURE = "3.5.0" | 251 | __DICT_VERSION_ADMIN_MEASURE = "3.5.0" |
267 | __DICT_VERSION_ADMIN_DIMENSION = "3.5.0" | 252 | __DICT_VERSION_ADMIN_DIMENSION = "3.5.0" |
@@ -3709,7 +3694,7 @@ class Mastodon: | |||
3709 | crypto_ver = cryptography.__version__ | 3694 | crypto_ver = cryptography.__version__ |
3710 | if len(crypto_ver) < 5: | 3695 | if len(crypto_ver) < 5: |
3711 | crypto_ver += ".0" | 3696 | crypto_ver += ".0" |
3712 | if bigger_version(crypto_ver, "2.5.0") == crypto_ver: | 3697 | if parse_version_string(crypto_ver) == (2, 5, 0): |
3713 | push_key_pub = push_key_pair.public_key().public_bytes(serialization.Encoding.X962, serialization.PublicFormat.UncompressedPoint) | 3698 | push_key_pub = push_key_pair.public_key().public_bytes(serialization.Encoding.X962, serialization.PublicFormat.UncompressedPoint) |
3714 | else: | 3699 | else: |
3715 | push_key_pub = push_key_pair.public_key().public_numbers().encode_point() | 3700 | push_key_pub = push_key_pair.public_key().public_numbers().encode_point() |