Skip to content

Commit

Permalink
refactor(Track): Use defaultdict with a dict factory for data property
Browse files Browse the repository at this point in the history
This is so both internal code and service code can save data to sub-keys without the parent keys needing to exist.

This also fixes a bug introduced in v3.3.3 where it will fail to download tracks without the "hls" key in the data property. This can happen when manually making Audio tracks using the HLS descriptor, and not putting any of the hls data the HLS class sets in to_tracks().
  • Loading branch information
rlaphoenix committed May 9, 2024
1 parent 50d6f3a commit 2196892
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions devine/core/tracks/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import shutil
import subprocess
from collections import defaultdict
from copy import copy
from enum import Enum
from functools import partial
Expand Down Expand Up @@ -42,7 +43,7 @@ def __init__(
drm: Optional[Iterable[DRM_T]] = None,
edition: Optional[str] = None,
downloader: Optional[Callable] = None,
data: Optional[dict] = None,
data: Optional[Union[dict, defaultdict]] = None,
id_: Optional[str] = None,
) -> None:
if not isinstance(url, (str, list)):
Expand All @@ -63,8 +64,8 @@ def __init__(
raise TypeError(f"Expected edition to be a {str}, not {type(edition)}")
if not isinstance(downloader, (Callable, type(None))):
raise TypeError(f"Expected downloader to be a {Callable}, not {type(downloader)}")
if not isinstance(data, (dict, type(None))):
raise TypeError(f"Expected data to be a {dict}, not {type(data)}")
if not isinstance(data, (dict, defaultdict, type(None))):
raise TypeError(f"Expected data to be a {dict} or {defaultdict}, not {type(data)}")

invalid_urls = ", ".join(set(type(x) for x in url if not isinstance(x, str)))
if invalid_urls:
Expand Down Expand Up @@ -93,7 +94,7 @@ def __init__(
self.drm = drm
self.edition: str = edition
self.downloader = downloader
self.data = data or {}
self.data = defaultdict(dict, **(data or {}))

if self.name is None:
lang = Language.get(self.language)
Expand Down

0 comments on commit 2196892

Please sign in to comment.