reorganise option.py
This commit is contained in:
parent
2c5d376219
commit
e29e11b939
6 changed files with 127 additions and 119 deletions
|
@ -110,7 +110,7 @@ class DomainnameOption(Option):
|
|||
raise ValueError(_("invalid length (max {0})"
|
||||
"").format(part_name_length))
|
||||
|
||||
if self._get_extra('_allow_ip') is True:
|
||||
if self.impl_get_extra('_allow_ip') is True:
|
||||
try:
|
||||
IP('{0}/32'.format(value))
|
||||
except ValueError:
|
||||
|
@ -124,9 +124,9 @@ class DomainnameOption(Option):
|
|||
pass
|
||||
else:
|
||||
raise ValueError(_('must not be an IP'))
|
||||
part_name_length = self._get_len(self._get_extra('_dom_type'))
|
||||
if self._get_extra('_dom_type') == 'domainname':
|
||||
if not self._get_extra('_allow_without_dot') and not "." in value:
|
||||
part_name_length = self._get_len(self.impl_get_extra('_dom_type'))
|
||||
if self.impl_get_extra('_dom_type') == 'domainname':
|
||||
if not self.impl_get_extra('_allow_without_dot') and not "." in value:
|
||||
raise ValueError(_("must have dot"))
|
||||
if len(value) > 255:
|
||||
raise ValueError(_("invalid length (max 255)"))
|
||||
|
@ -136,9 +136,9 @@ class DomainnameOption(Option):
|
|||
_valid_length(value)
|
||||
|
||||
def _second_level_validation(self, value, warnings_only):
|
||||
if self._get_extra('_has_upper').search(value):
|
||||
if self.impl_get_extra('_has_upper').search(value):
|
||||
raise ValueError(_('some characters are uppercase'))
|
||||
if not self._get_extra('_domain_re').search(value):
|
||||
if not self.impl_get_extra('_domain_re').search(value):
|
||||
if warnings_only:
|
||||
raise ValueError(_('some characters may cause problems'))
|
||||
else:
|
||||
|
|
|
@ -49,9 +49,9 @@ class IntOption(Option):
|
|||
**kwargs):
|
||||
if not isinstance(value, int):
|
||||
raise ValueError()
|
||||
min_number = self._get_extra('min_number')
|
||||
min_number = self.impl_get_extra('min_number')
|
||||
if min_number is not None and value < min_number:
|
||||
raise ValueError(_('value must be greater than "{0}"'.format(min_number)))
|
||||
max_number = self._get_extra('max_number')
|
||||
max_number = self.impl_get_extra('max_number')
|
||||
if max_number is not None and value > max_number:
|
||||
raise ValueError(_('value must be less than "{0}"'.format(max_number)))
|
||||
|
|
|
@ -84,13 +84,13 @@ class IPOption(Option):
|
|||
value,
|
||||
warnings_only):
|
||||
ip = IP('{0}/32'.format(value))
|
||||
if not self._get_extra('_allow_reserved') and ip.iptype() == 'RESERVED':
|
||||
if not self.impl_get_extra('_allow_reserved') and ip.iptype() == 'RESERVED':
|
||||
if warnings_only:
|
||||
msg = _("shouldn't in reserved class")
|
||||
else:
|
||||
msg = _("mustn't be in reserved class")
|
||||
raise ValueError(msg)
|
||||
if self._get_extra('_private_only') and ip.iptype() != 'PRIVATE':
|
||||
if self.impl_get_extra('_private_only') and ip.iptype() != 'PRIVATE':
|
||||
if warnings_only:
|
||||
msg = _("should be in private class")
|
||||
else:
|
||||
|
|
|
@ -161,12 +161,63 @@ class Option(OnlyOption):
|
|||
self._impl_set_callback(callback,
|
||||
callback_params)
|
||||
|
||||
#__________________________________________________________________________
|
||||
# option's information
|
||||
|
||||
def impl_is_multi(self):
|
||||
return getattr(self, '_multi', 1) != 1
|
||||
|
||||
def impl_is_submulti(self):
|
||||
return getattr(self, '_multi', 1) == 2
|
||||
|
||||
def impl_is_unique(self):
|
||||
return getattr(self, '_unique', False)
|
||||
|
||||
def impl_allow_empty_list(self):
|
||||
return getattr(self, '_allow_empty_list', undefined)
|
||||
|
||||
def impl_is_dynsymlinkoption(self):
|
||||
return False
|
||||
|
||||
def impl_getdoc(self):
|
||||
"accesses the Option's doc"
|
||||
return self.impl_get_information('doc')
|
||||
|
||||
def impl_getdefault(self):
|
||||
"accessing the default value"
|
||||
is_multi = self.impl_is_multi()
|
||||
default = getattr(self, '_default', undefined)
|
||||
if default is undefined:
|
||||
if is_multi:
|
||||
default = []
|
||||
else:
|
||||
default = None
|
||||
else:
|
||||
if is_multi:
|
||||
default = list(default)
|
||||
return default
|
||||
|
||||
def impl_getdefault_multi(self):
|
||||
"accessing the default value for a multi"
|
||||
if self.impl_is_submulti():
|
||||
default_value = []
|
||||
else:
|
||||
default_value = None
|
||||
return getattr(self, '_default_multi', default_value)
|
||||
|
||||
def impl_get_extra(self,
|
||||
key):
|
||||
extra = getattr(self, '_extra', {})
|
||||
if isinstance(extra, tuple):
|
||||
if key in extra[0]:
|
||||
return extra[1][extra[0].index(key)]
|
||||
return None
|
||||
else:
|
||||
return extra.get(key)
|
||||
|
||||
#__________________________________________________________________________
|
||||
# validator
|
||||
|
||||
def impl_get_validator(self):
|
||||
val = getattr(self, '_val_call', (None,))[0]
|
||||
if val is None:
|
||||
|
@ -308,44 +359,6 @@ class Option(OnlyOption):
|
|||
if err_msg:
|
||||
msg += ', {}'.format(err_msg)
|
||||
raise ValueError(msg)
|
||||
|
||||
|
||||
def impl_is_dynsymlinkoption(self):
|
||||
return False
|
||||
|
||||
def impl_is_master_slaves(self, type_='both'):
|
||||
master_slaves = self.impl_get_master_slaves()
|
||||
if master_slaves is not None:
|
||||
if type_ in ('both', 'master') and \
|
||||
master_slaves.is_master(self):
|
||||
return True
|
||||
if type_ in ('both', 'slave') and \
|
||||
not master_slaves.is_master(self):
|
||||
return True
|
||||
return False
|
||||
|
||||
def impl_get_master_slaves(self):
|
||||
masterslave = getattr(self, '_master_slaves', None)
|
||||
if masterslave is None:
|
||||
return masterslave
|
||||
return masterslave()
|
||||
|
||||
def impl_getdoc(self):
|
||||
"accesses the Option's doc"
|
||||
return self.impl_get_information('doc')
|
||||
|
||||
def _second_level_validation(self,
|
||||
value,
|
||||
warnings_only):
|
||||
pass
|
||||
|
||||
def impl_getdefault_multi(self):
|
||||
"accessing the default value for a multi"
|
||||
if self.impl_is_submulti():
|
||||
default_value = []
|
||||
else:
|
||||
default_value = None
|
||||
return getattr(self, '_default_multi', default_value)
|
||||
|
||||
def _validate_calculator(self,
|
||||
callback,
|
||||
|
@ -364,35 +377,31 @@ class Option(OnlyOption):
|
|||
raise ValueError(_('default value not allowed if option "{0}" '
|
||||
'is calculated').format(self.impl_getname()))
|
||||
|
||||
def impl_getdefault(self):
|
||||
"accessing the default value"
|
||||
is_multi = self.impl_is_multi()
|
||||
default = getattr(self, '_default', undefined)
|
||||
if default is undefined:
|
||||
if is_multi:
|
||||
default = []
|
||||
else:
|
||||
default = None
|
||||
else:
|
||||
if is_multi:
|
||||
default = list(default)
|
||||
return default
|
||||
def _second_level_validation(self,
|
||||
value,
|
||||
warnings_only):
|
||||
pass
|
||||
|
||||
def _get_extra(self,
|
||||
key):
|
||||
extra = getattr(self, '_extra', {})
|
||||
if isinstance(extra, tuple):
|
||||
if key in extra[0]:
|
||||
return extra[1][extra[0].index(key)]
|
||||
return None
|
||||
else:
|
||||
return extra.get(key)
|
||||
#__________________________________________________________________________
|
||||
# master/slaves
|
||||
|
||||
def impl_is_submulti(self):
|
||||
return getattr(self, '_multi', 1) == 2
|
||||
def impl_is_master_slaves(self, type_='both'):
|
||||
master_slaves = self.impl_get_master_slaves()
|
||||
if master_slaves is not None:
|
||||
if type_ in ('both', 'master') and \
|
||||
master_slaves.is_master(self):
|
||||
return True
|
||||
if type_ in ('both', 'slave') and \
|
||||
not master_slaves.is_master(self):
|
||||
return True
|
||||
return False
|
||||
|
||||
def impl_get_master_slaves(self):
|
||||
masterslave = getattr(self, '_master_slaves', None)
|
||||
if masterslave is None:
|
||||
return masterslave
|
||||
return masterslave()
|
||||
|
||||
def impl_allow_empty_list(self):
|
||||
return getattr(self, '_allow_empty_list', undefined)
|
||||
#____________________________________________________________
|
||||
# consistencies
|
||||
|
||||
|
@ -452,44 +461,6 @@ class Option(OnlyOption):
|
|||
self._add_dependency(opt)
|
||||
opt._add_dependency(self)
|
||||
|
||||
def _valid_consistencies(self,
|
||||
other_opts,
|
||||
init=True,
|
||||
func=None):
|
||||
if self.issubdyn():
|
||||
dynod = self.getsubdyn()
|
||||
else:
|
||||
dynod = None
|
||||
if self.impl_is_submulti():
|
||||
raise ConfigError(_('cannot add consistency with submulti option'))
|
||||
is_multi = self.impl_is_multi()
|
||||
for opt in other_opts:
|
||||
if isinstance(opt, weakref.ReferenceType):
|
||||
opt = opt()
|
||||
assert not opt.impl_is_submulti(), _('cannot add consistency with submulti option')
|
||||
assert isinstance(opt, Option), _('consistency must be set with an option, not {}').format(opt)
|
||||
if opt.issubdyn():
|
||||
if dynod is None:
|
||||
raise ConfigError(_('almost one option in consistency is '
|
||||
'in a dynoptiondescription but not all'))
|
||||
subod = opt.getsubdyn()
|
||||
if dynod != subod:
|
||||
raise ConfigError(_('option in consistency must be in same'
|
||||
' dynoptiondescription'))
|
||||
dynod = subod
|
||||
elif dynod is not None:
|
||||
raise ConfigError(_('almost one option in consistency is in a '
|
||||
'dynoptiondescription but not all'))
|
||||
if self is opt:
|
||||
raise ConfigError(_('cannot add consistency with itself'))
|
||||
if is_multi != opt.impl_is_multi():
|
||||
raise ConfigError(_('every options in consistency must be '
|
||||
'multi or none'))
|
||||
if init:
|
||||
# FIXME
|
||||
if func != 'not_equal':
|
||||
opt._has_dependency = True
|
||||
|
||||
def _add_consistency(self,
|
||||
func,
|
||||
all_cons_opts,
|
||||
|
@ -504,7 +475,7 @@ class Option(OnlyOption):
|
|||
def get_consistencies(self):
|
||||
return getattr(self, '_consistencies', STATIC_TUPLE)
|
||||
|
||||
def _has_consistencies(self, context):
|
||||
def has_consistencies(self, context):
|
||||
descr = context.cfgimpl_get_description()
|
||||
if descr._cache_consistencies is None:
|
||||
return False
|
||||
|
@ -566,6 +537,44 @@ class Option(OnlyOption):
|
|||
if fromconsistency_is_empty:
|
||||
option_bag.fromconsistency = []
|
||||
|
||||
def _valid_consistencies(self,
|
||||
other_opts,
|
||||
init=True,
|
||||
func=None):
|
||||
if self.issubdyn():
|
||||
dynod = self.getsubdyn()
|
||||
else:
|
||||
dynod = None
|
||||
if self.impl_is_submulti():
|
||||
raise ConfigError(_('cannot add consistency with submulti option'))
|
||||
is_multi = self.impl_is_multi()
|
||||
for opt in other_opts:
|
||||
if isinstance(opt, weakref.ReferenceType):
|
||||
opt = opt()
|
||||
assert not opt.impl_is_submulti(), _('cannot add consistency with submulti option')
|
||||
assert isinstance(opt, Option), _('consistency must be set with an option, not {}').format(opt)
|
||||
if opt.issubdyn():
|
||||
if dynod is None:
|
||||
raise ConfigError(_('almost one option in consistency is '
|
||||
'in a dynoptiondescription but not all'))
|
||||
subod = opt.getsubdyn()
|
||||
if dynod != subod:
|
||||
raise ConfigError(_('option in consistency must be in same'
|
||||
' dynoptiondescription'))
|
||||
dynod = subod
|
||||
elif dynod is not None:
|
||||
raise ConfigError(_('almost one option in consistency is in a '
|
||||
'dynoptiondescription but not all'))
|
||||
if self is opt:
|
||||
raise ConfigError(_('cannot add consistency with itself'))
|
||||
if is_multi != opt.impl_is_multi():
|
||||
raise ConfigError(_('every options in consistency must be '
|
||||
'multi or none'))
|
||||
if init:
|
||||
# FIXME
|
||||
if func != 'not_equal':
|
||||
opt._has_dependency = True
|
||||
|
||||
def launch_consistency(self,
|
||||
current_opt: OnlyOption,
|
||||
func: Callable,
|
||||
|
@ -715,7 +724,6 @@ class Option(OnlyOption):
|
|||
raise ValueError(msg.format(display_list(list(equal_name))))
|
||||
|
||||
|
||||
|
||||
class RegexpOption(Option):
|
||||
__slots__ = tuple()
|
||||
|
||||
|
|
|
@ -103,7 +103,7 @@ class PortOption(Option):
|
|||
if isinstance(value, int):
|
||||
value = str(value)
|
||||
self._impl_valid_string(value)
|
||||
if self._get_extra('_allow_range') and ":" in str(value):
|
||||
if self.impl_get_extra('_allow_range') and ":" in str(value):
|
||||
value = str(value).split(':')
|
||||
if len(value) != 2:
|
||||
raise ValueError(_('range must have two values only'))
|
||||
|
@ -117,7 +117,7 @@ class PortOption(Option):
|
|||
if not self.port_re.search(val):
|
||||
raise ValueError()
|
||||
val = int(val)
|
||||
if not self._get_extra('_min_value') <= val <= self._get_extra('_max_value'):
|
||||
if not self.impl_get_extra('_min_value') <= val <= self.impl_get_extra('_max_value'):
|
||||
raise ValueError(_('must be an integer between {0} '
|
||||
'and {1}').format(self._get_extra('_min_value'),
|
||||
self._get_extra('_max_value')))
|
||||
'and {1}').format(self.impl_get_extra('_min_value'),
|
||||
self.impl_get_extra('_max_value')))
|
||||
|
|
|
@ -253,7 +253,7 @@ class Values(object):
|
|||
context = option_bag.config_bag.context
|
||||
owner = self.get_context_owner()
|
||||
if 'validator' in option_bag.config_bag.properties:
|
||||
if option_bag.index is not None or option_bag.option._has_consistencies(context):
|
||||
if option_bag.index is not None or option_bag.option.has_consistencies(context):
|
||||
# set value to a fake config when option has dependency
|
||||
# validation will be complet in this case (consistency, ...)
|
||||
tested_context = context._gen_fake_values()
|
||||
|
|
Loading…
Reference in a new issue