Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/playlist_deserialization #262

Merged
merged 2 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions ovos_utils/ocp.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,10 @@ def __init__(self, *args, **kwargs):
super().__init__()
for k, v in kwargs.items():
if hasattr(self, k):
self.__setattr__(k, v)
try:
self.__setattr__(k, v)
except AttributeError:
continue
if len(args) == 1 and isinstance(args[0], list):
args = args[0]
for e in args:
Expand All @@ -351,7 +354,8 @@ def __init__(self, *args, **kwargs):
@property
def length(self):
"""calc the length value based on all entries"""
return sum([e.length for e in self.entries])
# -1 is for live streams
return max(-1, sum([e.length for e in self.entries]))

@property
def infocard(self) -> dict:
Expand All @@ -372,9 +376,8 @@ def infocard(self) -> dict:
def from_dict(track: dict) -> 'Playlist':
if "playlist" not in track:
raise ValueError("track dictionary does not contain 'playlist' entries, it is not a valid Playlist")
kwargs = {k: v for k, v in track.items()
if k in inspect.signature(Playlist).parameters}
playlist = Playlist(**kwargs)

playlist = Playlist(**track)
for e in track.get("playlist", []):
playlist.add_entry(e)
return playlist
Expand Down
3 changes: 3 additions & 0 deletions test/unittests/test_ocp_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ def test_properties(self):
for idx, e in enumerate(pl.as_dict["playlist"]):
self.assertEqual(MediaEntry.from_dict(e), search_results[idx])

# test serialize/deserialize
self.assertEqual(Playlist.from_dict(pl.as_dict), pl)

def test_goto_start(self):
# TODO
pass
Expand Down
Loading