From dde7ad33734ed31b1901767bef2f17f2eff8858e Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Wed, 14 Nov 2018 17:38:21 +0100 Subject: [PATCH] simplify tiramisu/option/dynoptiondescription.py --- tiramisu/config.py | 9 ++-- tiramisu/option/dynoptiondescription.py | 59 +++++++++++-------------- tiramisu/option/optiondescription.py | 4 +- 3 files changed, 34 insertions(+), 38 deletions(-) diff --git a/tiramisu/config.py b/tiramisu/config.py index b5037c7..89b0419 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -116,8 +116,11 @@ class SubConfig(object): for woption in option_bag.option._get_dependencies(self): option = woption() if option.impl_is_dynoptiondescription(): - for doption in option.get_syndynoptiondescriptions(option_bag.config_bag, - remove_none=True): + subpath = option.impl_getpath().rsplit('.', 1)[0] + for suffix in option.get_suffixes(option_bag.config_bag, + remove_none=True): + doption = option.to_dynoption(subpath, + suffix) doption_path = doption.impl_getpath() doption_bag = OptionBag() doption_bag.set_option(doption, @@ -133,7 +136,7 @@ class SubConfig(object): dynopt = option.getsubdyn() rootpath = dynopt.impl_getpath() subpaths = [rootpath] + option.impl_getpath()[len(rootpath) + 1:].split('.')[:-1] - for suffix in dynopt.impl_get_suffixes(option_bag.config_bag): + for suffix in dynopt.get_suffixes(option_bag.config_bag): subpath = '.'.join([subp + suffix for subp in subpaths]) doption = option.to_dynoption(subpath, suffix) diff --git a/tiramisu/option/dynoptiondescription.py b/tiramisu/option/dynoptiondescription.py index facbbbd..a1aa3c7 100644 --- a/tiramisu/option/dynoptiondescription.py +++ b/tiramisu/option/dynoptiondescription.py @@ -19,11 +19,13 @@ # the whole pypy projet is under MIT licence # ____________________________________________________________ import re +from typing import List, Callable from ..i18n import _ from .optiondescription import OptionDescription -from ..setting import groups, undefined +from .baseoption import BaseOption +from ..setting import ConfigBag, groups, undefined from ..error import ConfigError from ..autolib import carry_out_calculation @@ -34,13 +36,13 @@ NAME_REGEXP = re.compile(r'^[a-zA-Z\d\-_]*$') class DynOptionDescription(OptionDescription): def __init__(self, - name, - doc, - children, + name: str, + doc: str, + children: List[BaseOption], requires=None, properties=None, - callback=None, - callback_params=None): + callback: Callable=None, + callback_params=None) -> None: super().__init__(name, doc, @@ -49,30 +51,31 @@ class DynOptionDescription(OptionDescription): properties) # check children + set relation to this dynoptiondescription for child in children: - if isinstance(child, OptionDescription): - if child.impl_get_group_type() != groups.master: - raise ConfigError(_('cannot set optiondescription in a ' + if __debug__: + if isinstance(child, OptionDescription): + if child.impl_get_group_type() != groups.master: + raise ConfigError(_('cannot set optiondescription in a ' + 'dynoptiondescription')) + for chld in child.get_children(config_bag=undefined): + chld._setsubdyn(self) + if child.impl_is_symlinkoption(): + raise ConfigError(_('cannot set symlinkoption in a ' 'dynoptiondescription')) - for chld in child.get_children(config_bag=undefined): - chld._setsubdyn(self) - if child.impl_is_symlinkoption(): - raise ConfigError(_('cannot set symlinkoption in a ' - 'dynoptiondescription')) child._setsubdyn(self) # add callback self._impl_set_callback(callback, callback_params) def _validate_calculator(self, - callback, - callback_params): + callback: Callable, + callback_params) -> None: if callback is None: raise ConfigError(_('callback is mandatory for the dynoptiondescription "{}"' '').format(self.impl_get_display_name())) - def impl_get_suffixes(self, - config_bag, - remove_none=False): + def get_suffixes(self, + config_bag: ConfigBag, + remove_none: bool=False) -> List[str]: callback, callback_params = self.impl_get_callback() values = carry_out_calculation(self, callback, @@ -92,23 +95,13 @@ class DynOptionDescription(OptionDescription): self.impl_get_display_name())) else: values_.append(val) - values = values_ - if len(values) > len(set(values)): - extra_values = values.copy() - for val in set(values): + if len(values_) > len(set(values_)): + extra_values = values_.copy() + for val in set(values_): extra_values.remove(val) raise ValueError(_('DynOptionDescription callback return a list with multiple value ' '"{}"''').format(extra_values)) return values_ - def get_syndynoptiondescriptions(self, - config_bag, - remove_none=False): - subpath = self.impl_getpath().rsplit('.', 1)[0] - for suffix in self.impl_get_suffixes(config_bag, - remove_none=remove_none): - yield self.to_dynoption(subpath, - suffix) - - def impl_is_dynoptiondescription(self): + def impl_is_dynoptiondescription(self) -> bool: return True diff --git a/tiramisu/option/optiondescription.py b/tiramisu/option/optiondescription.py index f6a4d23..adcebe6 100644 --- a/tiramisu/option/optiondescription.py +++ b/tiramisu/option/optiondescription.py @@ -212,7 +212,7 @@ class OptionDescriptionWalk(CacheOptionDescription): if child.impl_is_dynoptiondescription(): cname = child.impl_getname() if name.startswith(cname): - for suffix in child.impl_get_suffixes(config_bag): + for suffix in child.get_suffixes(config_bag): if name == cname + suffix: return child.to_dynoption(subpath, suffix) @@ -230,7 +230,7 @@ class OptionDescriptionWalk(CacheOptionDescription): subpath = self.impl_getpath() for child in self._children[1]: if dyn and child.impl_is_dynoptiondescription(): - for suffix in child.impl_get_suffixes(config_bag): + for suffix in child.get_suffixes(config_bag): yield child.to_dynoption(subpath, suffix) else: