def get_config_value(key, ns=None, default=None): """Request a value from the user's configuration. :param key: The configuration key. :type key: str :param ns: The namespace in which to look for the key. :type key: str :param default: A default value in case the key cannot be found within the user's configuration. :return: The value if found, None if not found. """ try: if ns is None: return config.load_config()['flow'][key] else: return config.load_config()['flow'][ns][key] except KeyError: return default
def _import_configured_environments(): cfg = config.load_config(config.FN_CONFIG) try: for name in cfg['flow'].as_list('environment_modules'): try: importlib.import_module(name) except ImportError as e: logger.warning(e) except KeyError: pass
def require_config_value(key, ns=None, default=_GET_CONFIG_VALUE_NONE): """Request a value from the user's configuration, fail if not available. :param key: The environment specific configuration key. :type key: str :param ns: The namespace in which to look for the key. :type ns: str :param default: A default value in case the key cannot be found within the user's configuration. :return: The value or default value. :raises ConfigKeyError: If the key is not in the user's configuration and no default value is provided. """ try: if ns is None: return config.load_config()["flow"][key] else: return config.load_config()["flow"][ns][key] except KeyError: if default is _GET_CONFIG_VALUE_NONE: k = str(key) if ns is None else f"{ns}.{key}" raise ConfigKeyError("flow." + k) else: return default
def require_config_value(key, ns=None, default=_GET_CONFIG_VALUE_NONE): """Request a value from the user's configuration, failing if not available. Parameters ---------- key : str The environment specific configuration key. ns : str The namespace in which to look for the key. (Default value = None) default A default value in case the key cannot be found within the user's configuration. Returns ------- object The value or default value. Raises ------ :class:`~.ConfigKeyError` If the key is not in the user's configuration and no default value is provided. """ try: if ns is None: return config.load_config()["flow"][key] else: return config.load_config()["flow"][ns][key] except KeyError: if default is _GET_CONFIG_VALUE_NONE: k = str(key) if ns is None else f"{ns}.{key}" raise ConfigKeyError("flow." + k) else: return default
def test_config_show(self): err = self.call('python -m signac config --local show'.split(), error=True).strip() assert 'Did not find a local configuration file' in err self.call('python -m signac init my_project'.split()) out = self.call('python -m signac config --local show'.split()).strip() cfg = config.read_config_file('signac.rc') expected = config.Config(cfg).write() assert out.split(os.linesep) == expected out = self.call('python -m signac config show'.split()).strip() cfg = config.load_config() expected = config.Config(cfg).write() assert out.split(os.linesep) == expected out = self.call( 'python -m signac config --global show'.split()).strip() cfg = config.read_config_file(config.FN_CONFIG) expected = config.Config(cfg).write() assert out.split(os.linesep) == expected