diff options
-rw-r--r-- | mastodon/Mastodon.py | 65 | ||||
-rw-r--r-- | tests/test_instance.py | 20 |
2 files changed, 32 insertions, 53 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index e1a7a83..18171e7 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,18 +85,14 @@ 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("Version check failed (Need version " + version + ")") |
108 | "Version check failed (Need version " + version + ")") | ||
109 | elif major == self.mastodon_major and minor > self.mastodon_minor: | 92 | elif major == self.mastodon_major and minor > self.mastodon_minor: |
110 | raise MastodonVersionError( | 93 | raise MastodonVersionError("Version check failed (Need version " + version + ")") |
111 | "Version check failed (Need version " + version + ")") | ||
112 | elif major == self.mastodon_major and minor == self.mastodon_minor and patch > self.mastodon_patch: | 94 | elif major == self.mastodon_major and minor == self.mastodon_minor and patch > self.mastodon_patch: |
113 | raise MastodonVersionError( | 95 | raise MastodonVersionError("Version check failed (Need version " + version + ", patch is " + str(self.mastodon_patch) + ")") |
114 | "Version check failed (Need version " + version + ", patch is " + str(self.mastodon_patch) + ")") | ||
115 | return function(self, *args, **kwargs) | 96 | return function(self, *args, **kwargs) |
116 | function.__doc__ = function.__doc__ + "\n\n *Added: Mastodon v" + \ | 97 | function.__doc__ = function.__doc__ + "\n\n *Added: Mastodon v" + \ |
117 | created_ver + ", last changed: Mastodon v" + last_changed_ver + "*" | 98 | created_ver + ", last changed: Mastodon v" + last_changed_ver + "*" |
@@ -122,8 +103,6 @@ def api_version(created_ver, last_changed_ver, return_value_ver): | |||
122 | # Dict helper class. | 103 | # Dict helper class. |
123 | # Defined at top level so it can be pickled. | 104 | # Defined at top level so it can be pickled. |
124 | ### | 105 | ### |
125 | |||
126 | |||
127 | class AttribAccessDict(dict): | 106 | class AttribAccessDict(dict): |
128 | def __getattr__(self, attr): | 107 | def __getattr__(self, attr): |
129 | if attr in self: | 108 | if attr in self: |
@@ -228,40 +207,40 @@ class Mastodon: | |||
228 | __SCOPE_SETS['read'] + __SCOPE_SETS['write'] + \ | 207 | __SCOPE_SETS['read'] + __SCOPE_SETS['write'] + \ |
229 | __SCOPE_SETS['admin:read'] + __SCOPE_SETS['admin:write'] | 208 | __SCOPE_SETS['admin:read'] + __SCOPE_SETS['admin:write'] |
230 | 209 | ||
231 | __SUPPORTED_MASTODON_VERSION = "3.1.1" | 210 | __SUPPORTED_MASTODON_VERSION = "3.5.4" |
232 | 211 | ||
233 | # Dict versions | 212 | # Dict versions |
213 | # Dict versions | ||
234 | __DICT_VERSION_APPLICATION = "2.7.2" | 214 | __DICT_VERSION_APPLICATION = "2.7.2" |
235 | __DICT_VERSION_MENTION = "1.0.0" | 215 | __DICT_VERSION_MENTION = "1.0.0" |
236 | __DICT_VERSION_MEDIA = "3.2.0" | 216 | __DICT_VERSION_MEDIA = "3.2.0" |
237 | __DICT_VERSION_ACCOUNT = "3.3.0" | 217 | __DICT_VERSION_ACCOUNT = "3.3.0" |
238 | __DICT_VERSION_POLL = "2.8.0" | 218 | __DICT_VERSION_POLL = "2.8.0" |
239 | __DICT_VERSION_STATUS = bigger_version(bigger_version(bigger_version(bigger_version(bigger_version( | 219 | __DICT_VERSION_STATUS = max_version("3.1.0", __DICT_VERSION_MEDIA, __DICT_VERSION_ACCOUNT, __DICT_VERSION_APPLICATION, __DICT_VERSION_MENTION, __DICT_VERSION_POLL) |
240 | "3.1.0", __DICT_VERSION_MEDIA), __DICT_VERSION_ACCOUNT), __DICT_VERSION_APPLICATION), __DICT_VERSION_MENTION), __DICT_VERSION_POLL) | 220 | __DICT_VERSION_INSTANCE = max_version("3.4.0", __DICT_VERSION_ACCOUNT) |
241 | __DICT_VERSION_INSTANCE = bigger_version("3.4.0", __DICT_VERSION_ACCOUNT) | ||
242 | __DICT_VERSION_HASHTAG = "2.3.4" | 221 | __DICT_VERSION_HASHTAG = "2.3.4" |
243 | __DICT_VERSION_EMOJI = "3.0.0" | 222 | __DICT_VERSION_EMOJI = "3.0.0" |
244 | __DICT_VERSION_RELATIONSHIP = "3.3.0" | 223 | __DICT_VERSION_RELATIONSHIP = "3.3.0" |
245 | __DICT_VERSION_NOTIFICATION = bigger_version(bigger_version("3.5.0", __DICT_VERSION_ACCOUNT), __DICT_VERSION_STATUS) | 224 | __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) | 225 | __DICT_VERSION_CONTEXT = max_version("1.0.0", __DICT_VERSION_STATUS) |
247 | __DICT_VERSION_LIST = "2.1.0" | 226 | __DICT_VERSION_LIST = "2.1.0" |
248 | __DICT_VERSION_CARD = "3.2.0" | 227 | __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) | 228 | __DICT_VERSION_SEARCHRESULT = max_version("1.0.0", __DICT_VERSION_ACCOUNT, __DICT_VERSION_STATUS, __DICT_VERSION_HASHTAG) |
250 | __DICT_VERSION_ACTIVITY = "2.1.2" | 229 | __DICT_VERSION_ACTIVITY = "2.1.2" |
251 | __DICT_VERSION_REPORT = "2.9.1" | 230 | __DICT_VERSION_REPORT = "2.9.1" |
252 | __DICT_VERSION_PUSH = "2.4.0" | 231 | __DICT_VERSION_PUSH = "2.4.0" |
253 | __DICT_VERSION_PUSH_NOTIF = "2.4.0" | 232 | __DICT_VERSION_PUSH_NOTIF = "2.4.0" |
254 | __DICT_VERSION_FILTER = "2.4.3" | 233 | __DICT_VERSION_FILTER = "2.4.3" |
255 | __DICT_VERSION_CONVERSATION = bigger_version(bigger_version("2.6.0", __DICT_VERSION_ACCOUNT), __DICT_VERSION_STATUS) | 234 | __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) | 235 | __DICT_VERSION_SCHEDULED_STATUS = max_version("2.7.0", __DICT_VERSION_STATUS) |
257 | __DICT_VERSION_PREFERENCES = "2.8.0" | 236 | __DICT_VERSION_PREFERENCES = "2.8.0" |
258 | __DICT_VERSION_ADMIN_ACCOUNT = bigger_version("4.0.0", __DICT_VERSION_ACCOUNT) | 237 | __DICT_VERSION_ADMIN_ACCOUNT = max_version("4.0.0", __DICT_VERSION_ACCOUNT) |
259 | __DICT_VERSION_FEATURED_TAG = "3.0.0" | 238 | __DICT_VERSION_FEATURED_TAG = "3.0.0" |
260 | __DICT_VERSION_MARKER = "3.0.0" | 239 | __DICT_VERSION_MARKER = "3.0.0" |
261 | __DICT_VERSION_REACTION = "3.1.0" | 240 | __DICT_VERSION_REACTION = "3.1.0" |
262 | __DICT_VERSION_ANNOUNCEMENT = bigger_version("3.1.0", __DICT_VERSION_REACTION) | 241 | __DICT_VERSION_ANNOUNCEMENT = max_version("3.1.0", __DICT_VERSION_REACTION) |
263 | __DICT_VERSION_STATUS_EDIT = "3.5.0" | 242 | __DICT_VERSION_STATUS_EDIT = "3.5.0" |
264 | __DICT_VERSION_FAMILIAR_FOLLOWERS = bigger_version("3.5.0", __DICT_VERSION_ACCOUNT) | 243 | __DICT_VERSION_FAMILIAR_FOLLOWERS = max_version("3.5.0", __DICT_VERSION_ACCOUNT) |
265 | __DICT_VERSION_ADMIN_DOMAIN_BLOCK = "4.0.0" | 244 | __DICT_VERSION_ADMIN_DOMAIN_BLOCK = "4.0.0" |
266 | __DICT_VERSION_ADMIN_MEASURE = "3.5.0" | 245 | __DICT_VERSION_ADMIN_MEASURE = "3.5.0" |
267 | __DICT_VERSION_ADMIN_DIMENSION = "3.5.0" | 246 | __DICT_VERSION_ADMIN_DIMENSION = "3.5.0" |
@@ -3709,8 +3688,8 @@ class Mastodon: | |||
3709 | crypto_ver = cryptography.__version__ | 3688 | crypto_ver = cryptography.__version__ |
3710 | if len(crypto_ver) < 5: | 3689 | if len(crypto_ver) < 5: |
3711 | crypto_ver += ".0" | 3690 | crypto_ver += ".0" |
3712 | if bigger_version(crypto_ver, "2.5.0") == crypto_ver: | 3691 | 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) | 3692 | sapush_key_pub = push_key_pair.public_key().public_bytes(serialization.Encoding.X962, serialization.PublicFormat.UncompressedPoint) |
3714 | else: | 3693 | else: |
3715 | push_key_pub = push_key_pair.public_key().public_numbers().encode_point() | 3694 | push_key_pub = push_key_pair.public_key().public_numbers().encode_point() |
3716 | push_shared_secret = os.urandom(16) | 3695 | push_shared_secret = os.urandom(16) |
diff --git a/tests/test_instance.py b/tests/test_instance.py index 99a3534..afd883d 100644 --- a/tests/test_instance.py +++ b/tests/test_instance.py | |||
@@ -74,13 +74,13 @@ def test_instance_rules(api): | |||
74 | assert isinstance(api.instance_rules(), list) | 74 | assert isinstance(api.instance_rules(), list) |
75 | 75 | ||
76 | def test_version_parsing(api): | 76 | def test_version_parsing(api): |
77 | assert parse_version_string(api._Mastodon__normalize_version_string("4.0.2")) == [4, 0, 2] | 77 | assert parse_version_string(api._Mastodon__normalize_version_string("4.0.2")) == (4, 0, 2) |
78 | assert parse_version_string(api._Mastodon__normalize_version_string("2.1.0rc3")) == [2, 1, 0] | 78 | assert parse_version_string(api._Mastodon__normalize_version_string("2.1.0rc3")) == (2, 1, 0) |
79 | assert parse_version_string(api._Mastodon__normalize_version_string("1.0.7+3.5.5")) == [3, 5, 5] | 79 | assert parse_version_string(api._Mastodon__normalize_version_string("1.0.7+3.5.5")) == (3, 5, 5) |
80 | assert parse_version_string(api._Mastodon__normalize_version_string("1.0.7+3.5.5rc2")) == [3, 5, 5] | 80 | assert parse_version_string(api._Mastodon__normalize_version_string("1.0.7+3.5.5rc2")) == (3, 5, 5) |
81 | assert parse_version_string(api._Mastodon__normalize_version_string("3.5.1+chitter")) == [3, 5, 1] | 81 | assert parse_version_string(api._Mastodon__normalize_version_string("3.5.1+chitter")) == (3, 5, 1) |
82 | assert parse_version_string(api._Mastodon__normalize_version_string("3.5.1+chitter-6.6.6")) == [3, 5, 1] | 82 | assert parse_version_string(api._Mastodon__normalize_version_string("3.5.1+chitter-6.6.6")) == (3, 5, 1) |
83 | assert parse_version_string(api._Mastodon__normalize_version_string("3.5.1rc4+chitter-6.6.6")) == [3, 5, 1] | 83 | assert parse_version_string(api._Mastodon__normalize_version_string("3.5.1rc4+chitter-6.6.6")) == (3, 5, 1) |
84 | assert parse_version_string(api._Mastodon__normalize_version_string("3.5.1+chitter6.6.6")) == [3, 5, 1] | 84 | assert parse_version_string(api._Mastodon__normalize_version_string("3.5.1+chitter6.6.6")) == (3, 5, 1) |
85 | assert parse_version_string(api._Mastodon__normalize_version_string("3.5.0 (compatible; Pleroma 1.2.3)")) == [3, 5, 0] | 85 | assert parse_version_string(api._Mastodon__normalize_version_string("3.5.0 (compatible; Pleroma 1.2.3)")) == (3, 5, 0) |
86 | assert parse_version_string(api._Mastodon__normalize_version_string("3.2.1rc3 (compatible; Akkoma 3.2.4+shinychariot)")) == [3, 2, 1] | 86 | assert parse_version_string(api._Mastodon__normalize_version_string("3.2.1rc3 (compatible; Akkoma 3.2.4+shinychariot)")) == (3, 2, 1) |