Mo basics

This commit is contained in:
Piotr Dec 2025-11-02 22:15:12 +01:00
parent 1341b022d4
commit 8ee950940e
Signed by: stawros
GPG key ID: 74B18A3F0F1E99C0
3 changed files with 44 additions and 26 deletions

16
app/services/mo.py Normal file
View file

@ -0,0 +1,16 @@
from pathlib import Path
from typing import Annotated
from injectable import injectable, autowired, Autowired
from app.services import Passwords
@injectable
class Mo:
@autowired
def __init__(self, passwords: Annotated[Passwords, Autowired]):
self._passwords = passwords
def process(self, mo_file: Path):
pass

View file

@ -1,7 +1,8 @@
import os.path
import shutil
from injectable import injectable
from pykeepass import PyKeePass, create_database, Group
from pykeepass import PyKeePass, create_database
@injectable(singleton=True)
@ -12,29 +13,27 @@ class Passwords:
with open(settings.kp.secret, "r") as fh:
secret = fh.read().splitlines()[0]
self._kp_org = self.__get_or_create_store(settings.kp.file, secret)
self._kp = self.__get_lock(settings.kp.file, secret)
self._path = settings.kp.file
self._kp_org = self._open_or_create(self._path, secret)
self._kp = self._open_lock(self._path, secret)
@staticmethod
def __get_or_create_store(path, passwd) -> PyKeePass:
def _open_or_create(path, password) -> PyKeePass:
if os.path.exists(path):
return PyKeePass(
path,
password=passwd,
)
return create_database(path, passwd)
return PyKeePass(path, password=password)
return create_database(path, password)
@staticmethod
def __get_lock(path, passwd) -> PyKeePass:
def _open_lock(path, password) -> PyKeePass:
lock_path = path + ".lock"
import shutil
shutil.copyfile(path, lock_path)
return Passwords.__get_or_create_store(lock_path, passwd)
return Passwords._open_or_create(lock_path, password)
@property
def store(self):
return self._kp.root_group
def kp(self) -> PyKeePass:
return self._kp
def save(self, group: Group):
pass
def save(self):
# nadpisz plik źródłowy zmianami z lock
self._kp.save()
shutil.copyfile(self._path + ".lock", self._path)