2019-05-07 06:02:21 +09:00
|
|
|
from abc import ABCMeta
|
|
|
|
|
2019-12-23 18:07:11 +09:00
|
|
|
ujson = False
|
|
|
|
try:
|
|
|
|
import ujson as json
|
|
|
|
ujson = True
|
|
|
|
except ImportError:
|
|
|
|
import json
|
|
|
|
|
2019-05-07 06:02:21 +09:00
|
|
|
|
2019-08-18 18:54:13 +09:00
|
|
|
class YandexMusicObject:
|
2019-05-07 06:02:21 +09:00
|
|
|
__metaclass__ = ABCMeta
|
|
|
|
_id_attrs = ()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.to_dict())
|
|
|
|
|
2019-05-15 05:38:15 +09:00
|
|
|
def __repr__(self):
|
|
|
|
return str(self)
|
|
|
|
|
2019-05-07 06:02:21 +09:00
|
|
|
def __getitem__(self, item):
|
|
|
|
return self.__dict__[item]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def de_json(cls, data, client):
|
|
|
|
if not data:
|
|
|
|
return None
|
|
|
|
|
|
|
|
data = data.copy()
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
def to_json(self):
|
2019-12-23 18:07:11 +09:00
|
|
|
return json.dumps(self.to_dict(), ensure_ascii=not ujson)
|
2019-05-07 06:02:21 +09:00
|
|
|
|
|
|
|
def to_dict(self):
|
Добавлены основные тесты для следующих классов: AlbumEvent, AlbumsLikes, ArtistEvent, ArtistsLikes, ChartItem, Dashboard, Day, Event, Sequence, StationTracksResult, Supplement, TrackWithAds.
Исправлены фикстуры и сравнения для вышеперечисленных классов.
Отрефакторен conftest.py.
Исправлен баг в .to_dict() методе, связанный с не рекурсивной десериализацией объектов списков и словарей.
Исправлен баг с десериализацией artist в классе ArtistEvent
2019-11-09 22:43:02 +09:00
|
|
|
def parse(val):
|
|
|
|
if hasattr(val, 'to_dict'):
|
|
|
|
return val.to_dict()
|
|
|
|
elif isinstance(val, list):
|
|
|
|
return [parse(it) for it in val]
|
|
|
|
elif isinstance(val, dict):
|
|
|
|
return {k: parse(v) for k, v in val.items()}
|
|
|
|
else:
|
|
|
|
return val
|
|
|
|
|
|
|
|
data = self.__dict__.copy()
|
|
|
|
data.pop('client', None)
|
|
|
|
data.pop('_id_attrs', None)
|
|
|
|
|
|
|
|
return parse(data)
|
2019-05-07 06:02:21 +09:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if isinstance(other, self.__class__):
|
|
|
|
return self._id_attrs == other._id_attrs
|
|
|
|
return super(YandexMusicObject, self).__eq__(other)
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
if self._id_attrs:
|
2019-11-10 03:11:43 +09:00
|
|
|
frozen_attrs = tuple(frozenset(attr) if isinstance(attr, list) else attr for attr in self._id_attrs)
|
|
|
|
return hash((self.__class__, frozen_attrs))
|
2019-05-07 06:02:21 +09:00
|
|
|
return super(YandexMusicObject, self).__hash__()
|