mirror of
https://github.com/senju1337/senju.git
synced 2025-12-24 07:39:29 +00:00
feat: store manager basic implementation
Refs: OPS-16
This commit is contained in:
parent
554f3bfffc
commit
e6b10ce96e
3 changed files with 67 additions and 7 deletions
14
poetry.lock
generated
14
poetry.lock
generated
|
|
@ -243,6 +243,18 @@ tomli = {version = ">=1", markers = "python_version < \"3.11\""}
|
||||||
[package.extras]
|
[package.extras]
|
||||||
dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"]
|
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]]
|
[[package]]
|
||||||
name = "tomli"
|
name = "tomli"
|
||||||
version = "2.2.1"
|
version = "2.2.1"
|
||||||
|
|
@ -307,4 +319,4 @@ watchdog = ["watchdog (>=2.3)"]
|
||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "2.1"
|
lock-version = "2.1"
|
||||||
python-versions = ">=3.10"
|
python-versions = ">=3.10"
|
||||||
content-hash = "45718b68919d6fafaaed35cd58dbbb244f8ebb0f0c73de8050cd04b8cae82738"
|
content-hash = "29ea72f4fd9859486c77dc4fd0668e0c55b618c56c01c23a0e2ce0a2cf75d026"
|
||||||
|
|
|
||||||
|
|
@ -2,19 +2,17 @@
|
||||||
name = "senju"
|
name = "senju"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
description = "API / Webservice for Phrases/Words/Kanji/Haiku"
|
description = "API / Webservice for Phrases/Words/Kanji/Haiku"
|
||||||
authors = [
|
authors = [{ name = "PlexSheep", email = "software@cscherr.de" }]
|
||||||
{name = "PlexSheep",email = "software@cscherr.de"}
|
|
||||||
]
|
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.10"
|
requires-python = ">=3.10"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"jinja2 (>=3.1.5,<4.0.0)",
|
"jinja2 (>=3.1.5,<4.0.0)",
|
||||||
"pytest>=7.0.0",
|
"pytest>=7.0.0",
|
||||||
"flask (>=3.1.0,<4.0.0)",
|
"flask (>=3.1.0,<4.0.0)",
|
||||||
|
"tinydb (>=3.1.0,<4.0.0)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[build-system]
|
[build-system]
|
||||||
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
||||||
build-backend = "poetry.core.masonry.api"
|
build-backend = "poetry.core.masonry.api"
|
||||||
|
|
|
||||||
50
senju/store_manager.py
Normal file
50
senju/store_manager.py
Normal file
|
|
@ -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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue