reorganise code
This commit is contained in:
@ -34,14 +34,14 @@ if sys.version_info[0] >= 3: # pragma: no cover
|
||||
else:
|
||||
from inspect import getargspec
|
||||
|
||||
static_tuple = tuple()
|
||||
STATIC_TUPLE = tuple()
|
||||
|
||||
if sys.version_info[0] >= 3: # pragma: no cover
|
||||
xrange = range
|
||||
|
||||
|
||||
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',
|
||||
'make_dict', 'unwrap_from_path', 'read_only',
|
||||
'read_write', 'getowner', 'set_contexts'])
|
||||
@ -49,19 +49,55 @@ ALLOWED_CONST_LIST = ['_cons_not_equal']
|
||||
|
||||
|
||||
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):
|
||||
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 \
|
||||
not name.startswith('impl_') and \
|
||||
not name.startswith('cfgimpl_'):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
not name.startswith('cfgimpl_')
|
||||
|
||||
|
||||
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):
|
||||
raise ValueError(_('{0} must be a function').format(type_))
|
||||
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}"'
|
||||
).format(type_, key))
|
||||
for callbk in callbacks:
|
||||
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'))
|
||||
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)
|
||||
_validate_callback(callbk)
|
||||
|
||||
|
||||
#____________________________________________________________
|
||||
#
|
||||
|
||||
|
||||
class Base(object):
|
||||
__slots__ = ('_name',
|
||||
'_informations',
|
||||
@ -150,7 +155,7 @@ class Base(object):
|
||||
raise ValueError(_("invalid name: {0} for option").format(name))
|
||||
if not multi and default_multi is not None:
|
||||
raise ValueError(_("default_multi is set whereas multi is False"
|
||||
" in option: {0}").format(name))
|
||||
" in option: {0}").format(name))
|
||||
if multi is True:
|
||||
is_multi = True
|
||||
_multi = 0
|
||||
@ -314,9 +319,6 @@ class Base(object):
|
||||
def impl_getname(self):
|
||||
return self._name
|
||||
|
||||
def impl_is_multi(self):
|
||||
return getattr(self, '_multi', 1) != 1
|
||||
|
||||
def impl_is_readonly(self):
|
||||
return not isinstance(getattr(self, '_informations', dict()), dict)
|
||||
|
||||
@ -342,7 +344,7 @@ class Base(object):
|
||||
self._subdyn = subdyn
|
||||
|
||||
def impl_getrequires(self):
|
||||
return getattr(self, '_requires', static_tuple)
|
||||
return getattr(self, '_requires', STATIC_TUPLE)
|
||||
|
||||
def impl_get_callback(self):
|
||||
call = getattr(self, '_val_call', (None, None))[1]
|
||||
@ -488,6 +490,14 @@ class Option(OnlyOption):
|
||||
__slots__ = tuple()
|
||||
_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,
|
||||
index, submulti_index, opts, warnings_only,
|
||||
transitive):
|
||||
@ -1003,7 +1013,7 @@ class Option(OnlyOption):
|
||||
self._consistencies.pop(-1)
|
||||
|
||||
def _get_consistencies(self):
|
||||
return getattr(self, '_consistencies', static_tuple)
|
||||
return getattr(self, '_consistencies', STATIC_TUPLE)
|
||||
|
||||
def _has_consistencies(self):
|
||||
return hasattr(self, '_consistencies')
|
||||
|
Reference in New Issue
Block a user