Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
def read_config_file(filename):
logger.debug("Reading config file '{}'.".format(filename))
try:
config = Config(filename, configspec=cfg.split('\n'))
except (IOError, OSError, ConfigObjError) as error:
msg = "Failed to read configuration file '{}':\n{}"
raise ConfigError(msg.format(filename, error))
verification = config.verify()
if verification is not True:
logger.debug("Config file '{}' may contain invalid values.".format(
os.path.abspath(filename)))
if config.has_password():
check_and_fix_permissions(filename)
return config
def load_config(root=None, local=False):
if root is None:
root = os.getcwd()
config = Config(configspec=cfg.split('\n'))
if local:
for fn in _search_local(root):
tmp = read_config_file(fn)
config.merge(tmp)
if 'project' in tmp:
config['project_dir'] = os.path.dirname(fn)
break
else:
for fn in search_standard_dirs():
config.merge(read_config_file(fn))
for fn in search_tree(root):
tmp = read_config_file(fn)
config.merge(tmp)
if 'project' in tmp:
config['project_dir'] = os.path.dirname(fn)
break
def get_config(infile=None, configspec=None, * args, **kwargs):
if configspec is None:
configspec = cfg.split('\n')
return Config(infile, configspec=configspec, *args, **kwargs)
mode = ' local '
elif args.globalcfg:
mode = ' global '
else:
mode = ''
_print_err("Did not find a{}configuration file.".format(mode))
return
for key in args.key:
for kt in key.split('.'):
cfg = cfg.get(kt)
if cfg is None:
break
if not isinstance(cfg, Section):
print(cfg)
else:
for line in config.Config(cfg).write():
print(_hide_password(line))
def update_version_key(project, version):
config = Config()
fn_config = os.path.join(project.root_directory(), 'signac.rc')
try:
config.read(fn_config)
except FileNotFoundError:
pass
config['signac_version'] = version
config.verify()
config.write(fn_config)
update_hostcfg(
password=None, password_config=None)
else:
update_hostcfg(
password=None, password_config=parse_pwhash(pwhash))
elif args.password:
requires_username()
if args.password is True:
new_pw = prompt_password()
else:
new_pw = args.password
update_hostcfg(password=new_pw, password_config=None)
_print_err("Configured host '{}':".format(args.hostname))
print("[hosts]")
for line in config.Config({args.hostname: hostcfg()}).write():
print(_hide_password(line))
def verify(self, validator=None, *args, **kwargs):
if validator is None:
validator = get_validator()
return super(Config, self).validate(validator, *args, **kwargs)