From 1d251374a15b312263ed89b39ac7d90da74fc643 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sun, 23 Jul 2017 18:14:34 +0200 Subject: [PATCH] reorganise code --- tiramisu/config.py | 6 +- tiramisu/option/baseoption.py | 106 +++++++++++++++++++--------------- tiramisu/setting.py | 3 +- 3 files changed, 63 insertions(+), 52 deletions(-) diff --git a/tiramisu/config.py b/tiramisu/config.py index 4252d87..6ab6474 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -468,7 +468,7 @@ class SubConfig(object): if isinstance(value, Exception): if isinstance(value, PropertiesOptionError): return False - raise value + raise value # pragma: no cover elif isinstance(value, Multi): return byvalue in value else: @@ -502,7 +502,7 @@ class SubConfig(object): if isinstance(value, PropertiesOptionError): continue else: - raise value + raise value # pragma: no cover if type_ == 'value': retval = value elif type_ == 'path': @@ -617,7 +617,7 @@ class SubConfig(object): _setting_properties=setting_properties, returns_raise=True) if isinstance(value, Exception): - if not isinstance(value, PropertiesOptionError): + if not isinstance(value, PropertiesOptionError): # pragma: no cover raise value else: if opt.impl_is_optiondescription(): diff --git a/tiramisu/option/baseoption.py b/tiramisu/option/baseoption.py index ddb4cf8..7e1f78c 100644 --- a/tiramisu/option/baseoption.py +++ b/tiramisu/option/baseoption.py @@ -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') diff --git a/tiramisu/setting.py b/tiramisu/setting.py index df40a6f..d77ef5a 100644 --- a/tiramisu/setting.py +++ b/tiramisu/setting.py @@ -414,7 +414,8 @@ class Settings(object): is_cached, props = self._p_.getcache(path, ntime, index) if not is_cached: 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') if apply_requires: requires = self.apply_requires(opt, path, setting_properties, index, False)