2020-01-26 22:17:09 +09:00
|
|
|
import builtins
|
2019-05-07 06:02:21 +09:00
|
|
|
from abc import ABCMeta
|
2020-01-26 22:17:09 +09:00
|
|
|
from typing import TYPE_CHECKING, Optional
|
2019-05-07 06:02:21 +09:00
|
|
|
|
2020-01-26 22:17:09 +09:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from yandex_music import Client
|
2019-12-27 05:27:58 +09:00
|
|
|
|
Добавлены тайп хинты в соответствии с документацией для следуюшщих классов: YandexMusicObject, Cover, Captcha, DownloadInfo, Experiments, Icon, InvocationInfo, Pager, PermissionAlerts, PromoCodeStatus, Settings, TrackShort, TracksList, Video #120
2019-12-24 16:17:04 +09:00
|
|
|
ujson: bool = False
|
2019-12-23 18:07:11 +09:00
|
|
|
try:
|
|
|
|
import ujson as json
|
2020-01-26 22:17:09 +09:00
|
|
|
|
2019-12-23 18:07:11 +09:00
|
|
|
ujson = True
|
|
|
|
except ImportError:
|
|
|
|
import json
|
|
|
|
|
2019-12-27 05:27:58 +09:00
|
|
|
reserved_names = [name.lower() for name in dir(builtins)]
|
|
|
|
|
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
|
Добавлены тайп хинты в соответствии с документацией для следуюшщих классов: YandexMusicObject, Cover, Captcha, DownloadInfo, Experiments, Icon, InvocationInfo, Pager, PermissionAlerts, PromoCodeStatus, Settings, TrackShort, TracksList, Video #120
2019-12-24 16:17:04 +09:00
|
|
|
_id_attrs: tuple = ()
|
|
|
|
def __str__(self) -> str:
|
2019-05-07 06:02:21 +09:00
|
|
|
return str(self.to_dict())
|
|
|
|
|
Добавлены тайп хинты в соответствии с документацией для следуюшщих классов: YandexMusicObject, Cover, Captcha, DownloadInfo, Experiments, Icon, InvocationInfo, Pager, PermissionAlerts, PromoCodeStatus, Settings, TrackShort, TracksList, Video #120
2019-12-24 16:17:04 +09:00
|
|
|
def __repr__(self) -> str:
|
2019-05-15 05:38:15 +09:00
|
|
|
return str(self)
|
|
|
|
|
2019-05-07 06:02:21 +09:00
|
|
|
def __getitem__(self, item):
|
|
|
|
return self.__dict__[item]
|
|
|
|
|
|
|
|
@classmethod
|
2020-01-26 22:17:09 +09:00
|
|
|
def de_json(cls, data: dict, client: Optional['Client']) -> Optional[dict]:
|
|
|
|
"""Десериализация объекта.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data (:obj:`dict`): Поля и значения десериализуемого объекта.
|
Удаление избыточной информации (#247)
Классы: Account, AutoRenewable, PassportPhone, Permissions, Plus, Price,
Product, Status, Subscription, UserSettings, Album, Label,
TrackPosition, Playlist
У всех классов изменено описание атрибута client
2020-03-22 04:49:20 +09:00
|
|
|
client (:obj:`yandex_music.Client`, optional): Клиент Yandex Music.
|
2020-01-26 22:17:09 +09:00
|
|
|
|
|
|
|
Returns:
|
2020-03-22 06:29:34 +09:00
|
|
|
:obj:`yandex_music.YandexMusicObject` | :obj:`None`: :obj:`yandex_music.YandexMusicObject` или :obj:`None`.
|
2020-01-26 22:17:09 +09:00
|
|
|
"""
|
2019-05-07 06:02:21 +09:00
|
|
|
if not data:
|
|
|
|
return None
|
|
|
|
|
|
|
|
data = data.copy()
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
Добавлены тайп хинты в соответствии с документацией для следуюшщих классов: YandexMusicObject, Cover, Captcha, DownloadInfo, Experiments, Icon, InvocationInfo, Pager, PermissionAlerts, PromoCodeStatus, Settings, TrackShort, TracksList, Video #120
2019-12-24 16:17:04 +09:00
|
|
|
def to_json(self) -> str:
|
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
|
|
|
|
Добавлены тайп хинты в соответствии с документацией для следуюшщих классов: YandexMusicObject, Cover, Captcha, DownloadInfo, Experiments, Icon, InvocationInfo, Pager, PermissionAlerts, PromoCodeStatus, Settings, TrackShort, TracksList, Video #120
2019-12-24 16:17:04 +09:00
|
|
|
def to_dict(self) -> dict:
|
Добавлены основные тесты для следующих классов: 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):
|
2020-01-26 21:19:22 +09:00
|
|
|
return {key: parse(value) for key, value in val.items()}
|
Добавлены основные тесты для следующих классов: 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
|
|
|
else:
|
|
|
|
return val
|
|
|
|
|
|
|
|
data = self.__dict__.copy()
|
|
|
|
data.pop('client', None)
|
|
|
|
data.pop('_id_attrs', None)
|
|
|
|
|
2019-12-27 05:27:58 +09:00
|
|
|
for k, v in data.copy().items():
|
|
|
|
if k.lower() in reserved_names:
|
|
|
|
data.pop(k)
|
|
|
|
data.update({f'{k}_': v})
|
|
|
|
|
Добавлены основные тесты для следующих классов: 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
|
|
|
return parse(data)
|
2019-05-07 06:02:21 +09:00
|
|
|
|
Добавлены тайп хинты в соответствии с документацией для следуюшщих классов: YandexMusicObject, Cover, Captcha, DownloadInfo, Experiments, Icon, InvocationInfo, Pager, PermissionAlerts, PromoCodeStatus, Settings, TrackShort, TracksList, Video #120
2019-12-24 16:17:04 +09:00
|
|
|
def __eq__(self, other) -> bool:
|
2019-05-07 06:02:21 +09:00
|
|
|
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__()
|