From d93429bf62f076a76876014ac35bbc9355fd5b30 Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Mon, 30 Oct 2017 02:33:48 +0000 Subject: 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. --- pandora/models/__init__.py | 8 +++++--- 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"])): payload. """ - @staticmethod - def formatter(api_client, field, data): # pragma: no cover + def formatter(self, api_client, data, newval): # pragma: no cover """Format Value for Model 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"])): data complete JSON data blob for the parent model of which this field is a member + newval + the value of this field as retrieved from the JSON data after + having resolved default value logic """ raise NotImplementedError @@ -119,7 +121,7 @@ class PandoraModel(with_metaclass(ModelMetaClass, object)): newval = data.get(value.field, default) if isinstance(value, SyntheticField): - newval = value.formatter(api_client, value.field, data, newval) + newval = value.formatter(api_client, data, newval) setattr(instance, key, newval) continue 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): class AudioField(SyntheticField): - @staticmethod - def formatter(api_client, field, data, value): + def formatter(self, api_client, data, value): """Get audio-related fields Try to find fields for the audio url for specified preferred quality @@ -97,9 +96,9 @@ class AudioField(SyntheticField): audio_url = url_map.get(quality) if audio_url: - return audio_url[field] + return audio_url[self.field] - return audio_url[field] if audio_url else None + return audio_url[self.field] if audio_url else None class PlaylistModel(PandoraModel): -- cgit v1.2.3