Skip to content

Commit

Permalink
feat!: auto-populate the Json meta
Browse files Browse the repository at this point in the history
  • Loading branch information
billsioros committed Jun 25, 2021
1 parent 37155ee commit 2af3ad2
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 13 deletions.
21 changes: 17 additions & 4 deletions dotify/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,24 @@ class ModelMeta(JsonSerializableMeta):
"""

def __new__(cls, name, bases, attrs): # noqa: D102
if "Json" in attrs:
attrs["Json"].schema = cls._dependency_path(name)
json_meta = type("Json", (object,), {})
with contextlib.suppress(KeyError):
json_meta = attrs["Json"]

is_abstract = False
with contextlib.suppress(AttributeError):
is_abstract = json_meta.abstract

if not is_abstract:
json_meta.schema = cls._dependency_path(name)

with contextlib.suppress(AttributeError):
attrs["Json"].dependencies = cls._dependencies_from(
attrs["Json"].dependencies,
json_meta.dependencies = cls._dependencies_from(
json_meta.dependencies,
)

attrs["Json"] = json_meta

return super().__new__(cls, name, bases, attrs)

@classmethod
Expand Down Expand Up @@ -91,6 +101,9 @@ def decorator(_):
class Model(JsonSerializable, metaclass=ModelMeta):
"""The base class for every Spotify Web API entity."""

class Json(object):
abstract = True

class UnexpectedError(Exception):
"""An exception indicating an unexpected error."""

Expand Down
3 changes: 3 additions & 0 deletions dotify/models/_album.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
class AlbumBase(Model):
"""`AlbumBase` defines the interface of the Album class, which is subclassing it."""

class Json(object):
abstract = True

def __str__(self):
return "{0} - {1}".format(self.artist, self.name)

Expand Down
3 changes: 0 additions & 3 deletions dotify/models/_artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
class Artist(Model):
"""A class representing a Spotify `Artist`."""

class Json(object):
""" """

@property
def url(self) -> AnyStr:
"""Return the artist's Spotify URL.
Expand Down
3 changes: 0 additions & 3 deletions dotify/models/_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@
class Image(Model):
"""A class representing a Spotify `Image`."""

class Json(object):
""" """

def __str__(self):
return self.url
3 changes: 3 additions & 0 deletions dotify/models/_playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
class PlaylistBase(Model):
"""`PlaylistBase` defines the interface of the Playlist class, which is subclassing it."""

class Json(object):
abstract = True

def __init__(self, **props) -> None: # noqa: D107
props.pop("tracks", None)

Expand Down
3 changes: 3 additions & 0 deletions dotify/models/_track.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
class TrackBase(Model):
"""`TrackBase` defines the interface of the `Track` class, which is subclassing it."""

class Json(object):
abstract = True

def __str__(self) -> str:
return "{0} - {1}".format(self.artist, self.name)

Expand Down
3 changes: 0 additions & 3 deletions dotify/models/_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@
class User(Model):
"""A model representing a Spotify `User`."""

class Json(object):
""" """

def __str__(self):
return self.display_name

0 comments on commit 2af3ad2

Please sign in to comment.