From 90bb6d50db3db921f1eb76bfa9b7e6d88bf11ff0 Mon Sep 17 00:00:00 2001 From: Dreagonmon <531486058@qq.com> Date: Mon, 3 Nov 2025 09:34:41 +0800 Subject: [PATCH] WIP storage --- .gitignore | 2 ++ README.md | 6 ++-- backup_box/app.py | 21 +++++++------ backup_box/config.py | 49 ++++++++++++++++++++++++----- backup_box/storage/__init__.py | 5 +++ backup_box/storage/local_storage.py | 4 +++ backup_box/storage/storage.py | 12 +++++++ backup_box/tui.py | 2 +- 8 files changed, 82 insertions(+), 19 deletions(-) create mode 100644 backup_box/storage/__init__.py create mode 100644 backup_box/storage/local_storage.py create mode 100644 backup_box/storage/storage.py diff --git a/.gitignore b/.gitignore index 928e007..0f142b0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ *.egg* __pycache__ /libs/ +/backup_box.toml +/data/ \ No newline at end of file diff --git a/README.md b/README.md index 5199d69..4542c34 100644 --- a/README.md +++ b/README.md @@ -2,5 +2,7 @@ ## Config File -dir config: ./backup_box.toml -user config: C:\Users\\AppData\Local\backup_box\backup_box.toml \ No newline at end of file + +* additional config: bkb -c (--config) <config_toml_path> +* dir config: ./backup_box.toml +* user config: C:\Users\\AppData\Local\backup_box\backup_box.toml \ No newline at end of file diff --git a/backup_box/app.py b/backup_box/app.py index 28cbc61..7d1382e 100644 --- a/backup_box/app.py +++ b/backup_box/app.py @@ -1,7 +1,10 @@ from . import _env, config from argparse import ArgumentParser -from typing import TypedDict +from typing import NamedTuple, Literal +class ArgumentTuple(NamedTuple): + config: str + action: Literal["gui", "backup", "restore"] def parse_args(): p = ArgumentParser() @@ -22,16 +25,16 @@ def parse_args(): p_restore = sps.add_parser("restore") p_restore.set_defaults(action="restore") # parse args - args = p.parse_args() + args: ArgumentTuple = p.parse_args() # type: ignore # type: ArgumentTuple # load config file config.init_default_config() - if args["config"]: - config.apply_user_config(args["config"]) - print(args) + if args.config: + config.apply_user_config(args.config) + print("Args:", args) def main(): - config.init_default_config() - config.reload_config() - print(config.get_config()) - print("Hello World") parse_args() + print("Config:", config.get_config()) + print("Hello World") + from .storage import LocalStorage + print(LocalStorage.get_storage_type()) diff --git a/backup_box/config.py b/backup_box/config.py index 3e0cead..4687b59 100644 --- a/backup_box/config.py +++ b/backup_box/config.py @@ -2,15 +2,30 @@ from . import _env from os import path as _pth from tomllib import load as toml_load from tomli_w import dump as toml_dump -from typing import TypedDict +from typing import TypedDict, Literal EntryItem = TypedDict("EntryItem", { "source": str, + "ignored": list[str], }) +LocalStorageItem = TypedDict("LocalStorageItem", { + "type": Literal["LocalStorageItem"], + "path": str, +}) + +RemoteStorageItem = TypedDict("RemoteStorageItem", { + "type": Literal["RemoteStorageItem"], + "host": str, + "port": str, +}) + +StorageItem = LocalStorageItem | RemoteStorageItem + ConfigDict = TypedDict("ConfigDict", { "_config": list[str], "entry": dict[str, EntryItem], + "storage": dict[str, StorageItem], }) CONFIG_FILE_NAME = "backup_box.toml" @@ -18,31 +33,51 @@ CONFIG_FILE_NAME = "backup_box.toml" def _new_config() -> ConfigDict: return { "entry": {}, + "storage": {}, "_config": [], } -def _override_dict(target: ConfigDict, top_dict: dict): +_cfg: ConfigDict = _new_config() + +def _override_dict(target: ConfigDict, top_dict: ConfigDict): for k, v in top_dict.items(): if isinstance(v, dict) and isinstance(target.get(k, None), dict): - _override_dict(target[k], v) + _override_dict(target[k], v) # type: ignore else: target[k] = v -_cfg: ConfigDict = _new_config() +def _path_related_to(base: str, *sub: str): + return _pth.abspath(_pth.join(base, *sub)) def apply_user_config(dir_or_path: str): if _pth.isdir(dir_or_path): dir_or_path = _pth.abspath(_pth.join(dir_or_path, CONFIG_FILE_NAME)) + cfg_dir = _pth.dirname(dir_or_path) if dir_or_path in _cfg["_config"]: return # skip if already added try: with open(dir_or_path, "rb") as f: - config = toml_load(f) + config: ConfigDict = toml_load(f) # type: ignore except: - config = {} + config: ConfigDict = {} # type: ignore # ignore config file list if "_config" in config: - del config["_config"] + del config["_config"] # type: ignore + # ensure default value + config.setdefault("entry", {}) + for item in config["entry"].values(): + item.setdefault("source", "") + # path related to the config file. + if item["source"]: + item["source"] = _path_related_to(cfg_dir, item["source"]) + item.setdefault("ignored", []) + config.setdefault("storage", {}) + for item in config["storage"].values(): + if item["type"] == "LocalStorageItem": + item.setdefault("path", "") + # path related to the config file. + if item["path"]: + item["path"] = _path_related_to(cfg_dir, item["path"]) # update config _override_dict(_cfg, config) # append config file list diff --git a/backup_box/storage/__init__.py b/backup_box/storage/__init__.py new file mode 100644 index 0000000..d09b01d --- /dev/null +++ b/backup_box/storage/__init__.py @@ -0,0 +1,5 @@ +from .local_storage import LocalStorage + +__all__ = [ + "LocalStorage" +] diff --git a/backup_box/storage/local_storage.py b/backup_box/storage/local_storage.py new file mode 100644 index 0000000..9521c87 --- /dev/null +++ b/backup_box/storage/local_storage.py @@ -0,0 +1,4 @@ +from .storage import Storage + +class LocalStorage(Storage): + pass diff --git a/backup_box/storage/storage.py b/backup_box/storage/storage.py new file mode 100644 index 0000000..c47f064 --- /dev/null +++ b/backup_box/storage/storage.py @@ -0,0 +1,12 @@ +from abc import ABC, abstractmethod +from ..config import StorageItem + +class Storage(ABC): + @classmethod + def get_storage_type(cls) -> str: + return cls.__name__ + "Item" + + @classmethod + @abstractmethod + def from_config(cls, cfg: StorageItem) -> 'Storage': + ... diff --git a/backup_box/tui.py b/backup_box/tui.py index 176adbb..d778892 100644 --- a/backup_box/tui.py +++ b/backup_box/tui.py @@ -1 +1 @@ -# tui \ No newline at end of file +# tui