simplify tiramisu/option/syndynoptiondescription.py

This commit is contained in:
Emmanuel Garette 2018-11-13 22:44:42 +01:00
parent 974a178d4b
commit 872d6cd9c8
6 changed files with 72 additions and 67 deletions

View File

@ -1,6 +1,6 @@
from .optiondescription import OptionDescription from .optiondescription import OptionDescription
from .dynoptiondescription import DynOptionDescription from .dynoptiondescription import DynOptionDescription
from .syndynoptiondescription import SynDynOptionDescription from .syndynoptiondescription import SynDynOptionDescription, SynDynMasterSlaves
from .masterslaves import MasterSlaves from .masterslaves import MasterSlaves
from .baseoption import submulti from .baseoption import submulti
from .symlinkoption import SymLinkOption from .symlinkoption import SymLinkOption
@ -26,7 +26,7 @@ from .passwordoption import PasswordOption
__all__ = ('MasterSlaves', 'OptionDescription', 'DynOptionDescription', __all__ = ('MasterSlaves', 'OptionDescription', 'DynOptionDescription',
'SynDynOptionDescription', 'Option', 'SymLinkOption', 'SynDynOptionDescription', 'SynDynMasterSlaves', 'Option', 'SymLinkOption',
'DynSymLinkOption', 'ChoiceOption', 'BoolOption', 'DateOption', 'DynSymLinkOption', 'ChoiceOption', 'BoolOption', 'DateOption',
'IntOption', 'FloatOption', 'StrOption', 'UnicodeOption', 'IntOption', 'FloatOption', 'StrOption', 'UnicodeOption',
'IPOption', 'PortOption', 'NetworkOption', 'NetmaskOption', 'IPOption', 'PortOption', 'NetworkOption', 'NetmaskOption',

View File

@ -23,7 +23,6 @@ from types import FunctionType
import weakref import weakref
from inspect import signature from inspect import signature
from itertools import chain from itertools import chain
from typing import Union
from ..i18n import _ from ..i18n import _
@ -31,7 +30,6 @@ from ..setting import undefined
from ..error import ConfigError, display_list from ..error import ConfigError, display_list
from ..function import Params, ParamContext, ParamOption, ParamIndex from ..function import Params, ParamContext, ParamOption, ParamIndex
from .dynsymlinkoption import DynSymLinkOption from .dynsymlinkoption import DynSymLinkOption
from .syndynoptiondescription import SynDynOptionDescription
STATIC_TUPLE = frozenset() STATIC_TUPLE = frozenset()
@ -451,11 +449,7 @@ class BaseOption(Base):
def to_dynoption(self, def to_dynoption(self,
rootpath: str, rootpath: str,
suffix: str) -> Union[SynDynOptionDescription, DynSymLinkOption]: suffix: str) -> DynSymLinkOption:
if self.impl_is_optiondescription():
return SynDynOptionDescription(self,
rootpath,
suffix)
return DynSymLinkOption(self, return DynSymLinkOption(self,
rootpath, rootpath,
suffix) suffix)

View File

@ -26,7 +26,6 @@ from .optiondescription import OptionDescription
from ..setting import groups, undefined from ..setting import groups, undefined
from ..error import ConfigError from ..error import ConfigError
from ..autolib import carry_out_calculation from ..autolib import carry_out_calculation
from .syndynoptiondescription import SynDynOptionDescription
NAME_REGEXP = re.compile(r'^[a-zA-Z\d\-_]*$') NAME_REGEXP = re.compile(r'^[a-zA-Z\d\-_]*$')
@ -108,8 +107,7 @@ class DynOptionDescription(OptionDescription):
subpath = self.impl_getpath().rsplit('.', 1)[0] subpath = self.impl_getpath().rsplit('.', 1)[0]
for suffix in self.impl_get_suffixes(config_bag, for suffix in self.impl_get_suffixes(config_bag,
remove_none=remove_none): remove_none=remove_none):
yield SynDynOptionDescription(self, yield self.to_dynoption(subpath,
subpath,
suffix) suffix)
def impl_is_dynoptiondescription(self): def impl_is_dynoptiondescription(self):

View File

@ -26,6 +26,7 @@ from itertools import chain
from ..i18n import _ from ..i18n import _
from ..setting import groups, undefined, OptionBag from ..setting import groups, undefined, OptionBag
from .optiondescription import OptionDescription from .optiondescription import OptionDescription
from .syndynoptiondescription import SynDynMasterSlaves
from .option import Option from .option import Option
from ..error import SlaveError, PropertiesOptionError, RequirementError from ..error import SlaveError, PropertiesOptionError, RequirementError
from ..function import ParamOption from ..function import ParamOption
@ -225,3 +226,10 @@ class MasterSlaves(OptionDescription):
def impl_is_master_slaves(self, *args, **kwargs): def impl_is_master_slaves(self, *args, **kwargs):
return True return True
def to_dynoption(self,
rootpath: str,
suffix: str) -> SynDynMasterSlaves:
return SynDynMasterSlaves(self,
rootpath,
suffix)

View File

@ -26,7 +26,7 @@ from ..i18n import _
from ..setting import ConfigBag, OptionBag, groups, undefined, owners, Undefined from ..setting import ConfigBag, OptionBag, groups, undefined, owners, Undefined
from .baseoption import BaseOption from .baseoption import BaseOption
from .option import ALLOWED_CONST_LIST from .option import ALLOWED_CONST_LIST
from .syndynoptiondescription import SynDynOptionDescription from .syndynoptiondescription import SynDynOptionDescription, SynDynMasterSlaves
from ..error import ConfigError, ConflictError from ..error import ConfigError, ConflictError
@ -212,11 +212,10 @@ 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 value in child.impl_get_suffixes(config_bag): for suffix in child.impl_get_suffixes(config_bag):
if name == cname + value: if name == cname + suffix:
return SynDynOptionDescription(child, return child.to_dynoption(subpath,
subpath, suffix)
value)
raise AttributeError(_('unknown option "{0}" ' raise AttributeError(_('unknown option "{0}" '
'in optiondescription "{1}"' 'in optiondescription "{1}"'
'').format(name, self.impl_getname())) '').format(name, self.impl_getname()))
@ -232,8 +231,7 @@ class OptionDescriptionWalk(CacheOptionDescription):
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.impl_get_suffixes(config_bag):
yield SynDynOptionDescription(child, yield child.to_dynoption(subpath,
subpath,
suffix) suffix)
else: else:
yield child yield child
@ -340,3 +338,10 @@ class OptionDescription(OptionDescriptionWalk):
def impl_get_group_type(self) -> groups.GroupType: def impl_get_group_type(self) -> groups.GroupType:
return self._group_type return self._group_type
def to_dynoption(self,
rootpath: str,
suffix: str) -> SynDynOptionDescription:
return SynDynOptionDescription(self,
rootpath,
suffix)

View File

@ -18,12 +18,14 @@
# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/ # the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
# the whole pypy projet is under MIT licence # 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 ..i18n import _
from ..setting import ConfigBag, groups, undefined from ..setting import ConfigBag, groups, undefined, Settings
#from .baseoption import BaseOption from ..value import Values
from .baseoption import BaseOption
from .dynsymlinkoption import DynSymLinkOption
class SynDynOptionDescription(object): class SynDynOptionDescription(object):
@ -32,23 +34,26 @@ class SynDynOptionDescription(object):
'_suffix') '_suffix')
def __init__(self, def __init__(self,
opt, opt: BaseOption,
subpath, subpath: str,
suffix): suffix: str) -> None:
self._opt = opt self._opt = opt
self._subpath = subpath self._subpath = subpath
self._suffix = suffix self._suffix = suffix
def __getattr__(self, name): def __getattr__(self,
return getattr(self._opt, name) 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 return self._opt
def get_child(self, def get_child(self,
name: str, name: str,
config_bag: ConfigBag, config_bag: ConfigBag,
subpath: str): subpath: str) -> BaseOption:
#FIXME -> Union[BaseOption, SynDynOptionDescription]: #FIXME -> Union[BaseOption, SynDynOptionDescription]:
if name.endswith(self._suffix): if name.endswith(self._suffix):
oname = name[:-len(self._suffix)] oname = name[:-len(self._suffix)]
@ -64,58 +69,58 @@ class SynDynOptionDescription(object):
'in syndynoptiondescription "{1}"' 'in syndynoptiondescription "{1}"'
'').format(name, self.impl_getname())) '').format(name, self.impl_getname()))
def impl_getname(self): def impl_getname(self) -> str:
return self._opt.impl_getname() + self._suffix return self._opt.impl_getname() + self._suffix
def impl_is_dynoptiondescription(self): def impl_is_dynoptiondescription(self) -> bool:
return True return True
def get_children(self, def get_children(self,
config_bag, config_bag: ConfigBag,
dyn=True): dyn: bool=True):
children = []
subpath = self.impl_getpath() subpath = self.impl_getpath()
for child in self._opt.get_children(config_bag): for child in self._opt.get_children(config_bag):
yield child.to_dynoption(subpath, yield child.to_dynoption(subpath,
self._suffix) self._suffix)
def get_children_recursively(self, def get_children_recursively(self,
bytype, # FIXME : Optional[BaseOption], bytype: Optional[BaseOption],
byname: Optional[str], byname: Optional[str],
config_bag: ConfigBag, config_bag: ConfigBag,
self_opt=None): # FIXME : BaseOption=None) self_opt: BaseOption=None) -> BaseOption:
# FIXME -> Iterator[Union[BaseOption, SynDynOptionDescription]]: # FIXME -> Iterator[Union[BaseOption, SynDynOptionDescription]]:
return self._opt.get_children_recursively(bytype, return self._opt.get_children_recursively(bytype,
byname, byname,
config_bag, config_bag,
self) self)
def impl_getpath(self): def impl_getpath(self) -> str:
subpath = self._subpath subpath = self._subpath
if subpath != '': if subpath != '':
subpath += '.' subpath += '.'
return subpath + self.impl_getname() return subpath + self.impl_getname()
def getmaster(self): def impl_get_display_name(self) -> str:
master = self._opt.getmaster() return self._opt.impl_get_display_name() + self._suffix
return master.to_dynoption(self.impl_getpath(),
class SynDynMasterSlaves(SynDynOptionDescription):
def getmaster(self) -> DynSymLinkOption:
return self._opt.getmaster().to_dynoption(self.impl_getpath(),
self._suffix) self._suffix)
def getslaves(self): def getslaves(self) -> Iterator[DynSymLinkOption]:
subpath = self.impl_getpath() subpath = self.impl_getpath()
for slave in self._opt.getslaves(): for slave in self._opt.getslaves():
yield slave.to_dynoption(subpath, yield slave.to_dynoption(subpath,
self._suffix) self._suffix)
def impl_get_display_name(self):
return self._opt.impl_get_display_name() + self._suffix
def reset_cache(self, def reset_cache(self,
path, path: str,
values, values: Values,
settings, settings: Settings,
resetted_opts): resetted_opts: List[str]) -> None:
if self.impl_get_group_type() == groups.master:
master = self.getmaster() master = self.getmaster()
slaves = self.getslaves() slaves = self.getslaves()
self._reset_cache(path, self._reset_cache(path,
@ -124,15 +129,10 @@ class SynDynOptionDescription(object):
values, values,
settings, settings,
resetted_opts) resetted_opts)
else:
self._opt.reset_cache(path,
values,
settings,
resetted_opts)
def pop(self, def pop(self,
*args, *args,
**kwargs): **kwargs) -> None:
self._opt.pop(*args, self._opt.pop(*args,
slaves=self.getslaves(), slaves=self.getslaves(),
**kwargs) **kwargs)