From 98bed10f514a3aac975352d985f7ec54a5a9a73b Mon Sep 17 00:00:00 2001 From: miro Date: Sat, 3 Aug 2024 19:26:21 +0100 Subject: [PATCH 1/2] fix/playlist_deserialization --- ovos_utils/ocp.py | 14 +++++++++----- test/unittests/test_ocp_media.py | 3 +++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/ovos_utils/ocp.py b/ovos_utils/ocp.py index 0768c26..f46d18e 100644 --- a/ovos_utils/ocp.py +++ b/ovos_utils/ocp.py @@ -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: @@ -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: @@ -372,9 +376,9 @@ 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) + print(888, playlist) for e in track.get("playlist", []): playlist.add_entry(e) return playlist diff --git a/test/unittests/test_ocp_media.py b/test/unittests/test_ocp_media.py index 00f7304..13c6fa1 100644 --- a/test/unittests/test_ocp_media.py +++ b/test/unittests/test_ocp_media.py @@ -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 From 35ad92570ef50f0a7d7f3b8f963caced9c1b35c6 Mon Sep 17 00:00:00 2001 From: miro Date: Sat, 3 Aug 2024 19:27:02 +0100 Subject: [PATCH 2/2] rm debug print --- ovos_utils/ocp.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ovos_utils/ocp.py b/ovos_utils/ocp.py index f46d18e..194da33 100644 --- a/ovos_utils/ocp.py +++ b/ovos_utils/ocp.py @@ -378,7 +378,6 @@ def from_dict(track: dict) -> 'Playlist': raise ValueError("track dictionary does not contain 'playlist' entries, it is not a valid Playlist") playlist = Playlist(**track) - print(888, playlist) for e in track.get("playlist", []): playlist.add_entry(e) return playlist