adjust config format
This commit is contained in:
@@ -6,6 +6,7 @@ from typing import TypedDict, Literal
|
|||||||
|
|
||||||
EntryItem = TypedDict("EntryItem", {
|
EntryItem = TypedDict("EntryItem", {
|
||||||
"name": str,
|
"name": str,
|
||||||
|
"storage": str,
|
||||||
"sources": list[str],
|
"sources": list[str],
|
||||||
"included": list[str],
|
"included": list[str],
|
||||||
"ignored": list[str],
|
"ignored": list[str],
|
||||||
@@ -25,16 +26,16 @@ RemoteStorageItem = TypedDict("RemoteStorageItem", {
|
|||||||
StorageItem = LocalStorageItem | RemoteStorageItem
|
StorageItem = LocalStorageItem | RemoteStorageItem
|
||||||
|
|
||||||
ConfigDict = TypedDict("ConfigDict", {
|
ConfigDict = TypedDict("ConfigDict", {
|
||||||
"entry": dict[str, EntryItem],
|
|
||||||
"storage": dict[str, StorageItem],
|
"storage": dict[str, StorageItem],
|
||||||
|
"entry": dict[str, EntryItem],
|
||||||
})
|
})
|
||||||
|
|
||||||
CONFIG_FILE_NAME = "backup_box.toml"
|
CONFIG_FILE_NAME = "backup_box.toml"
|
||||||
|
|
||||||
def _new_config() -> ConfigDict:
|
def _new_config() -> ConfigDict:
|
||||||
return {
|
return {
|
||||||
"entry": {},
|
|
||||||
"storage": {},
|
"storage": {},
|
||||||
|
"entry": {},
|
||||||
}
|
}
|
||||||
|
|
||||||
_cfg: ConfigDict = _new_config()
|
_cfg: ConfigDict = _new_config()
|
||||||
@@ -50,27 +51,14 @@ def _override_dict(target: ConfigDict, top_dict: ConfigDict):
|
|||||||
def _path_related_to(base: str, *sub: str):
|
def _path_related_to(base: str, *sub: str):
|
||||||
return _pth.abspath(_pth.join(base, *sub))
|
return _pth.abspath(_pth.join(base, *sub))
|
||||||
|
|
||||||
def apply_user_config(dir_or_path: str):
|
def load_single_config(cfg_path: str) -> ConfigDict:
|
||||||
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
|
|
||||||
try:
|
try:
|
||||||
with open(dir_or_path, "rb") as f:
|
with open(cfg_path, "rb") as f:
|
||||||
config: ConfigDict = toml_load(f) # type: ignore
|
config: ConfigDict = toml_load(f) # type: ignore
|
||||||
except:
|
except:
|
||||||
config: ConfigDict = {} # type: ignore
|
config: ConfigDict = {} # type: ignore
|
||||||
# ensure default value
|
# ensure default value
|
||||||
config.setdefault("entry", {})
|
cfg_dir = _pth.dirname(cfg_path)
|
||||||
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", [])
|
|
||||||
config.setdefault("storage", {})
|
config.setdefault("storage", {})
|
||||||
for item in config["storage"].values():
|
for item in config["storage"].values():
|
||||||
if item["type"] == "LocalStorageItem":
|
if item["type"] == "LocalStorageItem":
|
||||||
@@ -78,6 +66,29 @@ def apply_user_config(dir_or_path: str):
|
|||||||
# path related to the config file.
|
# path related to the config file.
|
||||||
if item["path"]:
|
if item["path"]:
|
||||||
item["path"] = _path_related_to(cfg_dir, 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
|
# update config
|
||||||
_override_dict(_cfg, config)
|
_override_dict(_cfg, config)
|
||||||
# append config file list
|
# append config file list
|
||||||
@@ -99,7 +110,3 @@ def reload_config():
|
|||||||
|
|
||||||
def get_config():
|
def get_config():
|
||||||
return _cfg
|
return _cfg
|
||||||
|
|
||||||
# find config yaml
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user