diff options
author | Mike Crute <mike@crute.us> | 2017-10-30 02:33:48 +0000 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2017-10-30 02:33:48 +0000 |
commit | d93429bf62f076a76876014ac35bbc9355fd5b30 (patch) | |
tree | cbcca1fb2481b0b3f1c27909642c719b0cba4178 | |
parent | 71677cc962218a0001ef9d9b4404179995cc21ef (diff) | |
download | pydora-d93429bf62f076a76876014ac35bbc9355fd5b30.tar.bz2 pydora-d93429bf62f076a76876014ac35bbc9355fd5b30.tar.xz pydora-d93429bf62f076a76876014ac35bbc9355fd5b30.zip |
Refactor SyntheticField interface
The SyntheticField already has an instance of itself when the formatter
runs so no longer passes attributes from the value into the field.
-rw-r--r-- | pandora/models/__init__.py | 8 | ||||
-rw-r--r-- | pandora/models/pandora.py | 7 |
2 files changed, 8 insertions, 7 deletions
diff --git a/pandora/models/__init__.py b/pandora/models/__init__.py index 141a64b..7a60889 100644 --- a/pandora/models/__init__.py +++ b/pandora/models/__init__.py | |||
@@ -39,8 +39,7 @@ class SyntheticField(namedtuple("SyntheticField", ["field"])): | |||
39 | payload. | 39 | payload. |
40 | """ | 40 | """ |
41 | 41 | ||
42 | @staticmethod | 42 | def formatter(self, api_client, data, newval): # pragma: no cover |
43 | def formatter(api_client, field, data): # pragma: no cover | ||
44 | """Format Value for Model | 43 | """Format Value for Model |
45 | 44 | ||
46 | The return value of this method is used as a value for the field in the | 45 | The return value of this method is used as a value for the field in the |
@@ -51,6 +50,9 @@ class SyntheticField(namedtuple("SyntheticField", ["field"])): | |||
51 | data | 50 | data |
52 | complete JSON data blob for the parent model of which this field is | 51 | complete JSON data blob for the parent model of which this field is |
53 | a member | 52 | a member |
53 | newval | ||
54 | the value of this field as retrieved from the JSON data after | ||
55 | having resolved default value logic | ||
54 | """ | 56 | """ |
55 | raise NotImplementedError | 57 | raise NotImplementedError |
56 | 58 | ||
@@ -119,7 +121,7 @@ class PandoraModel(with_metaclass(ModelMetaClass, object)): | |||
119 | newval = data.get(value.field, default) | 121 | newval = data.get(value.field, default) |
120 | 122 | ||
121 | if isinstance(value, SyntheticField): | 123 | if isinstance(value, SyntheticField): |
122 | newval = value.formatter(api_client, value.field, data, newval) | 124 | newval = value.formatter(api_client, data, newval) |
123 | setattr(instance, key, newval) | 125 | setattr(instance, key, newval) |
124 | continue | 126 | continue |
125 | 127 | ||
diff --git a/pandora/models/pandora.py b/pandora/models/pandora.py index 71a1d51..cd1e588 100644 --- a/pandora/models/pandora.py +++ b/pandora/models/pandora.py | |||
@@ -54,8 +54,7 @@ class StationList(PandoraListModel): | |||
54 | 54 | ||
55 | class AudioField(SyntheticField): | 55 | class AudioField(SyntheticField): |
56 | 56 | ||
57 | @staticmethod | 57 | def formatter(self, api_client, data, value): |
58 | def formatter(api_client, field, data, value): | ||
59 | """Get audio-related fields | 58 | """Get audio-related fields |
60 | 59 | ||
61 | Try to find fields for the audio url for specified preferred quality | 60 | Try to find fields for the audio url for specified preferred quality |
@@ -97,9 +96,9 @@ class AudioField(SyntheticField): | |||
97 | audio_url = url_map.get(quality) | 96 | audio_url = url_map.get(quality) |
98 | 97 | ||
99 | if audio_url: | 98 | if audio_url: |
100 | return audio_url[field] | 99 | return audio_url[self.field] |
101 | 100 | ||
102 | return audio_url[field] if audio_url else None | 101 | return audio_url[self.field] if audio_url else None |
103 | 102 | ||
104 | 103 | ||
105 | class PlaylistModel(PandoraModel): | 104 | class PlaylistModel(PandoraModel): |