Load/Save
This example builds on Config/Data. It loads the config if it exists otherwise it returns the default config.
Code
import json
from .default import get_config_file
from .model import Config
def _get_resolved_filename(filename):
filename = Path(filename) if filename else get_config_file()
return filename.expanduser()
def load(filename=None):
filename = _get_resolved_filename(filename)
if not filename.exists():
return Config()
with open(filename, "r") as f:
data = json.load(f)
return Config(**data)
def save(cfg, filename=None):
filename = _get_resolved_filename(filename)
with open(filename, "w") as f:
json.dump(cfg.to_dict(), f)
return True