Better support for connecting dictionary parameters

This commit is contained in:
allegroai 2019-06-16 02:27:47 +03:00
parent fa2022a87a
commit 3112769ad9

View File

@ -27,11 +27,15 @@ class _Arguments(object):
class _ProxyDictReadOnly(dict): class _ProxyDictReadOnly(dict):
""" Dictionary wrapper that prevents modifications to the dictionary """ """ Dictionary wrapper that prevents modifications to the dictionary """
def __init__(self, *args, **kwargs): def __init__(self, arguments, *args, **kwargs):
super(_Arguments._ProxyDictReadOnly, self).__init__(*args, **kwargs) super(_Arguments._ProxyDictReadOnly, self).__init__(*args, **kwargs)
self._arguments = arguments
def __setitem__(self, key, value): def __setitem__(self, key, value):
pass if self._arguments:
param_dict = self._arguments.copy_to_dict({key: value})
value = param_dict.get(key, value)
super(_Arguments._ProxyDictReadOnly, self).__setitem__(key, value)
def __init__(self, task): def __init__(self, task):
super(_Arguments, self).__init__() super(_Arguments, self).__init__()
@ -219,13 +223,16 @@ class _Arguments(object):
if PY2 and not current_action.nargs: if PY2 and not current_action.nargs:
current_action.nargs = '?' current_action.nargs = '?'
else: else:
parent_parser.add_argument( # do not add parameters that do not exist in argparser, they might be the dict
'--%s' % k, # # add new parameters to arg parser
default=v, # parent_parser.add_argument(
type=type(v), # '--%s' % k,
required=False, # default=v,
help='Task parameter %s (default %s)' % (k, v), # type=type(v),
) # required=False,
# help='Task parameter %s (default %s)' % (k, v),
# )
pass
except ArgumentError: except ArgumentError:
pass pass
except Exception: except Exception:
@ -305,11 +312,11 @@ class _Arguments(object):
self._task.log.warning('Failed parsing task parameter %s=%s keeping default %s=%s' % self._task.log.warning('Failed parsing task parameter %s=%s keeping default %s=%s' %
(str(k), str(param), str(k), str(v))) (str(k), str(param), str(k), str(v)))
continue continue
# add missing parameters to dictionary # add missing parameters to dictionary
for k, v in parameters.items(): # for k, v in parameters.items():
if k not in dictionary: # if k not in dictionary:
dictionary[k] = v # dictionary[k] = v
if not isinstance(dictionary, self._ProxyDictReadOnly): if not isinstance(dictionary, self._ProxyDictReadOnly):
return self._ProxyDictReadOnly(**dictionary) return self._ProxyDictReadOnly(self, **dictionary)
return dictionary return dictionary