factorise Option and OptionDescription init

This commit is contained in:
Emmanuel Garette 2013-09-02 20:37:23 +02:00
parent f106f3ced7
commit 8ccfba1671

View file

@ -98,6 +98,30 @@ class BaseOption(BaseInformation):
""" """
__slots__ = ('_readonly', '_state_consistencies', '_state_requires') __slots__ = ('_readonly', '_state_consistencies', '_state_requires')
def __init__(self, name, doc, requires, properties):
if not valid_name(name):
raise ValueError(_("invalid name: {0} for option").format(name))
self._name = name
self._impl_informations = {}
self.impl_set_information('doc', doc)
self._calc_properties, self._requires = validate_requires_arg(
requires, self._name)
self._consistencies = None
if properties is None:
properties = tuple()
if not isinstance(properties, tuple):
raise TypeError(_('invalid properties type {0} for {1},'
' must be a tuple').format(
type(properties),
self._name))
if self._calc_properties is not None and properties is not tuple():
set_forbidden_properties = set(properties) & self._calc_properties
if set_forbidden_properties != frozenset():
raise ValueError('conflict: properties already set in '
'requirement {0}'.format(
list(set_forbidden_properties)))
self._properties = properties # 'hidden', 'disabled'...
def __setattr__(self, name, value): def __setattr__(self, name, value):
"""set once and only once some attributes in the option, """set once and only once some attributes in the option,
like `_name`. `_name` cannot be changed one the option and like `_name`. `_name` cannot be changed one the option and
@ -234,15 +258,8 @@ class Option(BaseOption):
:param validator_args: the validator's parameters :param validator_args: the validator's parameters
""" """
if not valid_name(name): super(Option, self).__init__(name, doc, requires, properties)
raise ValueError(_("invalid name: {0} for option").format(name))
self._name = name
self._impl_informations = {}
self.impl_set_information('doc', doc)
self._calc_properties, self._requires = validate_requires_arg(
requires, self._name)
self._multi = multi self._multi = multi
self._consistencies = None
if validator is not None: if validator is not None:
if type(validator) != FunctionType: if type(validator) != FunctionType:
raise TypeError(_("validator must be a function")) raise TypeError(_("validator must be a function"))
@ -285,20 +302,6 @@ class Option(BaseOption):
self._default_multi = default_multi self._default_multi = default_multi
self.impl_validate(default) self.impl_validate(default)
self._default = default self._default = default
if properties is None:
properties = tuple()
if not isinstance(properties, tuple):
raise TypeError(_('invalid properties type {0} for {1},'
' must be a tuple').format(
type(properties),
self._name))
if self._calc_properties is not None and properties is not tuple():
set_forbidden_properties = set(properties) & self._calc_properties
if set_forbidden_properties != frozenset():
raise ValueError('conflict: properties already set in '
'requirement {0}'.format(
list(set_forbidden_properties)))
self._properties = properties # 'hidden', 'disabled'...
def _launch_consistency(self, func, opt, vals, context, index, opt_): def _launch_consistency(self, func, opt, vals, context, index, opt_):
if context is not None: if context is not None:
@ -819,12 +822,7 @@ class OptionDescription(BaseOption):
:param children: a list of options (including optiondescriptions) :param children: a list of options (including optiondescriptions)
""" """
if not valid_name(name): super(OptionDescription, self).__init__(name, doc, requires, properties)
raise ValueError(_("invalid name:"
" {0} for optiondescription").format(name))
self._name = name
self._impl_informations = {}
self.impl_set_information('doc', doc)
child_names = [child._name for child in children] child_names = [child._name for child in children]
#better performance like this #better performance like this
valid_child = copy(child_names) valid_child = copy(child_names)
@ -836,22 +834,7 @@ class OptionDescription(BaseOption):
'{0}').format(child)) '{0}').format(child))
old = child old = child
self._children = (tuple(child_names), tuple(children)) self._children = (tuple(child_names), tuple(children))
self._calc_properties, self._requires = validate_requires_arg(requires, self._name)
self._cache_paths = None self._cache_paths = None
self._consistencies = None
if properties is None:
properties = tuple()
if not isinstance(properties, tuple):
raise TypeError(_('invalid properties type {0} for {1},'
' must be a tuple').format(type(properties),
self._name))
if self._calc_properties is not None and properties is not tuple():
set_forbidden_properties = set(properties) & self._calc_properties
if set_forbidden_properties != frozenset():
raise ValueError('conflict: properties already set in '
'requirement {0}'.format(
list(set_forbidden_properties)))
self._properties = properties # 'hidden', 'disabled'...
# the group_type is useful for filtering OptionDescriptions in a config # the group_type is useful for filtering OptionDescriptions in a config
self._group_type = groups.default self._group_type = groups.default