Skip to content

Commit

Permalink
fix/playlist_deserialization (#262)
Browse files Browse the repository at this point in the history
* fix/playlist_deserialization

* rm debug print
  • Loading branch information
JarbasAl authored Aug 3, 2024
1 parent 70c46a3 commit b90285c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
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

0 comments on commit b90285c

Please sign in to comment.