From 35e61ea46e43cdedf1433623d7175e8641550b1f Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Sun, 24 Nov 2024 17:26:14 +0100
Subject: [PATCH] Refactor checks and reactions to paths existing, possibly as
 wrong entry type.

---
 ytplom/misc.py | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

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)
-- 
2.30.2