From 3caa27a113945bc1662f4f63fa0387b9aece6390 Mon Sep 17 00:00:00 2001 From: halcy Date: Thu, 24 Nov 2022 01:37:47 +0200 Subject: Robustify version parsing --- CHANGELOG.rst | 19 ++++++++++--------- mastodon/Mastodon.py | 13 ++++++++++++- tests/test_instance.py | 13 +++++++++++++ 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index bc84eea..3079108 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,17 +4,18 @@ version number. Breaking changes will be indicated by a change in the minor v1.7.0 ------ -* Clean code up a bit (thanks eumiro) -* Fix some Pleroma related issues (thanks aveao, taraletti, adbenitez) -* Add post editing (`status_update`, `status_source`, `status_history`) -* Add missing streaming events -* Add missing parameters on directory endpoint (thanks heharkon) +* Cleaned code up a bit (thanks eumiro) +* Fixed some Pleroma related issues (thanks aveao, taraletti, adbenitez) +* Added post editing (`status_update`, `status_source`, `status_history`) +* Added missing streaming events +* 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 * Fixed search ignoring `exclude_unreviewed` (Thanks acidghost) -* Add support for using pathlib paths when loading media files (Thanks reagle) -* Remove blocklist with long dead instances -* Add `types` parameter to notifications. -* Document additional notification types +* Added support for using pathlib paths when loading media files (Thanks reagle) +* Removed blocklist with long dead instances +* Added `types` parameter to notifications. +* 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. v1.6.3 diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 896d87d..d98289d 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -470,6 +470,17 @@ class Mastodon: if ratelimit_method not in ["throw", "wait", "pace"]: 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): """ 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. """ try: - version_str = self.__instance()["version"].split('+')[0] + version_str = self.__normalize_version_string(self.__instance()["version"]) self.version_check_worked = True except: # instance() was added in 1.1.0, so our best guess is 1.0.0. diff --git a/tests/test_instance.py b/tests/test_instance.py index 1fbd692..e25a686 100644 --- a/tests/test_instance.py +++ b/tests/test_instance.py @@ -1,6 +1,7 @@ import pytest from mastodon.Mastodon import MastodonVersionError +from mastodon.Mastodon import parse_version_string import datetime import os import pickle @@ -75,3 +76,15 @@ def test_directory(api): @pytest.mark.vcr() def test_instance_rules(api): 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] -- cgit v1.2.3