import subprocess from pathlib import Path from dataclasses import dataclass from typing import Any from pc_backup.env import is_windows @dataclass class KeePass: path: Path bin: str | Path def read_entry_attribute(self, key, attribute): print(f"reading attr {attribute} of key {key}..") return self._exec(["show", "-a", attribute, self.path, key]).strip() def read_entry_attachment(self, key, attachment): print(f"reading attachment {attachment} of key {key}..") return self._exec( ["attachment-export", "--stdout", self.path, key, attachment, "/dev/null"] ) def _exec(self, args: list[Any]): try: return subprocess.check_output([self.bin] + args, text=True) except subprocess.CalledProcessError as e: print("\nThere was an error on call to keepass, please check the outout") exit(1) @classmethod def new(cls, path: Path): binary = ( Path("C:\\") / "Program Files" / "KeePassXC" / "keepassxc-cli.exe" if is_windows() else "keepassxc-cli" ) return cls(path=path, bin=binary)