diff options
Diffstat (limited to 'pyapd/config.py')
-rw-r--r-- | pyapd/config.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/pyapd/config.py b/pyapd/config.py new file mode 100644 index 0000000..7850eab --- /dev/null +++ b/pyapd/config.py @@ -0,0 +1,37 @@ +from contextlib import contextmanager +from typing import Optional + +import ruamel.yaml + +class Config(): + def __init__(self, config_fn: Optional[str] = None, **kw): + self.config_fn = config_fn + self.data = {} + if config_fn: + self.load() + + def __getattr__(self, name): + try: + return self.data[name] + except KeyError as e: + raise NameError(e) + + def load(self, config_fn: Optional[str] = None): + if config_fn: + self.config_fn = config_fn + yaml = ruamel.yaml.YAML() + if self.config_fn: + fnames = [self.config_fn] + else: + fnames = [ + 'pyapd_config.yaml', + '/etc/pyapd/pyapd.yaml', + ] + for fn in fnames: + try: + with open(fn, 'r') as fp: + self.data = yaml.load(fp) + self.config_fn = fn + break + except FileNotFoundError: + continue |