simplify tiramisu/option/syndynoption.py

This commit is contained in:
Emmanuel Garette 2018-11-14 21:51:40 +01:00
parent 5d26762761
commit 184a086bc1
11 changed files with 112 additions and 108 deletions

View File

@ -8,7 +8,7 @@ from tiramisu import BoolOption, StrOption, ChoiceOption, IPOption, \
NetworkOption, NetmaskOption, IntOption, FloatOption, \ NetworkOption, NetmaskOption, IntOption, FloatOption, \
UnicodeOption, PortOption, BroadcastOption, DomainnameOption, \ UnicodeOption, PortOption, BroadcastOption, DomainnameOption, \
EmailOption, URLOption, UsernameOption, FilenameOption, SymLinkOption, \ EmailOption, URLOption, UsernameOption, FilenameOption, SymLinkOption, \
OptionDescription, DynOptionDescription, DynSymLinkOption, submulti, MasterSlaves, \ OptionDescription, DynOptionDescription, SynDynOption, submulti, MasterSlaves, \
Config, Params, ParamOption, ParamValue Config, Params, ParamOption, ParamValue
from tiramisu.error import PropertiesOptionError, ConfigError, ConflictError from tiramisu.error import PropertiesOptionError, ConfigError, ConflictError
from tiramisu.storage import list_sessions from tiramisu.storage import list_sessions
@ -687,11 +687,11 @@ def test_find_dyndescription_context():
api = Config(od2) api = Config(od2)
api.option('od.dodval1.stval1').value.set('yes') api.option('od.dodval1.stval1').value.set('yes')
assert api.option.find('stval1', first=True).value.get() == "yes" assert api.option.find('stval1', first=True).value.get() == "yes"
assert isinstance(api.option.find('stval1', first=True).option.get(), DynSymLinkOption) assert isinstance(api.option.find('stval1', first=True).option.get(), SynDynOption)
#assert api.option.find(bytype=StrOption, type='path') == ['od.dodval1.stval1', 'od.dodval2.stval2', 'od.val1'] #assert api.option.find(bytype=StrOption, type='path') == ['od.dodval1.stval1', 'od.dodval2.stval2', 'od.val1']
#opts = api.option.find(byvalue='yes') #opts = api.option.find(byvalue='yes')
#assert len(opts) == 1 #assert len(opts) == 1
#assert isinstance(opts[0], DynSymLinkOption) #assert isinstance(opts[0], SynDynOption)
#assert opts[0].impl_getname() == 'stval1' #assert opts[0].impl_getname() == 'stval1'
raises(AttributeError, "list(api.option.find('strnotexists'))") raises(AttributeError, "list(api.option.find('strnotexists'))")

View File

@ -48,7 +48,7 @@ def manager_callback(callbk: Union[ParamOption, ParamValue],
force_settings=get_default_settings_storages()) force_settings=get_default_settings_storages())
opt = callbk.option opt = callbk.option
if opt.issubdyn(): if opt.issubdyn():
opt = opt.to_dynoption(option._rootpath, opt = opt.to_dynoption(option.rootpath,
option.impl_getsuffix()) option.impl_getsuffix())
path = opt.impl_getpath() path = opt.impl_getpath()
if index is not None and opt.impl_is_master_slaves() and \ if index is not None and opt.impl_is_master_slaves() and \

View File

@ -4,7 +4,7 @@ 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
from .dynsymlinkoption import DynSymLinkOption from .syndynoption import SynDynOption
from .option import Option, RegexpOption from .option import Option, RegexpOption
from .choiceoption import ChoiceOption from .choiceoption import ChoiceOption
from .booloption import BoolOption from .booloption import BoolOption
@ -27,7 +27,7 @@ from .passwordoption import PasswordOption
__all__ = ('MasterSlaves', 'OptionDescription', 'DynOptionDescription', __all__ = ('MasterSlaves', 'OptionDescription', 'DynOptionDescription',
'SynDynOptionDescription', 'SynDynMasterSlaves', 'Option', 'SymLinkOption', 'SynDynOptionDescription', 'SynDynMasterSlaves', 'Option', 'SymLinkOption',
'DynSymLinkOption', 'ChoiceOption', 'BoolOption', 'DateOption', 'SynDynOption', 'ChoiceOption', 'BoolOption', 'DateOption',
'IntOption', 'FloatOption', 'StrOption', 'UnicodeOption', 'IntOption', 'FloatOption', 'StrOption', 'UnicodeOption',
'IPOption', 'PortOption', 'NetworkOption', 'NetmaskOption', 'IPOption', 'PortOption', 'NetworkOption', 'NetmaskOption',
'BroadcastOption', 'DomainnameOption', 'EmailOption', 'URLOption', 'BroadcastOption', 'DomainnameOption', 'EmailOption', 'URLOption',

View File

@ -29,7 +29,6 @@ from ..i18n import _
from ..setting import undefined 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
STATIC_TUPLE = frozenset() STATIC_TUPLE = frozenset()
@ -447,13 +446,6 @@ class BaseOption(Base):
def impl_is_symlinkoption(self): def impl_is_symlinkoption(self):
return False return False
def to_dynoption(self,
rootpath: str,
suffix: str) -> DynSymLinkOption:
return DynSymLinkOption(self,
rootpath,
suffix)
def validate_requires_arg(new_option, def validate_requires_arg(new_option,
multi, multi,

View File

@ -51,16 +51,15 @@ 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 __debug__ and 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 '
'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')) 'dynoptiondescription'))
for chld in child.get_children(config_bag=undefined):
chld._setsubdyn(self)
if __debug__ and child.impl_is_symlinkoption():
raise ConfigError(_('cannot set symlinkoption in a '
'dynoptiondescription'))
child._setsubdyn(self) child._setsubdyn(self)
# add callback # add callback
self._impl_set_callback(callback, self._impl_set_callback(callback,

View File

@ -1,77 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018 Team tiramisu (see AUTHORS for all contributors)
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# The original `Config` design model is unproudly borrowed from
# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
# the whole pypy projet is under MIT licence
# ____________________________________________________________
from ..setting import undefined, OptionBag
class DynSymLinkOption(object):
__slots__ = ('_rootpath',
'_opt',
'_suffix')
def __init__(self,
opt,
rootpath,
suffix) -> None:
self._opt = opt
self._rootpath = rootpath
self._suffix = suffix
def __getattr__(self,
name):
return getattr(self._opt, name)
def __eq__(self, left):
if not isinstance(left, DynSymLinkOption):
return False
return self._opt == left._opt and \
self._rootpath == left._rootpath and \
self._suffix == left._suffix
def impl_getname(self):
return self._opt.impl_getname() + self._suffix
def impl_get_display_name(self):
return self._opt.impl_get_display_name(dyn_name=self.impl_getname())
def impl_getsuffix(self):
return self._suffix
def impl_getpath(self):
return self._rootpath + '.' + self.impl_getname()
def impl_validate(self,
value,
option_bag,
check_error=True):
context = option_bag.config_bag.context
soption_bag = OptionBag()
soption_bag.set_option(self._opt,
self.impl_getpath(),
option_bag.index,
option_bag.config_bag)
soption_bag.ori_option = option_bag.option
soption_bag.fromconsistency = option_bag.fromconsistency.copy()
self._opt.impl_validate(value,
soption_bag,
check_error=check_error)
def impl_is_dynsymlinkoption(self):
return True

View File

@ -113,7 +113,7 @@ class MasterSlaves(OptionDescription):
def is_master(self, opt): def is_master(self, opt):
master = self._children[0][0] master = self._children[0][0]
return opt.impl_getname() == master or (opt.impl_is_dynsymlinkoption() and return opt.impl_getname() == master or (opt.impl_is_dynsymlinkoption() and
opt._opt.impl_getname() == master) opt.opt.impl_getname() == master)
def getmaster(self): def getmaster(self):
return self._children[1][0] return self._children[1][0]
@ -124,7 +124,7 @@ class MasterSlaves(OptionDescription):
def in_same_group(self, opt): def in_same_group(self, opt):
if opt.impl_is_dynsymlinkoption(): if opt.impl_is_dynsymlinkoption():
c_opt = opt._opt c_opt = opt.opt
else: else:
c_opt = opt c_opt = opt
return c_opt in self._children[1] return c_opt in self._children[1]

View File

@ -30,6 +30,7 @@ from ..autolib import carry_out_calculation
from ..error import (ConfigError, ValueWarning, PropertiesOptionError, from ..error import (ConfigError, ValueWarning, PropertiesOptionError,
ValueOptionError, display_list) ValueOptionError, display_list)
from ..function import Params, ParamValue from ..function import Params, ParamValue
from .syndynoption import SynDynOption
ALLOWED_CONST_LIST = ['_cons_not_equal'] ALLOWED_CONST_LIST = ['_cons_not_equal']
@ -511,8 +512,8 @@ class Option(BaseOption):
if option_bag.ori_option.impl_is_dynsymlinkoption(): if option_bag.ori_option.impl_is_dynsymlinkoption():
opts = [] opts = []
for opt in all_cons_opts: for opt in all_cons_opts:
opts.append(opt().to_dynoption(option_bag.ori_option._rootpath, opts.append(opt().to_dynoption(option_bag.ori_option.rootpath,
option_bag.ori_option._suffix)) option_bag.ori_option.suffix))
wopt = opts[0] wopt = opts[0]
else: else:
opts = all_cons_opts opts = all_cons_opts
@ -714,6 +715,13 @@ class Option(BaseOption):
equal_name.append(opt.impl_get_display_name()) equal_name.append(opt.impl_get_display_name())
raise ValueError(msg.format(display_list(list(equal_name)))) raise ValueError(msg.format(display_list(list(equal_name))))
def to_dynoption(self,
rootpath: str,
suffix: str) -> SynDynOption:
return SynDynOption(self,
rootpath,
suffix)
class RegexpOption(Option): class RegexpOption(Option):
__slots__ = tuple() __slots__ = tuple()

View File

@ -0,0 +1,82 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018 Team tiramisu (see AUTHORS for all contributors)
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# The original `Config` design model is unproudly borrowed from
# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
# the whole pypy projet is under MIT licence
# ____________________________________________________________
from typing import Any
from ..setting import undefined, OptionBag
from .baseoption import BaseOption
class SynDynOption:
"""SynDynOption is an Option include un DynOptionDescription with specified prefix
"""
__slots__ = ('rootpath',
'opt',
'suffix')
def __init__(self,
opt: BaseOption,
rootpath: str,
suffix: str) -> None:
self.opt = opt
self.rootpath = rootpath
self.suffix = suffix
def __getattr__(self,
name: str) -> Any:
return getattr(self.opt,
name)
def __eq__(self,
left: BaseOption) -> bool:
if not isinstance(left, SynDynOption):
return False
return self.opt == left.opt and \
self.rootpath == left.rootpath and \
self.suffix == left.suffix
def impl_getname(self) -> str:
return self.opt.impl_getname() + self.suffix
def impl_get_display_name(self) -> str:
return self.opt.impl_get_display_name(dyn_name=self.impl_getname())
def impl_getsuffix(self) -> str:
return self.suffix
def impl_getpath(self) -> str:
return self.rootpath + '.' + self.impl_getname()
def impl_validate(self,
value: Any,
option_bag: OptionBag,
check_error: bool=True) -> None:
soption_bag = OptionBag()
soption_bag.set_option(self.opt,
self.impl_getpath(),
option_bag.index,
option_bag.config_bag)
soption_bag.ori_option = option_bag.option
soption_bag.fromconsistency = option_bag.fromconsistency.copy()
self.opt.impl_validate(value,
soption_bag,
check_error=check_error)
def impl_is_dynsymlinkoption(self) -> bool:
return True

View File

@ -25,7 +25,7 @@ from ..i18n import _
from ..setting import ConfigBag, groups, undefined, Settings from ..setting import ConfigBag, groups, undefined, Settings
from ..value import Values from ..value import Values
from .baseoption import BaseOption from .baseoption import BaseOption
from .dynsymlinkoption import DynSymLinkOption from .syndynoption import SynDynOption
class SynDynOptionDescription(object): class SynDynOptionDescription(object):
@ -106,11 +106,11 @@ class SynDynOptionDescription(object):
class SynDynMasterSlaves(SynDynOptionDescription): class SynDynMasterSlaves(SynDynOptionDescription):
def getmaster(self) -> DynSymLinkOption: def getmaster(self) -> SynDynOption:
return self._opt.getmaster().to_dynoption(self.impl_getpath(), return self._opt.getmaster().to_dynoption(self.impl_getpath(),
self._suffix) self._suffix)
def getslaves(self) -> Iterator[DynSymLinkOption]: def getslaves(self) -> Iterator[SynDynOption]:
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,

View File

@ -484,7 +484,7 @@ class Settings(object):
breaked = False breaked = False
for option, expected in exps: for option, expected in exps:
if option.issubdyn(): if option.issubdyn():
option = option.to_dynoption(option_bag.option._rootpath, option = option.to_dynoption(option_bag.option.rootpath,
option_bag.option.impl_getsuffix()) option_bag.option.impl_getsuffix())
reqpath = option.impl_getpath() reqpath = option.impl_getpath()
#FIXME too later! #FIXME too later!