from dataclasses import dataclass, field import db @dataclass class SFTPConfig: host: str = "" port: int = 22 user: str = "" auth_method: str = "key" # "key" or "password" key: str = "" password: str = "" remote_path: str = "" @dataclass class CalibreConfig: url: str = "" user: str = "" password: str = "" @dataclass class AppConfig: sftp: SFTPConfig = field(default_factory=SFTPConfig) calibre: CalibreConfig = field(default_factory=CalibreConfig) local_work_dir: str = "/tmp/calibresync" def load() -> AppConfig: s = db.get_all_settings() return AppConfig( sftp=SFTPConfig( host=s.get("sftp_host", ""), port=int(s.get("sftp_port", "22")), user=s.get("sftp_user", ""), auth_method=s.get("sftp_auth_method", "key"), key=s.get("sftp_key", ""), password=s.get("sftp_password", ""), remote_path=s.get("sftp_remote_path", ""), ), calibre=CalibreConfig( url=s.get("calibre_url", "").rstrip("/"), user=s.get("calibre_user", ""), password=s.get("calibre_pass", ""), ), local_work_dir=s.get("local_work_dir", "/tmp/calibresync"), ) def save(form: dict) -> None: keys = [ "sftp_host", "sftp_port", "sftp_user", "sftp_auth_method", "sftp_password", "sftp_remote_path", "calibre_url", "calibre_user", "calibre_pass", "local_work_dir", "scheduler_interval_minutes", "sync_batch_size", ] for key in keys: if key in form and form[key] is not None: db.set_setting(key, str(form[key])) # Only overwrite the SSH key if a non-empty value was submitted if form.get("sftp_key", "").strip(): db.set_setting("sftp_key", form["sftp_key"].strip())