home · contact · privacy
Refactor checks and reactions to paths existing, possibly as wrong entry type.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 24 Nov 2024 16:26:14 +0000 (17:26 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 24 Nov 2024 16:26:14 +0000 (17:26 +0100)
ytplom/misc.py

index a5fbeb8174389084e4b94353bead4f8eee2a566c..8c28a678a3edebaa12a65a99addfd342e94dd547 100644 (file)
@@ -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)