Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def error(self, message):
"""Logs error message if a logger is set, calls the error handler and raises a ParserError."""
self._logger.error(message)
if self._error_handler is not None:
with redirect_stderr(self._stderr):
self._error_handler(self, message)
raise ParserError(message)
if isinstance(v, list) and any([isinstance(x, SimpleNamespace) for x in v]):
cfg_dict[k] = [namespace_to_dict(x) for x in v]
elif isinstance(v, SimpleNamespace):
cfg_dict[k] = vars(v) # type: ignore
elif not (v is None and k in cfg_dict):
cfg_dict[k] = v
else:
kdict = cfg_dict
for num, kk in enumerate(ksplit[:len(ksplit)-1]):
if kk not in kdict or kdict[kk] is None:
kdict[kk] = {} # type: ignore
elif not isinstance(kdict[kk], dict):
raise ParserError('Conflicting namespace base: '+'.'.join(ksplit[:num+1]))
kdict = kdict[kk] # type: ignore
if ksplit[-1] in kdict and kdict[ksplit[-1]] is not None:
raise ParserError('Conflicting namespace base: '+k)
if isinstance(v, list) and any([isinstance(x, SimpleNamespace) for x in v]):
kdict[ksplit[-1]] = [namespace_to_dict(x) for x in v]
elif not (v is None and ksplit[-1] in kdict):
kdict[ksplit[-1]] = v
return cfg_dict
def __call__(self, parser, namespace, values, option_string=None):
"""Adds sub-command dest and parses sub-command arguments."""
subcommand = values[0]
arg_strings = values[1:]
# set the parser name
setattr(namespace, self.dest, subcommand)
# parse arguments
if subcommand in self._name_parser_map:
subparser = self._name_parser_map[subcommand]
subnamespace, unk = subparser._parse_known_args(arg_strings)
if unk:
raise ParserError('Unrecognized arguments: %s' % ' '.join(unk))
for key, value in vars(subnamespace).items():
setattr(namespace, subcommand+'.'+key, value)
"""
ext_vars, ext_codes = self.split_ext_vars(ext_vars)
fpath = None
fname = 'snippet'
snippet = jsonnet
try:
fpath = Path(jsonnet, mode=config_read_mode)
except:
pass
else:
fname = jsonnet(absolute=False) if isinstance(jsonnet, Path) else jsonnet
snippet = fpath.get_content()
try:
values = yaml.safe_load(_jsonnet.evaluate_snippet(fname, snippet, ext_vars=ext_vars, ext_codes=ext_codes))
except Exception as ex:
raise ParserError('Problems evaluating jsonnet "'+fname+'" :: '+str(ex))
if self._validator is not None:
self._validator.validate(values)
if with_meta and isinstance(values, dict) and fpath is not None:
values['__path__'] = fpath
return dict_to_namespace(values)
# Get sub-command parser
subcommand = None
if action.dest in cfg_dict and cfg_dict[action.dest] is not None:
subcommand = cfg_dict[action.dest]
else:
#for key in action._name_parser_map.keys():
for key in action.choices.keys():
if any([v.startswith(key+'.') for v in cfg_dict.keys()]):
subcommand = key
break
cfg_dict[action.dest] = subcommand
if subcommand in action._name_parser_map:
subparser = action._name_parser_map[subcommand]
else:
raise ParserError('Unknown sub-commad '+subcommand+' (choices: '+', '.join(action.choices)+')')
# merge environment variable values and default values
subnamespace = None
if env:
subnamespace = subparser.parse_env(defaults=defaults, nested=False, _skip_check=True)
elif defaults:
subnamespace = subparser.get_defaults(nested=False)
if subnamespace is not None:
for key, value in vars(subnamespace).items():
key = subcommand+'.'+key
if key not in cfg_dict:
cfg_dict[key] = value
try:
namespace, args = super()._parse_known_args(args, SimpleNamespace())
if len(args) > 0:
for action in self._actions:
if isinstance(action, ActionParser):
ns, args = action._parser._parse_known_args(args)
for key, val in vars(ns).items():
setattr(namespace, key, val)
if len(args) == 0:
break
if hasattr(namespace, _UNRECOGNIZED_ARGS_ATTR):
args.extend(getattr(namespace, _UNRECOGNIZED_ARGS_ATTR))
delattr(namespace, _UNRECOGNIZED_ARGS_ATTR)
return namespace, args
except (ArgumentError, ParserError):
err = sys.exc_info()[1]
self.error(str(err))