From 06595b9ca96c2199fe51d49c5fd707e6865ae1a8 Mon Sep 17 00:00:00 2001 From: Dreagonmon <531486058@qq.com> Date: Thu, 6 Nov 2025 11:38:24 +0800 Subject: [PATCH] adjust config format --- backup_box/config.py | 51 +++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/backup_box/config.py b/backup_box/config.py index 0ef1637..1a6f33b 100644 --- a/backup_box/config.py +++ b/backup_box/config.py @@ -6,6 +6,7 @@ from typing import TypedDict, Literal EntryItem = TypedDict("EntryItem", { "name": str, + "storage": str, "sources": list[str], "included": list[str], "ignored": list[str], @@ -25,16 +26,16 @@ RemoteStorageItem = TypedDict("RemoteStorageItem", { StorageItem = LocalStorageItem | RemoteStorageItem ConfigDict = TypedDict("ConfigDict", { - "entry": dict[str, EntryItem], "storage": dict[str, StorageItem], + "entry": dict[str, EntryItem], }) CONFIG_FILE_NAME = "backup_box.toml" def _new_config() -> ConfigDict: return { - "entry": {}, "storage": {}, + "entry": {}, } _cfg: ConfigDict = _new_config() @@ -50,27 +51,14 @@ def _override_dict(target: ConfigDict, top_dict: ConfigDict): 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 _config: - return # skip if already added +def load_single_config(cfg_path: str) -> ConfigDict: try: - with open(dir_or_path, "rb") as f: + with open(cfg_path, "rb") as f: config: ConfigDict = toml_load(f) # type: ignore except: config: ConfigDict = {} # type: ignore # ensure default value - config.setdefault("entry", {}) - for item in config["entry"].values(): - item.setdefault("name", "") - # path related to the config file. - item.setdefault("sources", []) - for i, s in enumerate(item["sources"]): - item["sources"][i] = _path_related_to(cfg_dir, s) - item.setdefault("included", []) - item.setdefault("ignored", []) + cfg_dir = _pth.dirname(cfg_path) config.setdefault("storage", {}) for item in config["storage"].values(): if item["type"] == "LocalStorageItem": @@ -78,6 +66,29 @@ def apply_user_config(dir_or_path: str): # path related to the config file. if item["path"]: item["path"] = _path_related_to(cfg_dir, item["path"]) + config.setdefault("entry", {}) + for item in config["entry"].values(): + item.setdefault("name", "") + item.setdefault("storage", "") + # path related to the config file. + item.setdefault("sources", []) + for i, s in enumerate(item["sources"]): + item["sources"][i] = _path_related_to(cfg_dir, s) + item.setdefault("included", []) + item.setdefault("ignored", []) + return config + +def save_single_config(cfg_path: str, config: ConfigDict): + with open(cfg_path, "wb") as fp: + toml_dump(config, fp, indent=2) + +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)) + if dir_or_path in _config: + return # skip if already added + # ensure default value + config = load_single_config(dir_or_path) # update config _override_dict(_cfg, config) # append config file list @@ -99,7 +110,3 @@ def reload_config(): def get_config(): return _cfg - -# find config yaml - -