Skip to content

Commit

Permalink
Renamed Descriptor 'from_dict' to 'from_node_dict', added '$source' k…
Browse files Browse the repository at this point in the history
…ey to dict
  • Loading branch information
fuzeman committed May 30, 2014
1 parent 209308c commit 78e63ea
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 11 deletions.
6 changes: 5 additions & 1 deletion spotify/objects/album.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ def tracks(self):
yield track

@classmethod
def from_dict(cls, sp, data, types):
def from_node_dict(cls, sp, data, types):
uri = Uri.from_id('album', data.get('id'))

return cls(sp, {
'gid': uri.to_gid(),
'name': data.get('name'),
'artist': [
{
'$source': 'node',
'id': data.get('artist-id'),
'name': data.get('artist-name')
}
Expand Down Expand Up @@ -95,14 +96,17 @@ def get_covers(cls, data):

return [
{
'$source': 'node',
'file_id': data.get('cover'),
'size': 0
},
{
'$source': 'node',
'file_id': data.get('cover-small'),
'size': 1
},
{
'$source': 'node',
'file_id': data.get('cover-large'),
'size': 2
}
Expand Down
5 changes: 4 additions & 1 deletion spotify/objects/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Artist(Descriptor):
portrait_group = PropertyProxy('portrait_group')

@classmethod
def from_dict(cls, sp, data, types):
def from_node_dict(cls, sp, data, types):
uri = Uri.from_id('artist', data.get('id'))

return cls(sp, {
Expand All @@ -62,18 +62,21 @@ def get_portraits(cls, data):

return [
{
'$source': 'node',
'file_id': portrait.get('id'),
'size': 0,
'width': width,
'height': height
},
{
'$source': 'node',
'file_id': portrait.get('small'),
'size': 1,
'width': math.ceil(width * 0.32),
'height': math.ceil(height * 0.32)
},
{
'$source': 'node',
'file_id': portrait.get('large'),
'size': 2,
'width': math.ceil(width * 3.2),
Expand Down
18 changes: 15 additions & 3 deletions spotify/objects/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,18 @@ def construct(self, sp, value, types):
return self.type.from_node(sp, value, types)

if type(value) is dict:
return self.type.from_dict(sp, value, types)
return self.construct_dict(sp, value, types)

return self.type.from_protobuf(sp, value, types)

def construct_dict(self, sp, value, types):
source = value.get('$source')

if source == 'node':
return self.type.from_node_dict(sp, value, types)

return self.type.from_dict(sp, value, types)

@staticmethod
def parse_date(value):
try:
Expand Down Expand Up @@ -195,12 +203,16 @@ def from_protobuf(cls, sp, internal, types, defaults=None):

@classmethod
def from_node(cls, sp, node, types):
return cls.from_dict(sp, etree_convert(node), types)
return cls.from_node_dict(sp, etree_convert(node), types)

@classmethod
def from_dict(cls, sp, data, types):
def from_node_dict(cls, sp, data, types):
raise NotImplementedError()

@classmethod
def from_dict(cls, sp, data, types):
return cls.from_node_dict(sp, data, types)

@classmethod
def construct(cls, sp, **kwargs):
obj = cls(sp)
Expand Down
2 changes: 1 addition & 1 deletion spotify/objects/external_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ExternalId(Descriptor):
id = PropertyProxy

@classmethod
def from_dict(cls, sp, data, types):
def from_node_dict(cls, sp, data, types):
return cls(sp, {
'type': data.get('type'),
'id': data.get('id')
Expand Down
2 changes: 1 addition & 1 deletion spotify/objects/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def file_url(self):
)

@classmethod
def from_dict(cls, sp, data, types):
def from_node_dict(cls, sp, data, types):
return cls(sp, {
'file_id': Uri.from_id('image', data.get('file_id')).to_gid(size=40),

Expand Down
2 changes: 1 addition & 1 deletion spotify/objects/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def extend(self, start, count=100):


@classmethod
def from_dict(cls, sp, data, types):
def from_node_dict(cls, sp, data, types):
uri = Uri.from_uri(data.get('uri'))

return cls(sp, {
Expand Down
2 changes: 1 addition & 1 deletion spotify/objects/restriction.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def check(self):
return False, 'unknown failure'

@classmethod
def from_dict(cls, sp, data, types):
def from_node_dict(cls, sp, data, types):
catalogue = [
CATALOGUE_MAP[name]
for name in data.get('catalogues', '').split(',')
Expand Down
6 changes: 4 additions & 2 deletions spotify/objects/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,12 @@ def track_end(self, lid, position, seeks=None, latency=150, context='unknown',

@classmethod
def from_node(cls, sp, node, types):
return cls.from_dict(sp, etree_convert(node, {
return cls.from_node_dict(sp, etree_convert(node, {
'artist-id': ('artist-id', 'artist')
}), types)

@classmethod
def from_dict(cls, sp, data, types):
def from_node_dict(cls, sp, data, types):
uri = Uri.from_id('track', data.get('id'))

return cls(sp, {
Expand All @@ -227,13 +227,15 @@ def from_dict(cls, sp, data, types):

'artist': [
{
'$source': 'node',
'id': artist.get('artist-id'),
'name': artist.get('artist')
}
for artist in data.get('artist', [])
],

'album': {
'$source': 'node',
'id': data.get('album-id'),
'name': data.get('album'),

Expand Down

0 comments on commit 78e63ea

Please sign in to comment.