diff --git a/api/src/automations/db.py b/api/src/automations/db.py index 986f847..c2705d7 100644 --- a/api/src/automations/db.py +++ b/api/src/automations/db.py @@ -1,15 +1,10 @@ import base64 import json -import sqlite3 -from contextlib import contextmanager -from pathlib import Path -from typing import Iterable, Iterator +from typing import Iterable from src.logger import get_logger from .errors import ( - DBDataError, - DBError, DBNoAutomationFound, RepeatedAutomationId, ) @@ -46,84 +41,33 @@ def decode_obj(obj_str: str) -> dict | list: class AutomationDBConnection: - def __init__(self, db_file: Path) -> None: - self.db_file = db_file + def __init__(self) -> None: + self.db: dict[str, ExtenededAutomation] = {} def reset(self): - self.db_file.unlink(missing_ok=True) - self.create_db() - - @contextmanager - def get_cur(self) -> Iterator[sqlite3.Cursor]: - conn = sqlite3.connect(self.db_file) - cur = conn.cursor() - try: - yield cur - conn.commit() - except sqlite3.DatabaseError as err: - raise DBError(*err.args) from err - finally: - cur.close() - conn.close() - - def create_db(self): - with self.get_cur() as cur: - cur.execute( - f""" - CREATE TABLE IF NOT EXISTS {automations_tbl} ( - id TEXT NOT NULL PRIMARY KEY, - alias TEXT, - description TEXT, - mode TEXT NOT NULL, - source_file TEXT NOT NULL, - source_file_type TEXT NOT NULL, - configuration_key BLOB NOT NULL, - rest BLOB NOT NULL - ); - """ - ) + self.db = {} def insert_automations(self, automations: list[ExtenededAutomation]): - with self.get_cur() as cur: - for automation in automations: - try: - cur.execute( - f""" - INSERT INTO {automations_tbl} VALUES ( - "{automation.id}", - "{automation.alias}", - "{automation.description}", - "{automation.mode}", - "{automation.source_file}", - "{automation.source_file_type}", - "{encode_obj(automation.configuration_key)}", - "{encode_auto(automation)}" - ); - """, - ) - except sqlite3.IntegrityError as err: - raise RepeatedAutomationId( - f"automation id '{automation.id}' in '{automation.source_file}' was already seen before. Please check for duplicate automation ids in your files." - ) from err + repeated_automations = [] + for automation in automations: + if self.db.get(automation.id, None) is None: + self.db[automation.id] = automation + else: + repeated_automations.append( + f"automation id '{automation.id}' in '{automation.source_file}' was already seen before. Please check for duplicate automation ids in your files." + ) + if len(repeated_automations) > 0: + raise RepeatedAutomationId(repeated_automations) def get_automation(self, automation_id: str) -> ExtenededAutomation: - with self.get_cur() as cur: - cur.execute( - f'SELECT rest, source_file, source_file_type, configuration_key FROM {automations_tbl} WHERE id = "{automation_id}"' # nosec - ) - objs = cur.fetchone() - if objs is None: - raise DBNoAutomationFound(automation_id) - [rest, source_file, source_file_type, configuration_key] = objs - return decode_auto(rest, source_file, source_file_type, configuration_key) + if a := self.db.get(automation_id, None): + return a + raise DBNoAutomationFound(automation_id) def list_automations(self, offset: int, limit: int) -> Iterable[ExtenededAutomation]: - with self.get_cur() as cur: - for [rest, source_file, source_file_type, configuration_key] in cur.execute( - f"SELECT rest, source_file, source_file_type, configuration_key FROM {automations_tbl} LIMIT {limit} OFFSET {offset}" # nosec - ): - yield decode_auto(rest, source_file, source_file_type, configuration_key) + for i, auto in enumerate(self.db.values()): + if i >= offset and i < limit + offset: + yield auto def count_automations(self) -> int: - with self.get_cur() as cur: - return cur.execute(f"SELECT count(id) FROM {automations_tbl}").fetchone()[0] # nosec + return len(self.db) diff --git a/api/src/automations/manager.py b/api/src/automations/manager.py index 5965223..a56da58 100644 --- a/api/src/automations/manager.py +++ b/api/src/automations/manager.py @@ -2,7 +2,7 @@ from pathlib import Path from tempfile import mktemp from typing import Optional, Union -from src.errors import ErrorSet +from src.errors import ErrorSet, FormattedError from src.hass_config.loader import HassConfig from src.logger import get_logger from src.yaml_serializer import dump_yaml, load_yaml @@ -24,12 +24,9 @@ class AutomationManager: def __init__( self, hass_config: HassConfig, - db_path: Optional[Path] = None, ) -> None: - if db_path is None: - db_path = Path(mktemp()) # nosec self.hass_config = hass_config - self.db = AutomationDBConnection(db_path) + self.db = AutomationDBConnection() self.tag_path = hass_config.get_automation_tags_path() self.tag_manager = TagManager() self.reload_tags() @@ -62,7 +59,7 @@ class AutomationManager: if len(batch) >= 0: self.db.insert_automations(batch) except DBError as e: - logger.warning(e) + logger.warning(FormattedError.from_error(e)) errors.append(e) if len(errors) > 0: raise ErrorSet(*errors) diff --git a/api/src/errors.py b/api/src/errors.py index 47ff5d6..086da2a 100644 --- a/api/src/errors.py +++ b/api/src/errors.py @@ -1,3 +1,7 @@ +import traceback +from typing import NamedTuple + + class ErrorSet(Exception): """An exception collection exception""" @@ -7,6 +11,24 @@ class ErrorSet(Exception): def gen_message(self): if len(self.errors) > 1: - return "Encountered multiple errors:\n >" + "\n >".join(map(repr, self.errors)) + return "Encountered multiple errors:\n >" + "\n >".join( + map(FormattedError.from_error, self.errors) + ) else: return str(self.errors[0].args) + + +class FormattedError(NamedTuple): + error_name: str + error_message: str + error_traceback: str + original_error: Exception + + @classmethod + def from_error(cls, err: Exception): + return cls( + error_name=str(err.__class__.__name__), + error_message=str(err.args), + error_traceback="\n\n".join(traceback.format_tb(err.__traceback__)), + original_error=err, + ) diff --git a/api/tests/unittests/automations/test_db.py b/api/tests/unittests/automations/test_db.py index ecaa246..8660eba 100644 --- a/api/tests/unittests/automations/test_db.py +++ b/api/tests/unittests/automations/test_db.py @@ -1,23 +1,12 @@ from pathlib import Path -from src.automations.errors import DBDataError +from src.automations.errors import DBDataError, RepeatedAutomationId from src.automations.types import ExtenededAutomation from .utils import TestWithDB class db_tests(TestWithDB): - def test_db_creation(self): - with self.db.get_cur() as cur: - cur.execute("SELECT name FROM sqlite_master WHERE type ='table'") - tables = cur.fetchall() - self.assertListEqual(tables, [("autos",)]) - self.db.create_db() - with self.db.get_cur() as cur: - cur.execute("SELECT name FROM sqlite_master WHERE type ='table'") - tables = cur.fetchall() - self.assertListEqual(tables, [("autos",)]) - def test_insert_automations(self): originals = [ ExtenededAutomation( @@ -61,7 +50,7 @@ class db_tests(TestWithDB): configuration_key=["automation"], ), ] - with self.assertRaises(DBDataError) as err: + with self.assertRaises(RepeatedAutomationId): self.db.insert_automations(originals) autos = list(self.db.list_automations(0, 10)) - self.assertEqual(len(autos), 0) + self.assertEqual(len(autos), 1) diff --git a/api/tests/unittests/automations/test_manager.py b/api/tests/unittests/automations/test_manager.py index 6d8c663..0991aa0 100644 --- a/api/tests/unittests/automations/test_manager.py +++ b/api/tests/unittests/automations/test_manager.py @@ -28,7 +28,6 @@ class manager_tests(TestWithDB): self.config_path = create_copy_of_example_config(ha_config_example) self.automation_manager = AutomationManager( HassConfig(self.config_path), - self.db_file, ) return self.config_path, self.automation_manager @@ -54,10 +53,6 @@ class manager_tests(TestWithDB): {"platform": "zone", "entity_id": "person.thor", "zone": "zone.azguard"} ) automation_manager.update(automation) - self.assertEqual( - automation_manager.get("sublist3-mlist2").trigger, - [], - ) automation_manager.reload() self.assertEqual( automation_manager.get("sublist3-mlist2").trigger, diff --git a/api/tests/unittests/automations/utils.py b/api/tests/unittests/automations/utils.py index 82f7c55..063446b 100644 --- a/api/tests/unittests/automations/utils.py +++ b/api/tests/unittests/automations/utils.py @@ -1,19 +1,8 @@ -from pathlib import Path -from tempfile import mktemp from unittest import TestCase from src.automations.db import AutomationDBConnection -def make_temp_db(): - return AutomationDBConnection(Path(mktemp() + ".db")) - - class TestWithDB(TestCase): def setUp(self) -> None: - self.db = make_temp_db() - self.db.create_db() - self.db_file = self.db.db_file - - def tearDown(self) -> None: - self.db_file.unlink() + self.db = AutomationDBConnection()