diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2023-06-08 01:38:58 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2023-06-08 01:38:58 +0300 |
commit | 3ae1c3b5a7196cd7f556579658244868026b5e11 (patch) | |
tree | ff040ba3437fedcf2f12cc814e68d06c5543b25a /src/home/config/config.py | |
parent | 5aad97192dc5941147d91d28486eaff5a61afeb0 (diff) |
save
Diffstat (limited to 'src/home/config/config.py')
-rw-r--r-- | src/home/config/config.py | 59 |
1 files changed, 40 insertions, 19 deletions
diff --git a/src/home/config/config.py b/src/home/config/config.py index 4681685..b79fecb 100644 --- a/src/home/config/config.py +++ b/src/home/config/config.py @@ -7,39 +7,50 @@ from os.path import join, isdir, isfile from typing import Optional, Any, MutableMapping from argparse import ArgumentParser from ..util import parse_addr +import _validators as validators -def _get_config_path(name: str) -> str: - formats = ['toml', 'yaml'] +_validators = {} + + +def _get_validator(name: str) -> Optional[callable]: + if hasattr(validators, f'{name}_validator'): + return getattr(validators, f'{name}_validator') + if name in _validators: + return _validators[name] + return None + - dirname = join(os.environ['HOME'], '.config', name) +def add_validator(name: str, f: callable): + _validators[name] = f - if isdir(dirname): - for fmt in formats: - filename = join(dirname, f'config.{fmt}') - if isfile(filename): - return filename - raise IOError(f'config not found in {dirname}') +def _get_config_path(name: str) -> str: + formats = ['toml', 'yaml'] + + dirnames = [ + join(os.environ['HOME'], '.config', 'homekit'), + '/etc/homekit' + ] - else: - filenames = [join(os.environ['HOME'], '.config', f'{name}.{format}') for format in formats] - for file in filenames: - if isfile(file): - return file + for dirname in dirnames: + if isdir(dirname): + for fmt in formats: + filename = join(dirname, f'{name}.{fmt}') + if isfile(filename): + return filename - raise IOError(f'config not found') + raise IOError(f'config \'{name}\' not found') -class ConfigStore: +class SingleConfig: data: MutableMapping[str, Any] - app_name: Optional[str] def __init__(self): self.data = {} - self.app_name = None - def load(self, name: Optional[str] = None, + def load(self, + name: Optional[str] = None, use_cli=True, parser: ArgumentParser = None): self.app_name = name @@ -126,6 +137,16 @@ class ConfigStore: return self.data.items() +class Config: + app_name: Optional[str] + + def __init__(self): + + self.app_name = None + + + + config = ConfigStore() |