aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenz Diener <[email protected]>2017-12-11 14:00:14 +0100
committerLorenz Diener <[email protected]>2017-12-11 14:00:14 +0100
commit9acfb0d3d80d49e6dd6d597ef355cb84bfae698e (patch)
treeca65cca988907abb0e8a062b110cc695b29788a2 /mastodon
parentd87ff6d490023690793a24e58a407b61df07aca5 (diff)
downloadmastodon.py-9acfb0d3d80d49e6dd6d597ef355cb84bfae698e.tar.gz
Versioning fixes
Diffstat (limited to 'mastodon')
-rw-r--r--mastodon/Mastodon.py55
1 files changed, 45 insertions, 10 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index da21e8e..71c882c 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -26,12 +26,23 @@ except ImportError:
26 from urlparse import urlparse 26 from urlparse import urlparse
27 27
28""" 28"""
29Version check decorator 29Version check functions, including decorator and parser
30""" 30"""
31def parse_version_string(version_string):
32 """Parses a semver version string, stripping off "rc" stuff if present."""
33 string_parts = version_string.split(".")
34 version_parts = [
35 int(re.match("([0-9]*)", string_parts[0]).group(0)),
36 int(re.match("([0-9]*)", string_parts[1]).group(0)),
37 int(re.match("([0-9]*)", string_parts[2]).group(0))
38 ]
39 return version_parts
40
31def api_version(version): 41def api_version(version):
42 """Version check decorator. Currently only checks Bigger Than."""
32 def api_min_version_decorator(function): 43 def api_min_version_decorator(function):
33 def wrapper(self, *args, **kwargs): 44 def wrapper(function, self, *args, **kwargs):
34 major, minor, patch = list(map(int, version.split("."))) 45 major, minor, patch = parse_version_string(version)
35 if major > self.mastodon_major: 46 if major > self.mastodon_major:
36 raise MastodonVersionError("Specified version does not support this API endpoint (Available from " + version + ")") 47 raise MastodonVersionError("Specified version does not support this API endpoint (Available from " + version + ")")
37 elif minor > self.mastodon_minor: 48 elif minor > self.mastodon_minor:
@@ -42,7 +53,7 @@ def api_version(version):
42 function.__doc__ = function.__doc__ + "\n\n *Minumum Mastodon version: " + version + "*" 53 function.__doc__ = function.__doc__ + "\n\n *Minumum Mastodon version: " + version + "*"
43 return decorate(function, wrapper) 54 return decorate(function, wrapper)
44 return api_min_version_decorator 55 return api_min_version_decorator
45 56
46class Mastodon: 57class Mastodon:
47 """ 58 """
48 Super basic but thorough and easy to use Mastodon 59 Super basic but thorough and easy to use Mastodon
@@ -105,7 +116,7 @@ class Mastodon:
105 def __init__(self, client_id, client_secret=None, access_token=None, 116 def __init__(self, client_id, client_secret=None, access_token=None,
106 api_base_url=__DEFAULT_BASE_URL, debug_requests=False, 117 api_base_url=__DEFAULT_BASE_URL, debug_requests=False,
107 ratelimit_method="wait", ratelimit_pacefactor=1.1, 118 ratelimit_method="wait", ratelimit_pacefactor=1.1,
108 request_timeout=__DEFAULT_TIMEOUT, version="2.0.0"): 119 request_timeout=__DEFAULT_TIMEOUT, mastodon_version="2.0.0"):
109 """ 120 """
110 Create a new API wrapper instance based on the given client_secret and client_id. If you 121 Create a new API wrapper instance based on the given client_secret and client_id. If you
111 give a client_id and it is not a file, you must also give a secret. 122 give a client_id and it is not a file, you must also give a secret.
@@ -127,9 +138,10 @@ class Mastodon:
127 By default, a timeout of 300 seconds is used for all requests. If you wish to change this, 138 By default, a timeout of 300 seconds is used for all requests. If you wish to change this,
128 pass the desired timeout (in seconds) as request_timeout. 139 pass the desired timeout (in seconds) as request_timeout.
129 140
130 The version parameter can be used to specify the version of Mastodon that Mastodon.py will 141 The mastodon_version parameter can be used to specify the version of Mastodon that Mastodon.py will
131 expect to be installed on the server. The function will throw an error if an unparseable 142 expect to be installed on the server. The function will throw an error if an unparseable
132 Version is specified. By default, Mastodon.py assumes the latest supported version. 143 Version is specified or if the server mastodon version is too old. If no version is specified,
144 Mastodon.py will set mastodon_version to the detected version.
133 """ 145 """
134 self.api_base_url = Mastodon.__protocolize(api_base_url) 146 self.api_base_url = Mastodon.__protocolize(api_base_url)
135 self.client_id = client_id 147 self.client_id = client_id
@@ -149,7 +161,7 @@ class Mastodon:
149 self.request_timeout = request_timeout 161 self.request_timeout = request_timeout
150 162
151 try: 163 try:
152 self.mastodon_major, self.mastodon_minor, self.mastodon_patch = list(map(int, version.split("."))) 164 self.mastodon_major, self.mastodon_minor, self.mastodon_patch = parse_version_string(mastodon_version)
153 except: 165 except:
154 raise MastodonVersionError("Bad version specified") 166 raise MastodonVersionError("Bad version specified")
155 167
@@ -167,7 +179,24 @@ class Mastodon:
167 if self.access_token is not None and os.path.isfile(self.access_token): 179 if self.access_token is not None and os.path.isfile(self.access_token):
168 with open(self.access_token, 'r') as token_file: 180 with open(self.access_token, 'r') as token_file:
169 self.access_token = token_file.readline().rstrip() 181 self.access_token = token_file.readline().rstrip()
170 182
183 def retrieve_mastodon_version(self):
184 """
185 Determine installed mastodon version and set major, minor and patch (not including RC info) accordingly.
186
187
188 Returns the version string, possibly including rc info.
189 """
190 try:
191 version_str = self.__instance()["version"]
192 except:
193 # instance() was added in 1.1.0, so our best guess is 1.0.0.
194 version_str = "1.0.0"
195
196 self.mastodon_major, self.mastodon_minor, self.mastodon_patch = parse_version_string(version)
197 return version_str
198
199
171 def auth_request_url(self, client_id=None, redirect_uris="urn:ietf:wg:oauth:2.0:oob", 200 def auth_request_url(self, client_id=None, redirect_uris="urn:ietf:wg:oauth:2.0:oob",
172 scopes=['read', 'write', 'follow']): 201 scopes=['read', 'write', 'follow']):
173 """Returns the url that a client needs to request the grant from the server. 202 """Returns the url that a client needs to request the grant from the server.
@@ -259,6 +288,12 @@ class Mastodon:
259 288
260 Returns an instance dict. 289 Returns an instance dict.
261 """ 290 """
291 return self.__instance('GET', '/api/v1/instance/')
292
293 def __instance(self):
294 """
295 Internal, non-version-checking helper that does the same as instance()
296 """
262 return self.__api_request('GET', '/api/v1/instance/') 297 return self.__api_request('GET', '/api/v1/instance/')
263 298
264 ### 299 ###
@@ -1475,7 +1510,7 @@ class Mastodon:
1475 """Internal helper for oauth code""" 1510 """Internal helper for oauth code"""
1476 self._refresh_token = value 1511 self._refresh_token = value
1477 return 1512 return
1478 1513
1479 @staticmethod 1514 @staticmethod
1480 def __protocolize(base_url): 1515 def __protocolize(base_url):
1481 """Internal add-protocol-to-url helper""" 1516 """Internal add-protocol-to-url helper"""
Powered by cgit v1.2.3 (git 2.41.0)