From 872d6cd9c8f05a8876f1883c054853b32224679e Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Tue, 13 Nov 2018 22:44:42 +0100 Subject: [PATCH] simplify tiramisu/option/syndynoptiondescription.py --- tiramisu/option/__init__.py | 4 +- tiramisu/option/baseoption.py | 8 +- tiramisu/option/dynoptiondescription.py | 6 +- tiramisu/option/masterslaves.py | 8 ++ tiramisu/option/optiondescription.py | 23 +++--- tiramisu/option/syndynoptiondescription.py | 90 +++++++++++----------- 6 files changed, 72 insertions(+), 67 deletions(-) diff --git a/tiramisu/option/__init__.py b/tiramisu/option/__init__.py index a2c295c..50437eb 100644 --- a/tiramisu/option/__init__.py +++ b/tiramisu/option/__init__.py @@ -1,6 +1,6 @@ from .optiondescription import OptionDescription from .dynoptiondescription import DynOptionDescription -from .syndynoptiondescription import SynDynOptionDescription +from .syndynoptiondescription import SynDynOptionDescription, SynDynMasterSlaves from .masterslaves import MasterSlaves from .baseoption import submulti from .symlinkoption import SymLinkOption @@ -26,7 +26,7 @@ from .passwordoption import PasswordOption __all__ = ('MasterSlaves', 'OptionDescription', 'DynOptionDescription', - 'SynDynOptionDescription', 'Option', 'SymLinkOption', + 'SynDynOptionDescription', 'SynDynMasterSlaves', 'Option', 'SymLinkOption', 'DynSymLinkOption', 'ChoiceOption', 'BoolOption', 'DateOption', 'IntOption', 'FloatOption', 'StrOption', 'UnicodeOption', 'IPOption', 'PortOption', 'NetworkOption', 'NetmaskOption', diff --git a/tiramisu/option/baseoption.py b/tiramisu/option/baseoption.py index cfb3c3a..c8b2a2a 100644 --- a/tiramisu/option/baseoption.py +++ b/tiramisu/option/baseoption.py @@ -23,7 +23,6 @@ from types import FunctionType import weakref from inspect import signature from itertools import chain -from typing import Union from ..i18n import _ @@ -31,7 +30,6 @@ from ..setting import undefined from ..error import ConfigError, display_list from ..function import Params, ParamContext, ParamOption, ParamIndex from .dynsymlinkoption import DynSymLinkOption -from .syndynoptiondescription import SynDynOptionDescription STATIC_TUPLE = frozenset() @@ -451,11 +449,7 @@ class BaseOption(Base): def to_dynoption(self, rootpath: str, - suffix: str) -> Union[SynDynOptionDescription, DynSymLinkOption]: - if self.impl_is_optiondescription(): - return SynDynOptionDescription(self, - rootpath, - suffix) + suffix: str) -> DynSymLinkOption: return DynSymLinkOption(self, rootpath, suffix) diff --git a/tiramisu/option/dynoptiondescription.py b/tiramisu/option/dynoptiondescription.py index e08da95..facbbbd 100644 --- a/tiramisu/option/dynoptiondescription.py +++ b/tiramisu/option/dynoptiondescription.py @@ -26,7 +26,6 @@ from .optiondescription import OptionDescription from ..setting import groups, undefined from ..error import ConfigError from ..autolib import carry_out_calculation -from .syndynoptiondescription import SynDynOptionDescription NAME_REGEXP = re.compile(r'^[a-zA-Z\d\-_]*$') @@ -108,9 +107,8 @@ class DynOptionDescription(OptionDescription): subpath = self.impl_getpath().rsplit('.', 1)[0] for suffix in self.impl_get_suffixes(config_bag, remove_none=remove_none): - yield SynDynOptionDescription(self, - subpath, - suffix) + yield self.to_dynoption(subpath, + suffix) def impl_is_dynoptiondescription(self): return True diff --git a/tiramisu/option/masterslaves.py b/tiramisu/option/masterslaves.py index 7a51ebf..79645d2 100644 --- a/tiramisu/option/masterslaves.py +++ b/tiramisu/option/masterslaves.py @@ -26,6 +26,7 @@ from itertools import chain from ..i18n import _ from ..setting import groups, undefined, OptionBag from .optiondescription import OptionDescription +from .syndynoptiondescription import SynDynMasterSlaves from .option import Option from ..error import SlaveError, PropertiesOptionError, RequirementError from ..function import ParamOption @@ -225,3 +226,10 @@ class MasterSlaves(OptionDescription): def impl_is_master_slaves(self, *args, **kwargs): return True + + def to_dynoption(self, + rootpath: str, + suffix: str) -> SynDynMasterSlaves: + return SynDynMasterSlaves(self, + rootpath, + suffix) diff --git a/tiramisu/option/optiondescription.py b/tiramisu/option/optiondescription.py index ebc7867..f6a4d23 100644 --- a/tiramisu/option/optiondescription.py +++ b/tiramisu/option/optiondescription.py @@ -26,7 +26,7 @@ from ..i18n import _ from ..setting import ConfigBag, OptionBag, groups, undefined, owners, Undefined from .baseoption import BaseOption from .option import ALLOWED_CONST_LIST -from .syndynoptiondescription import SynDynOptionDescription +from .syndynoptiondescription import SynDynOptionDescription, SynDynMasterSlaves from ..error import ConfigError, ConflictError @@ -212,11 +212,10 @@ class OptionDescriptionWalk(CacheOptionDescription): if child.impl_is_dynoptiondescription(): cname = child.impl_getname() if name.startswith(cname): - for value in child.impl_get_suffixes(config_bag): - if name == cname + value: - return SynDynOptionDescription(child, - subpath, - value) + for suffix in child.impl_get_suffixes(config_bag): + if name == cname + suffix: + return child.to_dynoption(subpath, + suffix) raise AttributeError(_('unknown option "{0}" ' 'in optiondescription "{1}"' '').format(name, self.impl_getname())) @@ -232,9 +231,8 @@ class OptionDescriptionWalk(CacheOptionDescription): for child in self._children[1]: if dyn and child.impl_is_dynoptiondescription(): for suffix in child.impl_get_suffixes(config_bag): - yield SynDynOptionDescription(child, - subpath, - suffix) + yield child.to_dynoption(subpath, + suffix) else: yield child @@ -340,3 +338,10 @@ class OptionDescription(OptionDescriptionWalk): def impl_get_group_type(self) -> groups.GroupType: return self._group_type + + def to_dynoption(self, + rootpath: str, + suffix: str) -> SynDynOptionDescription: + return SynDynOptionDescription(self, + rootpath, + suffix) diff --git a/tiramisu/option/syndynoptiondescription.py b/tiramisu/option/syndynoptiondescription.py index 660a89e..5a6484a 100644 --- a/tiramisu/option/syndynoptiondescription.py +++ b/tiramisu/option/syndynoptiondescription.py @@ -18,12 +18,14 @@ # the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/ # the whole pypy projet is under MIT licence # ____________________________________________________________ -from typing import Optional, Iterator, Union +from typing import Optional, Iterator, Union, Any, List from ..i18n import _ -from ..setting import ConfigBag, groups, undefined -#from .baseoption import BaseOption +from ..setting import ConfigBag, groups, undefined, Settings +from ..value import Values +from .baseoption import BaseOption +from .dynsymlinkoption import DynSymLinkOption class SynDynOptionDescription(object): @@ -32,23 +34,26 @@ class SynDynOptionDescription(object): '_suffix') def __init__(self, - opt, - subpath, - suffix): + opt: BaseOption, + subpath: str, + suffix: str) -> None: self._opt = opt self._subpath = subpath self._suffix = suffix - def __getattr__(self, name): - return getattr(self._opt, name) + def __getattr__(self, + name: str) -> Any: + # if not in SynDynOptionDescription, get value in self._opt + return getattr(self._opt, + name) - def impl_getopt(self): + def impl_getopt(self) -> BaseOption: return self._opt def get_child(self, name: str, config_bag: ConfigBag, - subpath: str): + subpath: str) -> BaseOption: #FIXME -> Union[BaseOption, SynDynOptionDescription]: if name.endswith(self._suffix): oname = name[:-len(self._suffix)] @@ -64,75 +69,70 @@ class SynDynOptionDescription(object): 'in syndynoptiondescription "{1}"' '').format(name, self.impl_getname())) - def impl_getname(self): + def impl_getname(self) -> str: return self._opt.impl_getname() + self._suffix - def impl_is_dynoptiondescription(self): + def impl_is_dynoptiondescription(self) -> bool: return True def get_children(self, - config_bag, - dyn=True): - children = [] + config_bag: ConfigBag, + dyn: bool=True): subpath = self.impl_getpath() for child in self._opt.get_children(config_bag): yield child.to_dynoption(subpath, self._suffix) def get_children_recursively(self, - bytype, # FIXME : Optional[BaseOption], + bytype: Optional[BaseOption], byname: Optional[str], config_bag: ConfigBag, - self_opt=None): # FIXME : BaseOption=None) + self_opt: BaseOption=None) -> BaseOption: # FIXME -> Iterator[Union[BaseOption, SynDynOptionDescription]]: return self._opt.get_children_recursively(bytype, byname, config_bag, self) - def impl_getpath(self): + def impl_getpath(self) -> str: subpath = self._subpath if subpath != '': subpath += '.' return subpath + self.impl_getname() - def getmaster(self): - master = self._opt.getmaster() - return master.to_dynoption(self.impl_getpath(), - self._suffix) + def impl_get_display_name(self) -> str: + return self._opt.impl_get_display_name() + self._suffix - def getslaves(self): + +class SynDynMasterSlaves(SynDynOptionDescription): + + def getmaster(self) -> DynSymLinkOption: + return self._opt.getmaster().to_dynoption(self.impl_getpath(), + self._suffix) + + def getslaves(self) -> Iterator[DynSymLinkOption]: subpath = self.impl_getpath() for slave in self._opt.getslaves(): yield slave.to_dynoption(subpath, self._suffix) - def impl_get_display_name(self): - return self._opt.impl_get_display_name() + self._suffix - def reset_cache(self, - path, - values, - settings, - resetted_opts): - if self.impl_get_group_type() == groups.master: - master = self.getmaster() - slaves = self.getslaves() - self._reset_cache(path, - master, - slaves, - values, - settings, - resetted_opts) - else: - self._opt.reset_cache(path, - values, - settings, - resetted_opts) + path: str, + values: Values, + settings: Settings, + resetted_opts: List[str]) -> None: + master = self.getmaster() + slaves = self.getslaves() + self._reset_cache(path, + master, + slaves, + values, + settings, + resetted_opts) def pop(self, *args, - **kwargs): + **kwargs) -> None: self._opt.pop(*args, slaves=self.getslaves(), **kwargs)