From: Christian Heller Date: Sun, 15 Dec 2024 02:45:22 +0000 (+0100) Subject: Simplify config file tagset fields. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/static/%7B%7Bprefix%7D%7D/process?a=commitdiff_plain;h=affe405681c1be7694768916f7debd6936d183d7;p=ytplom Simplify config file tagset fields. --- diff --git a/src/ytplom/misc.py b/src/ytplom/misc.py index ea65b5e..6cadf49 100644 --- a/src/ytplom/misc.py +++ b/src/ytplom/misc.py @@ -29,9 +29,9 @@ DEFAULTS = { 'port_remote': 8090, 'background_color': '#ffffff', 'queries_cutoff': '', - 'whitelist_tags_prefilter_str': '', - 'needed_tags_prefilter_str': '', - 'whitelist_tags_display_str': '', + 'whitelist_tags_prefilter': [], + 'needed_tags_prefilter': [], + 'whitelist_tags_display': [], 'allow_file_edit': True } @@ -84,6 +84,68 @@ def ensure_expected_dirs(expected_dirs: list[Path]) -> None: dir_path.mkdir(parents=True, exist_ok=True) +class TagSet: + """Collection of tags as used in VideoFile.tags.""" + + def __init__(self, tags: Optional[set[str]] = None) -> None: + self._tags = tags if tags else set() + + def __iter__(self) -> Generator: + yield from self._tags + + def add(self, other_tags: Self) -> None: + """To self, add tags of other_tags.""" + # pylint:disable=protected-access + self._tags = self._tags | other_tags._tags + + def all_not_in(self, other_tags: Self) -> Self: + """Return all tags of self _not_ in other_tags.""" + # pylint:disable=protected-access + return self.__class__({t for t in self._tags + if t not in other_tags._tags}) + + def all_also_in(self, other_tags: Self) -> Self: + """Return all tags of self _also_ in other_tags""" + # pylint:disable=protected-access + return self.__class__({t for t in self._tags + if t in other_tags._tags}) + + def are_all_in(self, other_tags: Self) -> bool: + """Return if all tags of self are also in other_tags.""" + return self.empty or self.all_not_in(other_tags).empty + + def whitelisted(self, whitelist: Self) -> Self: + """Return self filtered by whitelist; if empty, return all.""" + if whitelist.empty: + return self + return self.all_also_in(whitelist) + + @property + def empty(self) -> bool: + """Return if self empty.""" + return 0 == len(self._tags) + + @property + def as_str_list(self) -> list[str]: + """Return self list of strings.""" + return [str(t) for t in self._tags] + + @property + def joined(self) -> str: + """Return self as string of comma-separated tags.""" + return ','.join(self.as_str_list) + + @classmethod + def from_joined(cls, joined: str) -> Self: + """Build self from string of comma-separated tags.""" + return cls({t for t in joined.split(',') if t}) + + @classmethod + def from_str_list(cls, str_list: list[str]) -> Self: + """Build self from list of tag strings.""" + return cls(set(str_list)) + + class Config: """Collects user-configurable settings.""" host: str @@ -92,11 +154,11 @@ class Config: port_remote: int api_key: str background_color: str - queries_cutoff: str - needed_tags_prefilter_str: str - whitelist_tags_prefilter_str: str allow_file_edit: bool - whitelist_tags_display_str: str + queries_cutoff: DatetimeStr + needed_tags_prefilter: TagSet + whitelist_tags_prefilter: TagSet + whitelist_tags_display: TagSet def __init__(self): def set_attrs_from_dict(d): @@ -110,12 +172,6 @@ class Config: set_attrs_from_dict({k[len(ENVIRON_PREFIX):].lower(): v for k, v in environ.items() if k.isupper() and k.startswith(ENVIRON_PREFIX)}) - self.needed_tags_prefilter = TagSet.from_joined( - self.needed_tags_prefilter_str) - self.whitelist_tags_prefilter = TagSet.from_joined( - self.whitelist_tags_prefilter_str) - self.whitelist_tags_display = TagSet.from_joined( - self.whitelist_tags_display_str) class YoutubeQuery(DbData): @@ -258,68 +314,6 @@ class YoutubeVideo(DbData): (query_id, self.id_)) -class TagSet: - """Collection of tags as used in VideoFile.tags.""" - - def __init__(self, tags: Optional[set[str]] = None) -> None: - self._tags = tags if tags else set() - - def __iter__(self) -> Generator: - yield from self._tags - - def add(self, other_tags: Self) -> None: - """To self, add tags of other_tags.""" - # pylint:disable=protected-access - self._tags = self._tags | other_tags._tags - - def all_not_in(self, other_tags: Self) -> Self: - """Return all tags of self _not_ in other_tags.""" - # pylint:disable=protected-access - return self.__class__({t for t in self._tags - if t not in other_tags._tags}) - - def all_also_in(self, other_tags: Self) -> Self: - """Return all tags of self _also_ in other_tags""" - # pylint:disable=protected-access - return self.__class__({t for t in self._tags - if t in other_tags._tags}) - - def are_all_in(self, other_tags: Self) -> bool: - """Return if all tags of self are also in other_tags.""" - return self.empty or self.all_not_in(other_tags).empty - - def whitelisted(self, whitelist: Self) -> Self: - """Return self filtered by whitelist; if empty, return all.""" - if whitelist.empty: - return self - return self.all_also_in(whitelist) - - @property - def empty(self) -> bool: - """Return if self empty.""" - return 0 == len(self._tags) - - @property - def as_str_list(self) -> list[str]: - """Return self list of strings.""" - return [str(t) for t in self._tags] - - @property - def joined(self) -> str: - """Return self as string of comma-separated tags.""" - return ','.join(self.as_str_list) - - @classmethod - def from_joined(cls, joined: str) -> Self: - """Build self from string of comma-separated tags.""" - return cls({t for t in joined.split(',') if t}) - - @classmethod - def from_str_list(cls, str_list: list[str]) -> Self: - """Build self from list of tag strings.""" - return cls(set(str_list)) - - class VideoFile(DbData): """Collects data about downloaded files.""" id_name = 'digest'