diff --git a/requirements.txt b/requirements.txt index b3cf464..55f5d38 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ deprecated~=1.2.13 requests-oauthlib>=1.3 requests>=2.25 +dataclasses; python_version<"3.7" diff --git a/trakt/mixins.py b/trakt/mixins.py index 4b39195..3222faf 100644 --- a/trakt/mixins.py +++ b/trakt/mixins.py @@ -14,8 +14,10 @@ class IdsMixin: __ids = ['imdb', 'slug', 'tmdb', 'trakt'] - def __init__(self): - self._ids = {} + def __init__(self, ids=None): + if ids is None: + ids = {} + self._ids = ids @property def ids(self): @@ -51,3 +53,11 @@ def tvdb(self): @property def tvrage(self): return self._ids.get('tvrage', None) + + @property + def slug(self): + return self._ids.get('slug', None) + + @slug.setter + def slug(self, value): + self._ids['slug'] = value diff --git a/trakt/users.py b/trakt/users.py index 90717ef..3d2224f 100644 --- a/trakt/users.py +++ b/trakt/users.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- """Interfaces to all of the User objects offered by the Trakt.tv API""" -from collections import namedtuple +from collections import UserDict, namedtuple +from dataclasses import dataclass, field +from typing import Optional, Union from trakt.core import delete, get, post from trakt.mixins import IdsMixin @@ -62,28 +64,49 @@ def unfollow(user_name): yield 'users/{username}/follow'.format(username=slugify(user_name)) -class UserList(namedtuple('UserList', ['name', 'description', 'privacy', - 'share_link', 'type', 'display_numbers', - 'allow_comments', 'sort_by', - 'sort_how', 'created_at', - 'updated_at', 'item_count', - 'comment_count', 'likes', 'ids', - 'user', 'creator']), IdsMixin): +@dataclass(frozen=True) +class UserListData: + name: str + description: str + privacy: str + share_link: str + type: str + display_numbers: str + allow_comments: str + sort_by: str + sort_how: str + created_at: str + updated_at: str + item_count: str + comment_count: str + likes: str + user: str + creator: str + + +def DataClassMixin(data_class): + class DataClassMixinClass: + def __init__(self, **kwargs): + self.data = data_class(**kwargs) + + def __getattr__(self, item): + return getattr(self.data, item) + + return DataClassMixinClass + + +class UserList(DataClassMixin(UserListData), IdsMixin): """A list created by a Trakt.tv :class:`User`""" - def __init__(self, *args, ids=None, **kwargs): - super().__init__() + def __init__(self, ids=None, **kwargs): + super().__init__(**kwargs) self._ids = ids - self._items = list() + self._items = [] def __iter__(self): """Iterate over the items in this user list""" return self._items.__iter__() - @property - def slug(self): - return self._ids.get('slug', None) - @classmethod @post def create(cls, name, creator, description=None, privacy='private',