diff options
author | Lorenz Diener <[email protected]> | 2017-12-11 14:34:51 +0100 |
---|---|---|
committer | Lorenz Diener <[email protected]> | 2017-12-11 14:34:51 +0100 |
commit | fad615b8d6abb5b6c7a7576483650bd9bf341897 (patch) | |
tree | 78d7a144b7e5b840ed87aafeddc5d20cb482b4cb | |
parent | a93f051b5e8fe1968a2c306fe8ad711ad77e8e88 (diff) | |
download | mastodon.py-fad615b8d6abb5b6c7a7576483650bd9bf341897.tar.gz |
Fix versioning functionality more, explicit version check
-rw-r--r-- | mastodon/Mastodon.py | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 5b302e6..b52f13c 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -116,7 +116,7 @@ class Mastodon: | |||
116 | def __init__(self, client_id, client_secret=None, access_token=None, | 116 | def __init__(self, client_id, client_secret=None, access_token=None, |
117 | api_base_url=__DEFAULT_BASE_URL, debug_requests=False, | 117 | api_base_url=__DEFAULT_BASE_URL, debug_requests=False, |
118 | ratelimit_method="wait", ratelimit_pacefactor=1.1, | 118 | ratelimit_method="wait", ratelimit_pacefactor=1.1, |
119 | request_timeout=__DEFAULT_TIMEOUT, mastodon_version="2.0.0"): | 119 | request_timeout=__DEFAULT_TIMEOUT, mastodon_version=None): |
120 | """ | 120 | """ |
121 | 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 |
122 | 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. |
@@ -140,8 +140,8 @@ class Mastodon: | |||
140 | 140 | ||
141 | The mastodon_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 |
142 | 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 |
143 | Version is specified or if the server mastodon version is too old. If no version is specified, | 143 | Version is specified. If no version is specified, Mastodon.py will set mastodon_version to the |
144 | Mastodon.py will set mastodon_version to the detected version. | 144 | detected version. |
145 | """ | 145 | """ |
146 | self.api_base_url = Mastodon.__protocolize(api_base_url) | 146 | self.api_base_url = Mastodon.__protocolize(api_base_url) |
147 | self.client_id = client_id | 147 | self.client_id = client_id |
@@ -160,14 +160,20 @@ class Mastodon: | |||
160 | 160 | ||
161 | self.request_timeout = request_timeout | 161 | self.request_timeout = request_timeout |
162 | 162 | ||
163 | try: | 163 | # Versioning |
164 | self.mastodon_major, self.mastodon_minor, self.mastodon_patch = parse_version_string(mastodon_version) | 164 | if mastodon_version == None: |
165 | except: | 165 | self.retrieve_mastodon_version() |
166 | raise MastodonVersionError("Bad version specified") | 166 | else: |
167 | 167 | try: | |
168 | self.mastodon_major, self.mastodon_minor, self.mastodon_patch = parse_version_string(mastodon_version) | ||
169 | except: | ||
170 | raise MastodonVersionError("Bad version specified") | ||
171 | |||
172 | # Ratelimiting parameter check | ||
168 | if ratelimit_method not in ["throw", "wait", "pace"]: | 173 | if ratelimit_method not in ["throw", "wait", "pace"]: |
169 | raise MastodonIllegalArgumentError("Invalid ratelimit method.") | 174 | raise MastodonIllegalArgumentError("Invalid ratelimit method.") |
170 | 175 | ||
176 | # Token loading | ||
171 | if os.path.isfile(self.client_id): | 177 | if os.path.isfile(self.client_id): |
172 | with open(self.client_id, 'r') as secret_file: | 178 | with open(self.client_id, 'r') as secret_file: |
173 | self.client_id = secret_file.readline().rstrip() | 179 | self.client_id = secret_file.readline().rstrip() |
@@ -184,7 +190,6 @@ class Mastodon: | |||
184 | """ | 190 | """ |
185 | Determine installed mastodon version and set major, minor and patch (not including RC info) accordingly. | 191 | Determine installed mastodon version and set major, minor and patch (not including RC info) accordingly. |
186 | 192 | ||
187 | |||
188 | Returns the version string, possibly including rc info. | 193 | Returns the version string, possibly including rc info. |
189 | """ | 194 | """ |
190 | try: | 195 | try: |
@@ -193,10 +198,25 @@ class Mastodon: | |||
193 | # instance() was added in 1.1.0, so our best guess is 1.0.0. | 198 | # instance() was added in 1.1.0, so our best guess is 1.0.0. |
194 | version_str = "1.0.0" | 199 | version_str = "1.0.0" |
195 | 200 | ||
196 | self.mastodon_major, self.mastodon_minor, self.mastodon_patch = parse_version_string(version) | 201 | self.mastodon_major, self.mastodon_minor, self.mastodon_patch = parse_version_string(version_str) |
197 | return version_str | 202 | return version_str |
198 | 203 | ||
204 | def verify_minimum_version(self, version_str): | ||
205 | """ | ||
206 | Update version info from server and verify that at least the specified version is present. | ||
199 | 207 | ||
208 | Returns True if version requirement is satisfied, False if not. | ||
209 | """ | ||
210 | self.retrieve_mastodon_version() | ||
211 | major, minor, patch = parse_version_string(version_str) | ||
212 | if major > self.mastodon_major: | ||
213 | return False | ||
214 | elif major == self.mastodon_major and minor > self.mastodon_minor: | ||
215 | return False | ||
216 | elif major == self.mastodon_major and minor == self.mastodon_minor and patch > self.mastodon_patch: | ||
217 | return False | ||
218 | return True | ||
219 | |||
200 | def auth_request_url(self, client_id=None, redirect_uris="urn:ietf:wg:oauth:2.0:oob", | 220 | def auth_request_url(self, client_id=None, redirect_uris="urn:ietf:wg:oauth:2.0:oob", |
201 | scopes=['read', 'write', 'follow']): | 221 | scopes=['read', 'write', 'follow']): |
202 | """Returns the url that a client needs to request the grant from the server. | 222 | """Returns the url that a client needs to request the grant from the server. |