simplify tiramisu/option/dynoptiondescription.py

This commit is contained in:
Emmanuel Garette 2018-11-14 17:38:21 +01:00
parent 872d6cd9c8
commit dde7ad3373
3 changed files with 34 additions and 38 deletions

View File

@ -116,8 +116,11 @@ class SubConfig(object):
for woption in option_bag.option._get_dependencies(self): for woption in option_bag.option._get_dependencies(self):
option = woption() option = woption()
if option.impl_is_dynoptiondescription(): if option.impl_is_dynoptiondescription():
for doption in option.get_syndynoptiondescriptions(option_bag.config_bag, subpath = option.impl_getpath().rsplit('.', 1)[0]
for suffix in option.get_suffixes(option_bag.config_bag,
remove_none=True): remove_none=True):
doption = option.to_dynoption(subpath,
suffix)
doption_path = doption.impl_getpath() doption_path = doption.impl_getpath()
doption_bag = OptionBag() doption_bag = OptionBag()
doption_bag.set_option(doption, doption_bag.set_option(doption,
@ -133,7 +136,7 @@ class SubConfig(object):
dynopt = option.getsubdyn() dynopt = option.getsubdyn()
rootpath = dynopt.impl_getpath() rootpath = dynopt.impl_getpath()
subpaths = [rootpath] + option.impl_getpath()[len(rootpath) + 1:].split('.')[:-1] 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]) subpath = '.'.join([subp + suffix for subp in subpaths])
doption = option.to_dynoption(subpath, doption = option.to_dynoption(subpath,
suffix) suffix)

View File

@ -19,11 +19,13 @@
# the whole pypy projet is under MIT licence # the whole pypy projet is under MIT licence
# ____________________________________________________________ # ____________________________________________________________
import re import re
from typing import List, Callable
from ..i18n import _ from ..i18n import _
from .optiondescription import OptionDescription from .optiondescription import OptionDescription
from ..setting import groups, undefined from .baseoption import BaseOption
from ..setting import ConfigBag, groups, undefined
from ..error import ConfigError from ..error import ConfigError
from ..autolib import carry_out_calculation from ..autolib import carry_out_calculation
@ -34,13 +36,13 @@ NAME_REGEXP = re.compile(r'^[a-zA-Z\d\-_]*$')
class DynOptionDescription(OptionDescription): class DynOptionDescription(OptionDescription):
def __init__(self, def __init__(self,
name, name: str,
doc, doc: str,
children, children: List[BaseOption],
requires=None, requires=None,
properties=None, properties=None,
callback=None, callback: Callable=None,
callback_params=None): callback_params=None) -> None:
super().__init__(name, super().__init__(name,
doc, doc,
@ -49,6 +51,7 @@ class DynOptionDescription(OptionDescription):
properties) properties)
# check children + set relation to this dynoptiondescription # check children + set relation to this dynoptiondescription
for child in children: for child in children:
if __debug__:
if isinstance(child, OptionDescription): if isinstance(child, OptionDescription):
if child.impl_get_group_type() != groups.master: if child.impl_get_group_type() != groups.master:
raise ConfigError(_('cannot set optiondescription in a ' raise ConfigError(_('cannot set optiondescription in a '
@ -64,15 +67,15 @@ class DynOptionDescription(OptionDescription):
callback_params) callback_params)
def _validate_calculator(self, def _validate_calculator(self,
callback, callback: Callable,
callback_params): callback_params) -> None:
if callback is None: if callback is None:
raise ConfigError(_('callback is mandatory for the dynoptiondescription "{}"' raise ConfigError(_('callback is mandatory for the dynoptiondescription "{}"'
'').format(self.impl_get_display_name())) '').format(self.impl_get_display_name()))
def impl_get_suffixes(self, def get_suffixes(self,
config_bag, config_bag: ConfigBag,
remove_none=False): remove_none: bool=False) -> List[str]:
callback, callback_params = self.impl_get_callback() callback, callback_params = self.impl_get_callback()
values = carry_out_calculation(self, values = carry_out_calculation(self,
callback, callback,
@ -92,23 +95,13 @@ class DynOptionDescription(OptionDescription):
self.impl_get_display_name())) self.impl_get_display_name()))
else: else:
values_.append(val) values_.append(val)
values = values_ if len(values_) > len(set(values_)):
if len(values) > len(set(values)): extra_values = values_.copy()
extra_values = values.copy() for val in set(values_):
for val in set(values):
extra_values.remove(val) extra_values.remove(val)
raise ValueError(_('DynOptionDescription callback return a list with multiple value ' raise ValueError(_('DynOptionDescription callback return a list with multiple value '
'"{}"''').format(extra_values)) '"{}"''').format(extra_values))
return values_ return values_
def get_syndynoptiondescriptions(self, def impl_is_dynoptiondescription(self) -> bool:
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):
return True return True

View File

@ -212,7 +212,7 @@ class OptionDescriptionWalk(CacheOptionDescription):
if child.impl_is_dynoptiondescription(): if child.impl_is_dynoptiondescription():
cname = child.impl_getname() cname = child.impl_getname()
if name.startswith(cname): 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: if name == cname + suffix:
return child.to_dynoption(subpath, return child.to_dynoption(subpath,
suffix) suffix)
@ -230,7 +230,7 @@ class OptionDescriptionWalk(CacheOptionDescription):
subpath = self.impl_getpath() subpath = self.impl_getpath()
for child in self._children[1]: for child in self._children[1]:
if dyn and child.impl_is_dynoptiondescription(): 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, yield child.to_dynoption(subpath,
suffix) suffix)
else: else: