Robustify version parsing

This commit is contained in:
halcy 2022-11-24 01:37:47 +02:00
parent b7266db01b
commit 3caa27a113
3 changed files with 35 additions and 10 deletions

View File

@ -4,17 +4,18 @@ version number. Breaking changes will be indicated by a change in the minor
v1.7.0 v1.7.0
------ ------
* Clean code up a bit (thanks eumiro) * Cleaned code up a bit (thanks eumiro)
* Fix some Pleroma related issues (thanks aveao, taraletti, adbenitez) * Fixed some Pleroma related issues (thanks aveao, taraletti, adbenitez)
* Add post editing (`status_update`, `status_source`, `status_history`) * Added post editing (`status_update`, `status_source`, `status_history`)
* Add missing streaming events * Added missing streaming events
* Add missing parameters on directory endpoint (thanks heharkon) * Added missing parameters on directory endpoint (thanks heharkon)
* This isn't somehing I changed but thank you a / triggerofsol for answering Many questions I had about specifics of what the API does that are not documented * This isn't somehing I changed but thank you a / triggerofsol for answering Many questions I had about specifics of what the API does that are not documented
* Fixed search ignoring `exclude_unreviewed` (Thanks acidghost) * Fixed search ignoring `exclude_unreviewed` (Thanks acidghost)
* Add support for using pathlib paths when loading media files (Thanks reagle) * Added support for using pathlib paths when loading media files (Thanks reagle)
* Remove blocklist with long dead instances * Removed blocklist with long dead instances
* Add `types` parameter to notifications. * Added `types` parameter to notifications.
* Document additional notification types * Documented additional notification types
* Made version parsing more robust against varions things that Mastodon-compatible APIs might throw at it.
* TECHNICALLY BREAKING CHANGE, but I would be quite surprised if this actually breaks anyone: Date parsing will now, when the date string is empty, return Jan. 1st, 1970 instead. This is to work around what I assume is a bug in Pleroma. * TECHNICALLY BREAKING CHANGE, but I would be quite surprised if this actually breaks anyone: Date parsing will now, when the date string is empty, return Jan. 1st, 1970 instead. This is to work around what I assume is a bug in Pleroma.
v1.6.3 v1.6.3

View File

@ -470,6 +470,17 @@ class Mastodon:
if ratelimit_method not in ["throw", "wait", "pace"]: if ratelimit_method not in ["throw", "wait", "pace"]:
raise MastodonIllegalArgumentError("Invalid ratelimit method.") raise MastodonIllegalArgumentError("Invalid ratelimit method.")
def __normalize_version_string(self, version_string):
# Split off everything after the first space, to take care of Pleromalikes so that the parser doesn't get confused in case those have a + somewhere in their version
version_string = version_string.split(" ")[0]
try:
# Attempt to split at + and check if the part after parses as a version string, to account for hometown
parse_version_string(version_string.split("+")[1])
return version_string.split("+")[1]
except:
# If this fails, assume that if there is a +, what is before that is the masto version (or that there is no +)
return version_string.split("+")[0]
def retrieve_mastodon_version(self): def retrieve_mastodon_version(self):
""" """
Determine installed Mastodon version and set major, minor and patch (not including RC info) accordingly. Determine installed Mastodon version and set major, minor and patch (not including RC info) accordingly.
@ -477,7 +488,7 @@ class Mastodon:
Returns the version string, possibly including rc info. Returns the version string, possibly including rc info.
""" """
try: try:
version_str = self.__instance()["version"].split('+')[0] version_str = self.__normalize_version_string(self.__instance()["version"])
self.version_check_worked = True self.version_check_worked = True
except: except:
# instance() was added in 1.1.0, so our best guess is 1.0.0. # instance() was added in 1.1.0, so our best guess is 1.0.0.

View File

@ -1,6 +1,7 @@
import pytest import pytest
from mastodon.Mastodon import MastodonVersionError from mastodon.Mastodon import MastodonVersionError
from mastodon.Mastodon import parse_version_string
import datetime import datetime
import os import os
import pickle import pickle
@ -75,3 +76,15 @@ def test_directory(api):
@pytest.mark.vcr() @pytest.mark.vcr()
def test_instance_rules(api): def test_instance_rules(api):
assert isinstance(api.instance_rules(), list) assert isinstance(api.instance_rules(), list)
def test_version_parsing(api):
assert parse_version_string(api._Mastodon__normalize_version_string("4.0.2")) == [4, 0, 2]
assert parse_version_string(api._Mastodon__normalize_version_string("2.1.0rc3")) == [2, 1, 0]
assert parse_version_string(api._Mastodon__normalize_version_string("1.0.7+3.5.5")) == [3, 5, 5]
assert parse_version_string(api._Mastodon__normalize_version_string("1.0.7+3.5.5rc2")) == [3, 5, 5]
assert parse_version_string(api._Mastodon__normalize_version_string("3.5.1+chitter")) == [3, 5, 1]
assert parse_version_string(api._Mastodon__normalize_version_string("3.5.1+chitter-6.6.6")) == [3, 5, 1]
assert parse_version_string(api._Mastodon__normalize_version_string("3.5.1rc4+chitter-6.6.6")) == [3, 5, 1]
assert parse_version_string(api._Mastodon__normalize_version_string("3.5.1+chitter6.6.6")) == [3, 5, 1]
assert parse_version_string(api._Mastodon__normalize_version_string("3.5.0 (compatible; Pleroma 1.2.3)")) == [3, 5, 0]
assert parse_version_string(api._Mastodon__normalize_version_string("3.2.1rc3 (compatible; Akkoma 3.2.4+shinychariot)")) == [3, 2, 1]