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 .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',

View file

@ -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)

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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)