reorganise code

This commit is contained in:
Emmanuel Garette 2017-07-23 18:14:34 +02:00
parent 57a47763d6
commit 1d251374a1
3 changed files with 63 additions and 52 deletions

View File

@ -468,7 +468,7 @@ class SubConfig(object):
if isinstance(value, Exception): if isinstance(value, Exception):
if isinstance(value, PropertiesOptionError): if isinstance(value, PropertiesOptionError):
return False return False
raise value raise value # pragma: no cover
elif isinstance(value, Multi): elif isinstance(value, Multi):
return byvalue in value return byvalue in value
else: else:
@ -502,7 +502,7 @@ class SubConfig(object):
if isinstance(value, PropertiesOptionError): if isinstance(value, PropertiesOptionError):
continue continue
else: else:
raise value raise value # pragma: no cover
if type_ == 'value': if type_ == 'value':
retval = value retval = value
elif type_ == 'path': elif type_ == 'path':
@ -617,7 +617,7 @@ class SubConfig(object):
_setting_properties=setting_properties, _setting_properties=setting_properties,
returns_raise=True) returns_raise=True)
if isinstance(value, Exception): if isinstance(value, Exception):
if not isinstance(value, PropertiesOptionError): if not isinstance(value, PropertiesOptionError): # pragma: no cover
raise value raise value
else: else:
if opt.impl_is_optiondescription(): if opt.impl_is_optiondescription():

View File

@ -34,14 +34,14 @@ if sys.version_info[0] >= 3: # pragma: no cover
else: else:
from inspect import getargspec from inspect import getargspec
static_tuple = tuple() STATIC_TUPLE = tuple()
if sys.version_info[0] >= 3: # pragma: no cover if sys.version_info[0] >= 3: # pragma: no cover
xrange = range xrange = range
submulti = 2 submulti = 2
name_regexp = re.compile(r'^[a-z][a-zA-Z\d_]*$') NAME_REGEXP = re.compile(r'^[a-z][a-zA-Z\d_]*$')
FORBIDDEN_NAMES = frozenset(['iter_all', 'iter_group', 'find', 'find_first', FORBIDDEN_NAMES = frozenset(['iter_all', 'iter_group', 'find', 'find_first',
'make_dict', 'unwrap_from_path', 'read_only', 'make_dict', 'unwrap_from_path', 'read_only',
'read_write', 'getowner', 'set_contexts']) 'read_write', 'getowner', 'set_contexts'])
@ -49,19 +49,55 @@ ALLOWED_CONST_LIST = ['_cons_not_equal']
def valid_name(name): def valid_name(name):
"an option's name is a str and does not start with 'impl' or 'cfgimpl'" """an option's name is a str and does not start with 'impl' or 'cfgimpl'
and name is not a function name"""
if not isinstance(name, str): if not isinstance(name, str):
return False return False
if re.match(name_regexp, name) is not None and \ return re.match(NAME_REGEXP, name) is not None and \
name not in FORBIDDEN_NAMES and \ name not in FORBIDDEN_NAMES and \
not name.startswith('impl_') and \ not name.startswith('impl_') and \
not name.startswith('cfgimpl_'): not name.startswith('cfgimpl_')
return True
else:
return False
def validate_callback(callback, callback_params, type_, callbackoption): def validate_callback(callback, callback_params, type_, callbackoption):
"""validate function and parameter set for callback, validation, ...
"""
def _validate_option(option):
#validate option
if isinstance(option, SymLinkOption):
cur_opt = option._impl_getopt()
elif isinstance(option, Option):
cur_opt = option
else:
raise ValueError(_('{}_params must have an option'
' not a {} for first argument'
).format(type_, type(option)))
if cur_opt != callbackoption:
cur_opt._add_dependencies(callbackoption)
def _validate_force_permissive(force_permissive):
#validate force_permissive
if not isinstance(force_permissive, bool):
raise ValueError(_('{}_params must have a boolean'
' not a {} for second argument'
).format(type_, type(
force_permissive)))
def _validate_callback(callbk):
if isinstance(callbk, tuple):
if len(callbk) == 1:
if callbk not in ((None,), ('index',)):
raise ValueError(_('{0}_params with length of '
'tuple as 1 must only have '
'None as first value').format(type_))
return
elif len(callbk) != 2:
raise ValueError(_('{0}_params must only have 1 or 2 '
'as length').format(type_))
option, force_permissive = callbk
_validate_option(option)
_validate_force_permissive(force_permissive)
if not isinstance(callback, FunctionType): if not isinstance(callback, FunctionType):
raise ValueError(_('{0} must be a function').format(type_)) raise ValueError(_('{0} must be a function').format(type_))
if callback_params is not None: if callback_params is not None:
@ -76,42 +112,11 @@ def validate_callback(callback, callback_params, type_, callbackoption):
raise ValueError(_('{0}_params must be tuple for key "{1}"' raise ValueError(_('{0}_params must be tuple for key "{1}"'
).format(type_, key)) ).format(type_, key))
for callbk in callbacks: for callbk in callbacks:
if isinstance(callbk, tuple): _validate_callback(callbk)
if len(callbk) == 1:
if callbk not in ((None,), ('index',)):
raise ValueError(_('{0}_params with length of '
'tuple as 1 must only have '
'None as first value'))
elif len(callbk) != 2:
raise ValueError(_('{0}_params must only have 1 or 2 '
'as length'))
else:
option, force_permissive = callbk
if not isinstance(option, Option) and not \
isinstance(option, SymLinkOption):
raise ValueError(_('{}_params must have an option'
' not a {} for first argument'
).format(type_, type(option)))
if force_permissive not in [True, False]:
raise ValueError(_('{}_params must have a boolean'
' not a {} for second argument'
).format(type_, type(
force_permissive)))
if isinstance(option, SymLinkOption):
cur_opt = option._impl_getopt()
else:
cur_opt = option
if cur_opt != callbackoption:
if not getattr(cur_opt, '_dependencies', None):
options = set()
else:
options = set(cur_opt._dependencies)
options.add(callbackoption)
cur_opt._dependencies = tuple(options)
#____________________________________________________________ #____________________________________________________________
# #
class Base(object): class Base(object):
__slots__ = ('_name', __slots__ = ('_name',
'_informations', '_informations',
@ -150,7 +155,7 @@ class Base(object):
raise ValueError(_("invalid name: {0} for option").format(name)) raise ValueError(_("invalid name: {0} for option").format(name))
if not multi and default_multi is not None: if not multi and default_multi is not None:
raise ValueError(_("default_multi is set whereas multi is False" raise ValueError(_("default_multi is set whereas multi is False"
" in option: {0}").format(name)) " in option: {0}").format(name))
if multi is True: if multi is True:
is_multi = True is_multi = True
_multi = 0 _multi = 0
@ -314,9 +319,6 @@ class Base(object):
def impl_getname(self): def impl_getname(self):
return self._name return self._name
def impl_is_multi(self):
return getattr(self, '_multi', 1) != 1
def impl_is_readonly(self): def impl_is_readonly(self):
return not isinstance(getattr(self, '_informations', dict()), dict) return not isinstance(getattr(self, '_informations', dict()), dict)
@ -342,7 +344,7 @@ class Base(object):
self._subdyn = subdyn self._subdyn = subdyn
def impl_getrequires(self): def impl_getrequires(self):
return getattr(self, '_requires', static_tuple) return getattr(self, '_requires', STATIC_TUPLE)
def impl_get_callback(self): def impl_get_callback(self):
call = getattr(self, '_val_call', (None, None))[1] call = getattr(self, '_val_call', (None, None))[1]
@ -488,6 +490,14 @@ class Option(OnlyOption):
__slots__ = tuple() __slots__ = tuple()
_empty = '' _empty = ''
def impl_is_multi(self):
return getattr(self, '_multi', 1) != 1
def _add_dependencies(self, option):
options = set(getattr(self, '_dependencies', tuple()))
options.add(option)
self._dependencies = tuple(options)
def _launch_consistency(self, current_opt, func, option, value, context, def _launch_consistency(self, current_opt, func, option, value, context,
index, submulti_index, opts, warnings_only, index, submulti_index, opts, warnings_only,
transitive): transitive):
@ -1003,7 +1013,7 @@ class Option(OnlyOption):
self._consistencies.pop(-1) self._consistencies.pop(-1)
def _get_consistencies(self): def _get_consistencies(self):
return getattr(self, '_consistencies', static_tuple) return getattr(self, '_consistencies', STATIC_TUPLE)
def _has_consistencies(self): def _has_consistencies(self):
return hasattr(self, '_consistencies') return hasattr(self, '_consistencies')

View File

@ -414,7 +414,8 @@ class Settings(object):
is_cached, props = self._p_.getcache(path, ntime, index) is_cached, props = self._p_.getcache(path, ntime, index)
if not is_cached: if not is_cached:
props = self._p_.getproperties(path, opt.impl_getproperties()) props = self._p_.getproperties(path, opt.impl_getproperties())
if opt.impl_is_multi() and not opt.impl_is_master_slaves('slave'): if not opt.impl_is_optiondescription() and opt.impl_is_multi() and \
not opt.impl_is_master_slaves('slave'):
props.add('empty') props.add('empty')
if apply_requires: if apply_requires:
requires = self.apply_requires(opt, path, setting_properties, index, False) requires = self.apply_requires(opt, path, setting_properties, index, False)