2022-11-30 21:09:09 +00:00
|
|
|
|
|
|
|
|
|
# versions.py - versioning of return values
|
|
|
|
|
|
2023-06-19 21:35:03 +00:00
|
|
|
|
import re
|
|
|
|
|
from decorator import decorate
|
|
|
|
|
from mastodon.errors import MastodonVersionError
|
2022-11-30 21:09:09 +00:00
|
|
|
|
|
2023-06-19 21:35:03 +00:00
|
|
|
|
###
|
|
|
|
|
# Version check functions, including decorator and parser
|
|
|
|
|
###
|
|
|
|
|
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)), # type: ignore
|
|
|
|
|
int(re.match("([0-9]*)", string_parts[1]).group(0)), # type: ignore
|
|
|
|
|
int(re.match("([0-9]*)", string_parts[2]).group(0)) # type: ignore
|
|
|
|
|
)
|
|
|
|
|
return version_parts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def max_version(*version_strings):
|
|
|
|
|
"""Returns the maximum version of all provided version strings."""
|
|
|
|
|
return max(version_strings, key=parse_version_string)
|
|
|
|
|
|
|
|
|
|
|
2025-02-15 12:21:56 +00:00
|
|
|
|
def api_version(created_ver, last_changed_ver):
|
2023-06-19 21:35:03 +00:00
|
|
|
|
"""Version check decorator. Currently only checks Bigger Than."""
|
|
|
|
|
def api_min_version_decorator(function):
|
2025-02-15 12:21:56 +00:00
|
|
|
|
return_value_ver = None
|
|
|
|
|
return_value_type = function.__annotations__.get("return", None)
|
|
|
|
|
if return_value_type is not None:
|
|
|
|
|
return_value_ver = getattr(return_value_type, "_version", None)
|
2023-06-19 21:35:03 +00:00
|
|
|
|
def wrapper(function, self, *args, **kwargs):
|
|
|
|
|
if not self.version_check_mode == "none":
|
|
|
|
|
if self.version_check_mode == "created":
|
|
|
|
|
version = created_ver
|
|
|
|
|
else:
|
2025-02-15 12:21:56 +00:00
|
|
|
|
if return_value_ver is not None:
|
|
|
|
|
version = max_version(last_changed_ver, return_value_ver)
|
|
|
|
|
else:
|
|
|
|
|
version = last_changed_ver
|
2023-06-19 21:35:03 +00:00
|
|
|
|
major, minor, patch = parse_version_string(version)
|
|
|
|
|
if major > self.mastodon_major:
|
2025-02-14 00:10:42 +00:00
|
|
|
|
raise MastodonVersionError(f"Version check failed (Need Mastodon instance version {version} to call this endpoint)")
|
2023-06-19 21:35:03 +00:00
|
|
|
|
elif major == self.mastodon_major and minor > self.mastodon_minor:
|
2025-02-14 20:55:20 +00:00
|
|
|
|
raise MastodonVersionError(f"Version check failed (Need Mastodon instance version {version} to call this endpoint)")
|
2023-06-19 21:35:03 +00:00
|
|
|
|
elif major == self.mastodon_major and minor == self.mastodon_minor and patch > self.mastodon_patch:
|
2025-02-14 00:10:42 +00:00
|
|
|
|
raise MastodonVersionError(f"Version check failed (Need Mastodon instance version {version} to call this endpoint). Patch is {self.mastodon_patch}.")
|
2023-06-19 21:35:03 +00:00
|
|
|
|
return function(self, *args, **kwargs)
|
2025-02-14 00:10:42 +00:00
|
|
|
|
if function.__doc__:
|
2025-02-15 12:21:56 +00:00
|
|
|
|
if return_value_ver is not None:
|
|
|
|
|
function.__doc__ += f"\n\n *Added: Mastodon v{created_ver}, last changed: Mastodon v{last_changed_ver} (parameters), Mastodon v{return_value_ver} (return value)*"
|
|
|
|
|
else:
|
|
|
|
|
function.__doc__ += f"\n\n *Added: Mastodon v{created_ver}, last changed: Mastodon v{last_changed_ver}*"
|
2023-06-19 21:35:03 +00:00
|
|
|
|
return decorate(function, wrapper)
|
|
|
|
|
return api_min_version_decorator
|