diff options
author | Lorenz Diener <[email protected]> | 2017-12-14 13:54:31 +0100 |
---|---|---|
committer | Lorenz Diener <[email protected]> | 2017-12-14 13:54:31 +0100 |
commit | 301d719b837809fb1a62f72f2f2545c45263dbe5 (patch) | |
tree | be3e2df9c12bc5a620e99d9af8f1129a3ad9e1fe /mastodon | |
parent | f784eae8e64902a4b784fe30642a356c998a0b81 (diff) | |
download | mastodon.py-301d719b837809fb1a62f72f2f2545c45263dbe5.tar.gz |
Fix some problems with attribute access
Diffstat (limited to 'mastodon')
-rw-r--r-- | mastodon/Mastodon.py | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 2d51561..c0af05d 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -25,9 +25,9 @@ try: | |||
25 | except ImportError: | 25 | except ImportError: |
26 | from urlparse import urlparse | 26 | from urlparse import urlparse |
27 | 27 | ||
28 | """ | 28 | ### |
29 | Version check functions, including decorator and parser | 29 | # Version check functions, including decorator and parser |
30 | """ | 30 | ### |
31 | def parse_version_string(version_string): | 31 | def parse_version_string(version_string): |
32 | """Parses a semver version string, stripping off "rc" stuff if present.""" | 32 | """Parses a semver version string, stripping off "rc" stuff if present.""" |
33 | string_parts = version_string.split(".") | 33 | string_parts = version_string.split(".") |
@@ -58,7 +58,28 @@ def api_version(created_ver, last_changed_ver): | |||
58 | function.__doc__ = function.__doc__ + "\n\n *Added: Mastodon v" + created_ver + ", last changed: Mastodon v" + last_changed_ver + "*" | 58 | function.__doc__ = function.__doc__ + "\n\n *Added: Mastodon v" + created_ver + ", last changed: Mastodon v" + last_changed_ver + "*" |
59 | return decorate(function, wrapper) | 59 | return decorate(function, wrapper) |
60 | return api_min_version_decorator | 60 | return api_min_version_decorator |
61 | 61 | ||
62 | |||
63 | ### | ||
64 | # Dict helper class. | ||
65 | # Defined at top level so it can be pickled. | ||
66 | ### | ||
67 | class AttribAccessDict(dict): | ||
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().__setattr__(attr, val) | ||
78 | |||
79 | ### | ||
80 | # The actual Mastodon class | ||
81 | ### | ||
82 | |||
62 | class Mastodon: | 83 | class Mastodon: |
63 | """ | 84 | """ |
64 | Super basic but thorough and easy to use Mastodon | 85 | Super basic but thorough and easy to use Mastodon |
@@ -1398,17 +1419,6 @@ class Mastodon: | |||
1398 | Makes it possible to use attribute notation to access a dicts | 1419 | Makes it possible to use attribute notation to access a dicts |
1399 | elements, while still allowing the dict to act as a dict. | 1420 | elements, while still allowing the dict to act as a dict. |
1400 | """ | 1421 | """ |
1401 | class AttribAccessDict(dict): | ||
1402 | def __getattr__(self, attr): | ||
1403 | if attr in self: | ||
1404 | return self[attr] | ||
1405 | else: | ||
1406 | raise AttributeError() | ||
1407 | |||
1408 | def __setattr__(self, attr, val): | ||
1409 | if attr in self: | ||
1410 | raise AttributeError("Attribute-style access is read only") | ||
1411 | super().__setattr__(attr, val) | ||
1412 | if isinstance(json_object, dict): | 1422 | if isinstance(json_object, dict): |
1413 | return AttribAccessDict(json_object) | 1423 | return AttribAccessDict(json_object) |
1414 | return json_object | 1424 | return json_object |