From: Christian Heller Date: Sun, 24 Nov 2024 16:26:14 +0000 (+0100) Subject: Refactor checks and reactions to paths existing, possibly as wrong entry type. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7B%20web_path%20%7D%7D/%7B%7Bdb.prefix%7D%7D/%7B%7Bprefix%7D%7D?a=commitdiff_plain;h=35e61ea46e43cdedf1433623d7175e8641550b1f;p=ytplom Refactor checks and reactions to paths existing, possibly as wrong entry type. --- diff --git a/ytplom/misc.py b/ytplom/misc.py index a5fbeb8..8c28a67 100644 --- a/ytplom/misc.py +++ b/ytplom/misc.py @@ -133,12 +133,12 @@ class CantWriteException(Exception): 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): + if not isdir(dir_name): + if path_exists(dir_name): + raise CantWriteException('at expected directory path ' + f'{dir_name} found non-directory') 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 CantWriteException(msg) class DatabaseConnection: @@ -146,7 +146,10 @@ class DatabaseConnection: def __init__(self, path: PathStr = PATH_DB) -> None: self._path = path - if not path_exists(self._path): + if not isfile(self._path): + if path_exists(self._path): + raise CantWriteException(f'no DB at {self._path}; would create' + ', but something\'s already there?') with sql_connect(self._path) as conn: conn.executescript(SCRIPT_INIT_DB) self._conn = sql_connect(self._path)