From: Christian Heller Date: Sat, 20 Sep 2025 12:38:29 +0000 (+0200) Subject: Add make_config command to create a default config file. X-Git-Url: https://plomlompom.com/repos/booking/condition?a=commitdiff_plain;h=HEAD;p=ytplom Add make_config command to create a default config file. --- diff --git a/src/run.py b/src/run.py index f3da3eb..00c528a 100755 --- a/src/run.py +++ b/src/run.py @@ -8,6 +8,7 @@ try: from ytplom.db import DbFile from ytplom.primitives import HandledException from ytplom.migrations import MIGRATIONS + from ytplom.misc import Config from ytplom.http import serve from ytplom.sync import sync except ModuleNotFoundError as e: @@ -25,6 +26,8 @@ if __name__ == '__main__': case 'install_deps': raise HandledException( 'Should be handled by calling script.') + case 'make_config': + Config().save_file() case 'create_db': DbFile.create() case 'migrate_db': diff --git a/src/ytplom/misc.py b/src/ytplom/misc.py index 2eed335..fc3847a 100644 --- a/src/ytplom/misc.py +++ b/src/ytplom/misc.py @@ -7,7 +7,7 @@ from random import shuffle from time import sleep from datetime import datetime, timedelta from decimal import Decimal -from json import loads as json_loads +from json import dumps as json_dumps, loads as json_loads from urllib.request import urlretrieve from uuid import uuid4 from pathlib import Path @@ -25,17 +25,19 @@ from ytplom.primitives import HandledException, NotFoundException # default configuration DEFAULTS = { + 'api_key': '', + 'allow_file_edit': True, + 'background_color': '#ffffff', 'host': '127.0.0.1', # NB: to be found remotely, use '0.0.0.0'! + 'link_originals': True, 'port': 8090, 'port_remote': 8090, - 'background_color': '#ffffff', + 'remote': '192.168.1.100', 'queries_cutoff': '', + 'tags_default': ['new'], + 'tags_display_whitelist': [], 'tags_prefilter_whitelist': [], 'tags_prefilter_needed': [], - 'tags_display_whitelist': [], - 'tags_default': ['new'], - 'allow_file_edit': True, - 'link_originals': True } # type definitions for mypy @@ -172,19 +174,19 @@ class TagSet: class Config: """Collects user-configurable settings.""" + allow_file_edit: bool + api_key: str + background_color: str host: str - remote: str + link_originals: bool port: int port_remote: int - api_key: str - background_color: str - allow_file_edit: bool queries_cutoff: DatetimeStr + remote: str + tags_default: TagSet + tags_display_whitelist: TagSet tags_prefilter_needed: TagSet tags_prefilter_whitelist: TagSet - tags_display_whitelist: TagSet - tags_default: TagSet - link_originals: bool def __init__(self): def set_attrs_from_dict(d): @@ -202,6 +204,17 @@ class Config: if key.startswith('tags_')]: setattr(VideoFile, attr_key, getattr(self, attr_key)) + def save_file(self) -> None: + 'Write own state as JSON to PATH_CONFFILE.' + if PATH_CONFFILE.is_file(): + raise HandledException( + 'Config file already present, not overwriting.') + d: dict[str, bool | int | str | list[str]] = {} + for key, value in [(key, getattr(self, key)) + for key in self.__class__.__annotations__.keys()]: + d[key] = value.as_str_list if isinstance(value, TagSet) else value + PATH_CONFFILE.write_text(json_dumps(d), encoding='utf8') + class YoutubeQuery(DbData): """Representation of YouTube query (without results)."""