diff options
Diffstat (limited to 'mastodon/utility.py')
-rw-r--r-- | mastodon/utility.py | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/mastodon/utility.py b/mastodon/utility.py new file mode 100644 index 0000000..f393aa8 --- /dev/null +++ b/mastodon/utility.py | |||
@@ -0,0 +1,77 @@ | |||
1 | # utility.py - utility functions, externally usable | ||
2 | |||
3 | import re | ||
4 | from decorator import decorate | ||
5 | from .error import MastodonVersionError | ||
6 | |||
7 | ### | ||
8 | # Version check functions, including decorator and parser | ||
9 | ### | ||
10 | def parse_version_string(version_string): | ||
11 | """Parses a semver version string, stripping off "rc" stuff if present.""" | ||
12 | string_parts = version_string.split(".") | ||
13 | version_parts = ( | ||
14 | int(re.match("([0-9]*)", string_parts[0]).group(0)), | ||
15 | int(re.match("([0-9]*)", string_parts[1]).group(0)), | ||
16 | int(re.match("([0-9]*)", string_parts[2]).group(0)) | ||
17 | ) | ||
18 | return version_parts | ||
19 | |||
20 | def max_version(*version_strings): | ||
21 | """Returns the maximum version of all provided version strings.""" | ||
22 | return max(version_strings, key=parse_version_string) | ||
23 | |||
24 | def api_version(created_ver, last_changed_ver, return_value_ver): | ||
25 | """Version check decorator. Currently only checks Bigger Than.""" | ||
26 | def api_min_version_decorator(function): | ||
27 | def wrapper(function, self, *args, **kwargs): | ||
28 | if not self.version_check_mode == "none": | ||
29 | if self.version_check_mode == "created": | ||
30 | version = created_ver | ||
31 | else: | ||
32 | version = max_version(last_changed_ver, return_value_ver) | ||
33 | major, minor, patch = parse_version_string(version) | ||
34 | if major > self.mastodon_major: | ||
35 | raise MastodonVersionError("Version check failed (Need version " + version + ")") | ||
36 | elif major == self.mastodon_major and minor > self.mastodon_minor: | ||
37 | raise MastodonVersionError("Version check failed (Need version " + version + ")") | ||
38 | elif major == self.mastodon_major and minor == self.mastodon_minor and patch > self.mastodon_patch: | ||
39 | raise MastodonVersionError("Version check failed (Need version " + version + ", patch is " + str(self.mastodon_patch) + ")") | ||
40 | return function(self, *args, **kwargs) | ||
41 | function.__doc__ = function.__doc__ + "\n\n *Added: Mastodon v" + \ | ||
42 | created_ver + ", last changed: Mastodon v" + last_changed_ver + "*" | ||
43 | return decorate(function, wrapper) | ||
44 | return api_min_version_decorator | ||
45 | |||
46 | ### | ||
47 | # Dict helper class. | ||
48 | # Defined at top level so it can be pickled. | ||
49 | ### | ||
50 | class AttribAccessDict(dict): | ||
51 | def __getattr__(self, attr): | ||
52 | if attr in self: | ||
53 | return self[attr] | ||
54 | else: | ||
55 | raise AttributeError("Attribute not found: " + str(attr)) | ||
56 | |||
57 | def __setattr__(self, attr, val): | ||
58 | if attr in self: | ||
59 | raise AttributeError("Attribute-style access is read only") | ||
60 | super(AttribAccessDict, self).__setattr__(attr, val) | ||
61 | |||
62 | |||
63 | ### | ||
64 | # List helper class. | ||
65 | # Defined at top level so it can be pickled. | ||
66 | ### | ||
67 | class AttribAccessList(list): | ||
68 | def __getattr__(self, attr): | ||
69 | if attr in self: | ||
70 | return self[attr] | ||
71 | else: | ||
72 | raise AttributeError("Attribute not found: " + str(attr)) | ||
73 | |||
74 | def __setattr__(self, attr, val): | ||
75 | if attr in self: | ||
76 | raise AttributeError("Attribute-style access is read only") | ||
77 | super(AttribAccessList, self).__setattr__(attr, val) \ No newline at end of file | ||