aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'mastodon/utility.py')
-rw-r--r--mastodon/utility.py70
1 files changed, 68 insertions, 2 deletions
diff --git a/mastodon/utility.py b/mastodon/utility.py
index f393aa8..53980b6 100644
--- a/mastodon/utility.py
+++ b/mastodon/utility.py
@@ -2,7 +2,11 @@
2 2
3import re 3import re
4from decorator import decorate 4from decorator import decorate
5from .error import MastodonVersionError 5from .error import MastodonVersionError, MastodonAPIError
6import dateutil
7import datetime
8
9# Module level:
6 10
7### 11###
8# Version check functions, including decorator and parser 12# Version check functions, including decorator and parser
@@ -74,4 +78,66 @@ class AttribAccessList(list):
74 def __setattr__(self, attr, val): 78 def __setattr__(self, attr, val):
75 if attr in self: 79 if attr in self:
76 raise AttributeError("Attribute-style access is read only") 80 raise AttributeError("Attribute-style access is read only")
77 super(AttribAccessList, self).__setattr__(attr, val) \ No newline at end of file 81 super(AttribAccessList, self).__setattr__(attr, val)
82
83
84# Class level:
85class Mastodon():
86 def set_language(self, lang):
87 """
88 Set the locale Mastodon will use to generate responses. Valid parameters are all ISO 639-1 (two letter) or, for languages that do
89 not have one, 639-3 (three letter) language codes. This affects some error messages (those related to validation) and trends.
90 """
91 self.lang = lang
92
93 def retrieve_mastodon_version(self):
94 """
95 Determine installed Mastodon version and set major, minor and patch (not including RC info) accordingly.
96
97 Returns the version string, possibly including rc info.
98 """
99 try:
100 version_str = self.__normalize_version_string(self.__instance()["version"])
101 self.version_check_worked = True
102 except:
103 # instance() was added in 1.1.0, so our best guess is 1.0.0.
104 version_str = "1.0.0"
105 self.version_check_worked = False
106
107 self.mastodon_major, self.mastodon_minor, self.mastodon_patch = parse_version_string(version_str)
108 return version_str
109
110 def verify_minimum_version(self, version_str, cached=False):
111 """
112 Update version info from server and verify that at least the specified version is present.
113
114 If you specify "cached", the version info update part is skipped.
115
116 Returns True if version requirement is satisfied, False if not.
117 """
118 if not cached:
119 self.retrieve_mastodon_version()
120 major, minor, patch = parse_version_string(version_str)
121 if major > self.mastodon_major:
122 return False
123 elif major == self.mastodon_major and minor > self.mastodon_minor:
124 return False
125 elif major == self.mastodon_major and minor == self.mastodon_minor and patch > self.mastodon_patch:
126 return False
127 return True
128
129 def get_approx_server_time(self):
130 """
131 Retrieve the approximate server time
132
133 We parse this from the hopefully present "Date" header, but make no effort to compensate for latency.
134 """
135 response = self.__api_request("HEAD", "/", return_response_object=True)
136 if 'Date' in response.headers:
137 server_time_datetime = dateutil.parser.parse(response.headers['Date'])
138
139 # Make sure we're in local time
140 epoch_time = self.__datetime_to_epoch(server_time_datetime)
141 return datetime.datetime.fromtimestamp(epoch_time)
142 else:
143 raise MastodonAPIError("No server time in response.")
Powered by cgit v1.2.3 (git 2.41.0)