From 8ccfba167183cb949847ac1cd616ddb6fcf99e4f Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Mon, 2 Sep 2013 20:37:23 +0200 Subject: [PATCH] factorise Option and OptionDescription init --- tiramisu/option.py | 69 +++++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 43 deletions(-) diff --git a/tiramisu/option.py b/tiramisu/option.py index 9a2dd5d..f606536 100644 --- a/tiramisu/option.py +++ b/tiramisu/option.py @@ -98,6 +98,30 @@ class BaseOption(BaseInformation): """ __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): """set once and only once some attributes in the option, like `_name`. `_name` cannot be changed one the option and @@ -234,15 +258,8 @@ class Option(BaseOption): :param validator_args: the validator's parameters """ - 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) + super(Option, self).__init__(name, doc, requires, properties) self._multi = multi - self._consistencies = None if validator is not None: if type(validator) != FunctionType: raise TypeError(_("validator must be a function")) @@ -285,20 +302,6 @@ class Option(BaseOption): self._default_multi = default_multi self.impl_validate(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_): if context is not None: @@ -819,12 +822,7 @@ class OptionDescription(BaseOption): :param children: a list of options (including optiondescriptions) """ - if not valid_name(name): - raise ValueError(_("invalid name:" - " {0} for optiondescription").format(name)) - self._name = name - self._impl_informations = {} - self.impl_set_information('doc', doc) + super(OptionDescription, self).__init__(name, doc, requires, properties) child_names = [child._name for child in children] #better performance like this valid_child = copy(child_names) @@ -836,22 +834,7 @@ class OptionDescription(BaseOption): '{0}').format(child)) old = child self._children = (tuple(child_names), tuple(children)) - self._calc_properties, self._requires = validate_requires_arg(requires, self._name) 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 self._group_type = groups.default