1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
import re
class InvalidVersionError(Exception):
pass
class VersionNotFoundError(Exception):
pass
class ApiVersion(object):
versions = {}
@classmethod
def coerce_to_version(cls, version):
try:
return cls.versions[version]
except KeyError:
# Dynamically create a new Release object if version string is not found
if Release.FORMAT.match(version):
return Release(version)
raise VersionNotFoundError
@classmethod
def define_version(cls, version):
cls.versions[version.name] = version
return version
@classmethod
def define_known_versions(cls):
cls.define_version(Unstable())
cls.define_version(Release("2021-10"))
cls.define_version(Release("2022-01"))
cls.define_version(Release("2022-04"))
cls.define_version(Release("2022-07"))
cls.define_version(Release("2022-10"))
cls.define_version(Release("2023-01"))
cls.define_version(Release("2023-04"))
cls.define_version(Release("2023-07"))
cls.define_version(Release("2023-10"))
cls.define_version(Release("2024-01"))
cls.define_version(Release("2024-04"))
cls.define_version(Release("2024-07"))
cls.define_version(Release("2024-10"))
@classmethod
def clear_defined_versions(cls):
cls.versions = {}
@property
def numeric_version(self):
return self._numeric_version
@property
def name(self):
return self._name
def api_path(self, site):
return site + self._path
def __eq__(self, other):
if not isinstance(other, type(self)):
return False
return self.numeric_version == int(other.numeric_version)
class Release(ApiVersion):
FORMAT = re.compile(r"^\d{4}-\d{2}$")
API_PREFIX = "/admin/api"
def __init__(self, version_number):
if not self.FORMAT.match(version_number):
raise InvalidVersionError
self._name = version_number
self._numeric_version = int(version_number.replace("-", ""))
self._path = "%s/%s" % (self.API_PREFIX, version_number)
@property
def stable(self):
return True
class Unstable(ApiVersion):
def __init__(self):
self._name = "unstable"
self._numeric_version = 9000000
self._path = "/admin/api/unstable"
@property
def stable(self):
return False
ApiVersion.define_known_versions()
|