From: Christian Heller Date: Tue, 19 Nov 2024 05:24:51 +0000 (+0100) Subject: Refactor ensurance of expected directories. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7Bdb.prefix%7D%7D/conditions?a=commitdiff_plain;h=f0b29c259d7d60d8c942e94084484e6bd4525fe9;p=ytplom Refactor ensurance of expected directories. --- diff --git a/ytplom.py b/ytplom.py index 3bb9df0..d3f3f2a 100755 --- 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)) -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' @@ -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.""" @@ -415,6 +425,7 @@ class DownloadsDb: 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, @@ -482,17 +493,6 @@ class Server(HTTPServer): 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 @@ -620,6 +620,7 @@ class TaskHandler(BaseHTTPRequestHandler): 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] @@ -720,7 +721,6 @@ def run(): """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)