def get_config(args: typing.Optional[Namespace] = None, modname: str = PROGRAM_NAME) -> AttrDict: """Get the config dict, layering env and args over defaults.""" config = Configuration(PROGRAM_NAME, modname=modname, read=False) try: config.read() except Exception as exc: LOG.warning(exc) if args and args.comicbox and args.comicbox.config: config.set_file(args.comicbox.config) config.set_env() if args: config.set_args(args) ad = config.get(TEMPLATE) if not isinstance(ad, AttrDict): raise ValueError() if ad.comicbox.paths: ad.comicbox.paths = sorted(set(ad.comicbox.paths)) return ad.comicbox
def load_config() -> Configuration: template = { "bot_token": String(), "users_group_id": String(), "database": { "host": String(default="localhost"), "port": Integer(default=5432), "name": String(default="postgres"), "username": String(default="postgres"), "password": String(default=""), "timeout": Number(default=60.0), "wait": Number(default=30.0), "pool": { "minsize": Integer(default=2), "maxsize": Integer(default=10), "recycle": Integer(default=-1) } }, "pki": { "ca": Filename(default="certs/ca.crt"), "cert": Filename(default="certs/root.crt"), "pkey": Filename(default="certs/root.key"), "passphrase": String(default=None), "tls_auth": Filename(default="certs/ta.key") }, "server": { "host": String(default="127.0.0.1"), "port": Integer(default=1443) }, "default": { "max_devices": Integer(default=6) } } config = Configuration("VpnBot", __name__) config.set(load_secrets()) config.set_args(parse_args(), dots=True) log.info("Configuration loaded") return config.get(template)
class PrescConfig: """ Wrapper around a confuse Configuration object. This is used for managing config options in PRESC, including the global config. Attributes ---------- from_config : PrescConfig A PrescConfig instance to override. If None, the config is initialized to the default settings. """ def __init__(self, from_config=None): if from_config: self._config = LocalConfig(from_config.settings) else: self._config = Configuration("PRESC", read=False) self.reset_defaults() def reset_defaults(self): """Reset all options to their defaults.""" self._config.clear() self.update_from_file(DEFAULT_CONFIG_PATH) def update_from_file(self, file_path): """Override current settings with those in the given YAML file.""" self._config.set_file(str(file_path)) def set(self, settings): """Update one or more config options. These should be specified in a dict, either mirroring the nested structure of the configuration file, or as flat key-value pairs using dots to indicate nested namespaces. Examples -------- ``config.set({"report": {"title": "My Report", "author": "Me"}})`` ``config.set({"report.title": "My Report", "report.author": "Me"})`` """ if not isinstance(settings, dict): raise PrescError("Config settings must be specified in a dict") self._config.set_args(settings, dots=True) @property def settings(self): """Access the underlying confuse object.""" return self._config def dump(self): """Dump the current config in YAML format.""" return self._config.dump() # Make option access work on the PrescConfig: def __getitem__(self, key): return self._config.__getitem__(key) def get(self, template=None): # If template is None, defer to the underlying default arg. template_arg = {} if template: template_arg["template"] = template return self._config.get(**template_arg) def flatten(self): return self._config.flatten()