home · contact · privacy
Refactor ensurance of expected directories. master
authorChristian Heller <c.heller@plomlompom.de>
Tue, 19 Nov 2024 05:24:51 +0000 (06:24 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 19 Nov 2024 05:24:51 +0000 (06:24 +0100)
ytplom.py

index 3bb9df0554f6f49d325bf48d51cec3176ddbd1f6..d3f3f2a878dab0c2e48cb51000625a5ebd7c7e9f 100755 (executable)
--- a/ytplom.py
+++ b/ytplom.py
@@ -57,7 +57,6 @@ NAME_TEMPLATE_VIDEO_ABOUT = PathStr('video_about.tmpl')
 NAME_TEMPLATE_PLAYLIST = PathStr('playlist.tmpl')
 
 PATH_DIR_TEMP = PathStr(path_join(PATH_DIR_DOWNLOADS, NAME_DIR_TEMP))
 NAME_TEMPLATE_PLAYLIST = PathStr('playlist.tmpl')
 
 PATH_DIR_TEMP = PathStr(path_join(PATH_DIR_DOWNLOADS, NAME_DIR_TEMP))
-EXPECTED_DIRS = [PATH_DIR_DOWNLOADS, PATH_DIR_TEMP, PATH_DIR_THUMBNAILS]
 PATH_TEMPLATE_QUERIES = PathStr(path_join(PATH_DIR_TEMPLATES,
                                           NAME_TEMPLATE_QUERIES))
 TIMESTAMP_FMT = '%Y-%m-%d %H:%M:%S.%f'
 PATH_TEMPLATE_QUERIES = PathStr(path_join(PATH_DIR_TEMPLATES,
                                           NAME_TEMPLATE_QUERIES))
 TIMESTAMP_FMT = '%Y-%m-%d %H:%M:%S.%f'
@@ -104,6 +103,17 @@ CREATE TABLE quota_costs (
 '''
 
 
 '''
 
 
+def _ensure_expected_dirs(expected_dirs: list[PathStr]) -> None:
+    """Ensure existance of expected_dirs _as_ directories."""
+    for dir_name in expected_dirs:
+        if not path_exists(dir_name):
+            print(f'creating expected directory: {dir_name}')
+            makedirs(dir_name)
+        elif not isdir(dir_name):
+            msg = f'at expected directory path {dir_name} found non-directory'
+            raise Exception(msg)
+
+
 class DatabaseConnection:
     """Wrapped sqlite3.Connection."""
 
 class DatabaseConnection:
     """Wrapped sqlite3.Connection."""
 
@@ -415,6 +425,7 @@ class DownloadsDb:
 
     def __init__(self) -> None:
         self._to_download: list[VideoId] = []
 
     def __init__(self) -> None:
         self._to_download: list[VideoId] = []
+        _ensure_expected_dirs([PATH_DIR_DOWNLOADS, PATH_DIR_TEMP])
 
     @staticmethod
     def _id_from_filename(path: PathStr,
 
     @staticmethod
     def _id_from_filename(path: PathStr,
@@ -482,17 +493,6 @@ class Server(HTTPServer):
         self.downloads = downloads_db
 
 
         self.downloads = downloads_db
 
 
-def ensure_expected_dirs_and_files() -> None:
-    """Ensure existance of all dirs and files we need for proper operation."""
-    for dir_name in EXPECTED_DIRS:
-        if not path_exists(dir_name):
-            print(f'creating expected directory: {dir_name}')
-            makedirs(dir_name)
-        elif not isdir(dir_name):
-            msg = f'at expected directory path {dir_name} found non-directory'
-            raise Exception(msg)
-
-
 class TaskHandler(BaseHTTPRequestHandler):
     """Handler for GET and POST requests to our server."""
     server: Server
 class TaskHandler(BaseHTTPRequestHandler):
     """Handler for GET and POST requests to our server."""
     server: Server
@@ -620,6 +620,7 @@ class TaskHandler(BaseHTTPRequestHandler):
         self._send_http(bytes(html, 'utf8'))
 
     def _send_thumbnail(self, filename: PathStr) -> None:
         self._send_http(bytes(html, 'utf8'))
 
     def _send_thumbnail(self, filename: PathStr) -> None:
+        _ensure_expected_dirs([PATH_DIR_THUMBNAILS])
         path_thumbnail = path_join(PATH_DIR_THUMBNAILS, filename)
         if not path_exists(path_thumbnail):
             video_id = splitext(filename)[0]
         path_thumbnail = path_join(PATH_DIR_THUMBNAILS, filename)
         if not path_exists(path_thumbnail):
             video_id = splitext(filename)[0]
@@ -720,7 +721,6 @@ def run():
     """Create DownloadsDb, Player, run server loop."""
     downloads_db = DownloadsDb()
     downloads_db.clean_unfinished()
     """Create DownloadsDb, Player, run server loop."""
     downloads_db = DownloadsDb()
     downloads_db.clean_unfinished()
-    ensure_expected_dirs_and_files()
     Thread(target=downloads_db.download_loop, daemon=False).start()
     server = Server(Player(), downloads_db, ('localhost', HTTP_PORT),
                     TaskHandler)
     Thread(target=downloads_db.download_loop, daemon=False).start()
     server = Server(Player(), downloads_db, ('localhost', HTTP_PORT),
                     TaskHandler)