adjust config format
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user