home · contact · privacy
Add make_config command to create a default config file. master
authorChristian Heller <c.heller@plomlompom.de>
Sat, 20 Sep 2025 12:38:29 +0000 (14:38 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sat, 20 Sep 2025 12:38:29 +0000 (14:38 +0200)
src/run.py
src/ytplom/misc.py

index f3da3ebadacb22857b776fa70af1ee2511452389..00c528a0982ad34962199f8daeb691d03f77fb89 100755 (executable)
@@ -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':
index 2eed335412180aac2615e1f8e3316b06a33303bb..fc3847a7ea6b1de705c2e948294697e288abf51e 100644 (file)
@@ -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)."""