import pathlib
import os
from collections import defaultdict
import yaml
from typing import Dict, Union, Optional
# Find global config file
HOME = str(pathlib.Path.home())
GLOBALCONFIGFILEPATH = os.path.join(HOME, "ethoconfig/ethoconfig.yml")
MISSING_CONFIG_MESSAGE = "No config file found. Should be at ~/ethoconfig/ethoconfig.yml"
[docs]
def defaultify(d: Dict, defaultfactory=lambda: None) -> defaultdict:
"""Convert nested dict to defaultdict."""
if not isinstance(d, dict):
return d
return defaultdict(defaultfactory, {k: defaultify(v, defaultfactory) for k, v in d.items()})
def undefaultify(d: Union[Dict, defaultdict]) -> Dict:
if not isinstance(d, dict):
return d
return {k: undefaultify(v) for k, v in d.items()}
def saveconfig(filename: str, prot: Union[Dict, defaultdict]):
prot = undefaultify(prot)
with open(filename, "w") as f:
yaml.dump(prot, f)
def readconfig(filename: Optional[str] = None) -> defaultdict:
if filename is None:
filename = GLOBALCONFIGFILEPATH
if not os.path.exists(filename):
raise FileNotFoundError(MISSING_CONFIG_MESSAGE)
with open(filename, "r") as f:
config_dict = yaml.load(f.read(), Loader=yaml.FullLoader)
return defaultify(config_dict)