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)
def __init__( self, key, config: confuse.Configuration, log_root: Log = None, **kwargs, ): """Set base variables. Mainly the descriptions """ # Sets logging super().__init__(log_root=log_root) log = self.log log.debug(f"{__name__=}") self.key: str = key self.config_cf: confuse.Configuration = config # Override the YAML with a dictionary # https://confuse.readthedocs.io/en/latest/ # https://www.pythoncentral.io/how-to-check-if-a-list-tuple-or-dictionary-is-empty-in-python/ if kwargs: for k, v in kwargs.items(): if "index" in k: k = k.split("_")[0] ind = [ i for i in config["Model"][self.key]["index"].get() if f"({k})" in i ][0] args = {"Dimension": {ind: v}} else: args = {"Model": {self.key: {k: v}}} config.set_args(args, dots=True) self.data_cf: confuse.Configuration = config["Model"][key] self.dimension_cf: confuse.Configuration = config["Dimension"] self.index_cf: confuse.Configuration = self.data_cf["index"] self._array: np.ndArray = None self._df: pd.DataFrame = None self._narrow: pd.DataFrame = None # if there is no value it is a calculate amount and fill with NaNs try: self._array = np.array(self.data_cf["array"].get()) # user create exceptions must include the import module name # https://www.geeksforgeeks.org/user-defined-exceptions-python-examples/ except confuse.NotFoundError: log.debug(f"set null array based on {self.index_cf.get()=}") shape = [ len(self.dimension_cf[x].get()) for x in self.index_cf.get() ] log.debug(f"of {shape=}") self._array = np.empty( [len(self.dimension_cf[x].get()) for x in self.index_cf.get()]) log.debug(f"{self._array=}") self.set_df()
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()
def parse_args(config: confuse.Configuration) -> confuse.Configuration: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( "-r", "--repository", default=config["repository"].get(), help="Git repository URL", ) parser.add_argument("-b", "--branch", default=config["branch"].get("str"), help="Git branch") parser.add_argument( "-p", "--path", default=config["path"].get(), help="Path inside the git repository", ) default_replace = config["replace_existing"].get(bool) parser.add_argument( "-f", "--replace-existing", default=default_replace, action="store_true", help="Replace the existing file?", ) parser.add_argument( "-c", "--configuration-files", nargs="+", help="Configuration files to fetch", default=config["configuration_files"].get(list), ) update_gitignore = config["update_gitignore"].get(bool) parser.add_argument( "-u", "--update-gitignore", default=update_gitignore, action="store_true", help="Add configuration file to the .gitignore.", ) default_insecure = config["insecure"].get(bool) parser.add_argument( "-k", "--insecure", default=default_insecure, action="store_true", help="Accept self signed certificate?", ) parser.add_argument("--no-replace-existing", dest="replace_existing", action="store_false") default_verbose = config["verbose"].get(bool) parser.add_argument( "-v", "--verbose", default=default_verbose, action="store_true", help="Display additional information?", ) parser.add_argument("--no-verbose", dest="verbose", action="store_false") args = parser.parse_args() config.set_args(args) return config