diff --git a/poetry.lock b/poetry.lock index 8234754..72ccf61 100644 --- a/poetry.lock +++ b/poetry.lock @@ -243,6 +243,18 @@ tomli = {version = ">=1", markers = "python_version < \"3.11\""} [package.extras] dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"] +[[package]] +name = "tinydb" +version = "3.15.2" +description = "TinyDB is a tiny, document oriented database optimized for your happiness :)" +optional = false +python-versions = "*" +groups = ["main"] +files = [ + {file = "tinydb-3.15.2-py2.py3-none-any.whl", hash = "sha256:1087ade5300c47dbf9539d9f6dafd53115bd5e85a67d480d8188bdbfa2d9eaf4"}, + {file = "tinydb-3.15.2.tar.gz", hash = "sha256:f273d9b6d8b1b5e1d094a6eb8b72851b39b81099293344132c73332b60e3b893"}, +] + [[package]] name = "tomli" version = "2.2.1" @@ -307,4 +319,4 @@ watchdog = ["watchdog (>=2.3)"] [metadata] lock-version = "2.1" python-versions = ">=3.10" -content-hash = "45718b68919d6fafaaed35cd58dbbb244f8ebb0f0c73de8050cd04b8cae82738" +content-hash = "29ea72f4fd9859486c77dc4fd0668e0c55b618c56c01c23a0e2ce0a2cf75d026" diff --git a/pyproject.toml b/pyproject.toml index daff9bb..d23026f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,19 +2,17 @@ name = "senju" version = "0.1.0" description = "API / Webservice for Phrases/Words/Kanji/Haiku" -authors = [ - {name = "PlexSheep",email = "software@cscherr.de"} -] +authors = [{ name = "PlexSheep", email = "software@cscherr.de" }] readme = "README.md" requires-python = ">=3.10" dependencies = [ "jinja2 (>=3.1.5,<4.0.0)", - "pytest>=7.0.0", - "flask (>=3.1.0,<4.0.0)", + "pytest>=7.0.0", + "flask (>=3.1.0,<4.0.0)", + "tinydb (>=3.1.0,<4.0.0)", ] - [build-system] requires = ["poetry-core>=2.0.0,<3.0.0"] build-backend = "poetry.core.masonry.api" diff --git a/senju/store_manager.py b/senju/store_manager.py new file mode 100644 index 0000000..a2df91c --- /dev/null +++ b/senju/store_manager.py @@ -0,0 +1,50 @@ +from typing import Optional +from tinydb import TinyDB, Query +from uuid import UUID, uuid1 +from pathlib import Path +import time + +from tinydb.queries import QueryImpl + +DEFAULT_DB_PATH: Path = Path("/var/lib/senju.json") + + +class StoreManager: + __slots__ = "_db" + _db: TinyDB + + def __init__(self, path_to_db: Path = DEFAULT_DB_PATH) -> None: + self._db = TinyDB(path_to_db) + + def new_id(self) -> UUID: + unix_timestamp: int = int(time.time()) + _guard: int = 0 + while True: + id = uuid1(node=None, clock_seq=unix_timestamp) + if len(self._query(Query().id == id)) > 0: + break + _guard += 1 + if _guard > 100: + raise Exception( + "tried 100 random UUIDs but found no unused one") + return id + + def _query(self, query: QueryImpl) -> list[dict]: + return self._db.search(query) + + def load(self, key: UUID) -> Optional[dict]: + results = self._query(Query().id == key) + if len(results) < 1: + raise Exception("foobar") + elif len(results) > 2: + raise KeyError("The requested item did not exist in our database") + else: + return results[0] + + def save(self, data: dict) -> UUID: + id = self.new_id() + self._db.insert({ + "id": id, + "data": data + }) + return id