validator's function can have 1 arg, 2 args or 3 args
This commit is contained in:
@ -25,7 +25,7 @@ from .setting import undefined
|
||||
|
||||
|
||||
def carry_out_calculation(option, context, callback, callback_params,
|
||||
index=undefined, value_index=None):
|
||||
index=undefined):
|
||||
"""a function that carries out a calculation for an option's value
|
||||
|
||||
:param option: the option
|
||||
@ -150,7 +150,7 @@ def carry_out_calculation(option, context, callback, callback_params,
|
||||
#Not an option, set full context
|
||||
tcparams.setdefault(key, []).append((context.duplicate(), False))
|
||||
elif callbk[0] == 'index':
|
||||
tcparams.setdefault(key, []).append((value_index, False))
|
||||
tcparams.setdefault(key, []).append((index, False))
|
||||
else:
|
||||
# callbk is something link (opt, True|False)
|
||||
opt, force_permissive = callbk
|
||||
@ -182,7 +182,7 @@ def carry_out_calculation(option, context, callback, callback_params,
|
||||
has_option = True
|
||||
value = list(value)
|
||||
|
||||
if opt.impl_is_master_slaves() and \
|
||||
if opt != option and opt.impl_is_master_slaves() and \
|
||||
opt.impl_get_master_slaves().in_same_group(option):
|
||||
master_slave = True
|
||||
is_multi = True
|
||||
|
@ -140,16 +140,8 @@ class Base(StorageBase):
|
||||
type(properties),
|
||||
name))
|
||||
if validator is not None:
|
||||
if validator_params is None:
|
||||
func_args = getargspec(validator)
|
||||
defaults = func_args.defaults
|
||||
if defaults is None:
|
||||
defaults = []
|
||||
args = func_args.args[0:len(func_args.args)-len(defaults)]
|
||||
if len(args) == 2:
|
||||
validator_params = {'': ((self, False),)}
|
||||
elif len(args) == 3:
|
||||
validator_params = {'': ((self, False), ('index',))}
|
||||
if multi: # and validator_params is None:
|
||||
validator_params = self._build_validator_params(validator, validator_params)
|
||||
|
||||
validate_callback(validator, validator_params, 'validator')
|
||||
self._set_validator(validator, validator_params)
|
||||
@ -176,6 +168,33 @@ class Base(StorageBase):
|
||||
self.impl_set_callback(callback, callback_params, _init=True)
|
||||
self.commit(session)
|
||||
|
||||
def _build_validator_params(self, validator, validator_params):
|
||||
func_args = getargspec(validator)
|
||||
defaults = func_args.defaults
|
||||
if defaults is None:
|
||||
defaults = []
|
||||
args = func_args.args[0:len(func_args.args)-len(defaults)]
|
||||
if validator_params is not None:
|
||||
len_args = len(validator_params.get('', []))
|
||||
if len_args != 0 and len(args) <= len_args:
|
||||
args = args[0:len(args)-len_args]
|
||||
if len(args) >= 2:
|
||||
if validator_params is not None and args[1] in validator_params:
|
||||
args = []
|
||||
elif validator_params is not None and '' in validator_params:
|
||||
params = list(validator_params[''])
|
||||
params.append((self, False))
|
||||
validator_params[''] = tuple(params)
|
||||
else:
|
||||
if validator_params is None:
|
||||
validator_params = {}
|
||||
validator_params[''] = ((self, False),)
|
||||
if len(args) == 3 and args[2] not in validator_params:
|
||||
params = list(validator_params[''])
|
||||
params.append(('index',))
|
||||
validator_params[''] = tuple(params)
|
||||
return validator_params
|
||||
|
||||
def _set_has_dependency(self):
|
||||
if not isinstance(self, SymLinkOption):
|
||||
self._has_dependency = True
|
||||
@ -545,7 +564,7 @@ class Option(OnlyOption):
|
||||
value = carry_out_calculation(current_opt, context=context,
|
||||
callback=validator,
|
||||
callback_params=validator_params_,
|
||||
value_index=_index)
|
||||
index=_index)
|
||||
if isinstance(value, Exception):
|
||||
return value
|
||||
|
||||
|
Reference in New Issue
Block a user