Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
def setUp_base_h5Store(self, request):
self._tmp_dir = TemporaryDirectory(prefix='signac_')
request.addfinalizer(self._tmp_dir.cleanup)
self._tmp_pr = os.path.join(self._tmp_dir.name, 'pr')
self._tmp_wd = os.path.join(self._tmp_dir.name, 'wd')
os.mkdir(self._tmp_pr)
self.config = signac.common.config.load_config()
self.project = self.project_class.init_project(
name='testing_test_project',
root=self._tmp_pr,
workspace=self._tmp_wd)
warnings.filterwarnings('ignore', category=DeprecationWarning, module='signac')
self._fn_store = os.path.join(self._tmp_dir.name, 'signac_data.h5')
self._fn_store_other = os.path.join(self._tmp_dir.name, 'other.h5')
def config_from_cfg(cfg):
cfile = io.StringIO('\n'.join(cfg))
return signac.common.config.get_config(cfile)
self.config['default_host'] = 'testing'
self.config['author'] = 'test_author'
self.config['author_email'] = 'testauthor@example.com'
self.config['project'] = 'testing_test_project'
self.config['project_dir'] = self._tmp_pr
self.config['workspace_dir'] = self._tmp_wd
self.config['filestorage_dir'] = self._tmp_fs
self.config['signac_version'] = signac.VERSION_TUPLE
self.project = signac.contrib.Project(config=self.config)
self.addCleanup(self.project.remove, force=True)
# supress any output
self.stdout = sys.stdout
self.cwd = os.getcwd()
os.chdir(self._tmp_dir.name)
self.addCleanup(self.switch_back_to_cwd)
signac.common.config.write_config(self.config, 'signac.rc')
self.addCleanup(self.project.remove, force=True)
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 get_config(args, for_writing=False):
try:
if args._global:
config_ = config.read_config_file(USER_GLOBAL)
elif args.config:
config_ = config.read_config_file(args.config)
elif for_writing:
config_ = config.read_config_file(USER_LOCAL)
else:
config_ = config.load_config()
except FileNotFoundError:
pass
return config_
def get_config(args, for_writing=False):
try:
if args._global:
config_ = config.read_config_file(USER_GLOBAL)
elif args.config:
config_ = config.read_config_file(args.config)
elif for_writing:
config_ = config.read_config_file(USER_LOCAL)
else:
config_ = config.load_config()
except FileNotFoundError:
pass
return config_
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 '{}.{}'.format(ns, key)
raise ConfigKeyError('flow.' + k)
else:
return default
def set_value(args):
config_ = get_config(args, for_writing=True)
try:
config_[args.name] = args.value
except config.IllegalKeyError as error:
msg = "'{}' does not seem to be a valid configuration key. "\
"Use '-f' or '--force' to ignore this warning."
raise ValueError(msg.format(args.name))
except config.IllegalArgumentError as error:
msg = "Value '{value}' for '{key}' is illegal. "\
"Possible values: '{choices}'."
key, value, choices = error.args
raise ValueError(msg.format(
key=args.name, value=args.value, choices=choices))
write_config(config_, args)