From 184a086bc18ada4fcf94de015e776667d17254fe Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Wed, 14 Nov 2018 21:51:40 +0100 Subject: [PATCH] simplify tiramisu/option/syndynoption.py --- test/test_dyn_optiondescription.py | 6 +- tiramisu/autolib.py | 2 +- tiramisu/option/__init__.py | 4 +- tiramisu/option/baseoption.py | 8 --- tiramisu/option/dynoptiondescription.py | 17 +++-- tiramisu/option/dynsymlinkoption.py | 77 -------------------- tiramisu/option/masterslaves.py | 4 +- tiramisu/option/option.py | 12 +++- tiramisu/option/syndynoption.py | 82 ++++++++++++++++++++++ tiramisu/option/syndynoptiondescription.py | 6 +- tiramisu/setting.py | 2 +- 11 files changed, 112 insertions(+), 108 deletions(-) delete mode 100644 tiramisu/option/dynsymlinkoption.py create mode 100644 tiramisu/option/syndynoption.py diff --git a/test/test_dyn_optiondescription.py b/test/test_dyn_optiondescription.py index 5eb95bc..f29426d 100644 --- a/test/test_dyn_optiondescription.py +++ b/test/test_dyn_optiondescription.py @@ -8,7 +8,7 @@ from tiramisu import BoolOption, StrOption, ChoiceOption, IPOption, \ NetworkOption, NetmaskOption, IntOption, FloatOption, \ UnicodeOption, PortOption, BroadcastOption, DomainnameOption, \ EmailOption, URLOption, UsernameOption, FilenameOption, SymLinkOption, \ - OptionDescription, DynOptionDescription, DynSymLinkOption, submulti, MasterSlaves, \ + OptionDescription, DynOptionDescription, SynDynOption, submulti, MasterSlaves, \ Config, Params, ParamOption, ParamValue from tiramisu.error import PropertiesOptionError, ConfigError, ConflictError from tiramisu.storage import list_sessions @@ -687,11 +687,11 @@ def test_find_dyndescription_context(): api = Config(od2) api.option('od.dodval1.stval1').value.set('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'] #opts = api.option.find(byvalue='yes') #assert len(opts) == 1 - #assert isinstance(opts[0], DynSymLinkOption) + #assert isinstance(opts[0], SynDynOption) #assert opts[0].impl_getname() == 'stval1' raises(AttributeError, "list(api.option.find('strnotexists'))") diff --git a/tiramisu/autolib.py b/tiramisu/autolib.py index ea0ce09..95ab4b1 100644 --- a/tiramisu/autolib.py +++ b/tiramisu/autolib.py @@ -48,7 +48,7 @@ def manager_callback(callbk: Union[ParamOption, ParamValue], force_settings=get_default_settings_storages()) opt = callbk.option if opt.issubdyn(): - opt = opt.to_dynoption(option._rootpath, + opt = opt.to_dynoption(option.rootpath, option.impl_getsuffix()) path = opt.impl_getpath() if index is not None and opt.impl_is_master_slaves() and \ diff --git a/tiramisu/option/__init__.py b/tiramisu/option/__init__.py index 50437eb..909f1e6 100644 --- a/tiramisu/option/__init__.py +++ b/tiramisu/option/__init__.py @@ -4,7 +4,7 @@ from .syndynoptiondescription import SynDynOptionDescription, SynDynMasterSlaves from .masterslaves import MasterSlaves from .baseoption import submulti from .symlinkoption import SymLinkOption -from .dynsymlinkoption import DynSymLinkOption +from .syndynoption import SynDynOption from .option import Option, RegexpOption from .choiceoption import ChoiceOption from .booloption import BoolOption @@ -27,7 +27,7 @@ from .passwordoption import PasswordOption __all__ = ('MasterSlaves', 'OptionDescription', 'DynOptionDescription', 'SynDynOptionDescription', 'SynDynMasterSlaves', 'Option', 'SymLinkOption', - 'DynSymLinkOption', 'ChoiceOption', 'BoolOption', 'DateOption', + 'SynDynOption', 'ChoiceOption', 'BoolOption', 'DateOption', 'IntOption', 'FloatOption', 'StrOption', 'UnicodeOption', 'IPOption', 'PortOption', 'NetworkOption', 'NetmaskOption', 'BroadcastOption', 'DomainnameOption', 'EmailOption', 'URLOption', diff --git a/tiramisu/option/baseoption.py b/tiramisu/option/baseoption.py index c8b2a2a..dd87783 100644 --- a/tiramisu/option/baseoption.py +++ b/tiramisu/option/baseoption.py @@ -29,7 +29,6 @@ from ..i18n import _ from ..setting import undefined from ..error import ConfigError, display_list from ..function import Params, ParamContext, ParamOption, ParamIndex -from .dynsymlinkoption import DynSymLinkOption STATIC_TUPLE = frozenset() @@ -447,13 +446,6 @@ class BaseOption(Base): def impl_is_symlinkoption(self): return False - def to_dynoption(self, - rootpath: str, - suffix: str) -> DynSymLinkOption: - return DynSymLinkOption(self, - rootpath, - suffix) - def validate_requires_arg(new_option, multi, diff --git a/tiramisu/option/dynoptiondescription.py b/tiramisu/option/dynoptiondescription.py index a1aa3c7..be06799 100644 --- a/tiramisu/option/dynoptiondescription.py +++ b/tiramisu/option/dynoptiondescription.py @@ -51,16 +51,15 @@ class DynOptionDescription(OptionDescription): properties) # check children + set relation to this dynoptiondescription for child in children: - if __debug__: - if isinstance(child, OptionDescription): - if child.impl_get_group_type() != groups.master: - 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 ' + if isinstance(child, OptionDescription): + if __debug__ and child.impl_get_group_type() != groups.master: + raise ConfigError(_('cannot set optiondescription in a ' '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) # add callback self._impl_set_callback(callback, diff --git a/tiramisu/option/dynsymlinkoption.py b/tiramisu/option/dynsymlinkoption.py deleted file mode 100644 index d2ae0c7..0000000 --- a/tiramisu/option/dynsymlinkoption.py +++ /dev/null @@ -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 . -# -# 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 diff --git a/tiramisu/option/masterslaves.py b/tiramisu/option/masterslaves.py index 79645d2..347725c 100644 --- a/tiramisu/option/masterslaves.py +++ b/tiramisu/option/masterslaves.py @@ -113,7 +113,7 @@ class MasterSlaves(OptionDescription): def is_master(self, opt): master = self._children[0][0] return opt.impl_getname() == master or (opt.impl_is_dynsymlinkoption() and - opt._opt.impl_getname() == master) + opt.opt.impl_getname() == master) def getmaster(self): return self._children[1][0] @@ -124,7 +124,7 @@ class MasterSlaves(OptionDescription): def in_same_group(self, opt): if opt.impl_is_dynsymlinkoption(): - c_opt = opt._opt + c_opt = opt.opt else: c_opt = opt return c_opt in self._children[1] diff --git a/tiramisu/option/option.py b/tiramisu/option/option.py index a28d553..6e8bb04 100644 --- a/tiramisu/option/option.py +++ b/tiramisu/option/option.py @@ -30,6 +30,7 @@ from ..autolib import carry_out_calculation from ..error import (ConfigError, ValueWarning, PropertiesOptionError, ValueOptionError, display_list) from ..function import Params, ParamValue +from .syndynoption import SynDynOption ALLOWED_CONST_LIST = ['_cons_not_equal'] @@ -511,8 +512,8 @@ class Option(BaseOption): if option_bag.ori_option.impl_is_dynsymlinkoption(): opts = [] for opt in all_cons_opts: - opts.append(opt().to_dynoption(option_bag.ori_option._rootpath, - option_bag.ori_option._suffix)) + opts.append(opt().to_dynoption(option_bag.ori_option.rootpath, + option_bag.ori_option.suffix)) wopt = opts[0] else: opts = all_cons_opts @@ -714,6 +715,13 @@ class Option(BaseOption): equal_name.append(opt.impl_get_display_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): __slots__ = tuple() diff --git a/tiramisu/option/syndynoption.py b/tiramisu/option/syndynoption.py new file mode 100644 index 0000000..5dc82e2 --- /dev/null +++ b/tiramisu/option/syndynoption.py @@ -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 . +# +# 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 diff --git a/tiramisu/option/syndynoptiondescription.py b/tiramisu/option/syndynoptiondescription.py index 5a6484a..0e6aaf8 100644 --- a/tiramisu/option/syndynoptiondescription.py +++ b/tiramisu/option/syndynoptiondescription.py @@ -25,7 +25,7 @@ from ..i18n import _ from ..setting import ConfigBag, groups, undefined, Settings from ..value import Values from .baseoption import BaseOption -from .dynsymlinkoption import DynSymLinkOption +from .syndynoption import SynDynOption class SynDynOptionDescription(object): @@ -106,11 +106,11 @@ class SynDynOptionDescription(object): class SynDynMasterSlaves(SynDynOptionDescription): - def getmaster(self) -> DynSymLinkOption: + def getmaster(self) -> SynDynOption: return self._opt.getmaster().to_dynoption(self.impl_getpath(), self._suffix) - def getslaves(self) -> Iterator[DynSymLinkOption]: + def getslaves(self) -> Iterator[SynDynOption]: subpath = self.impl_getpath() for slave in self._opt.getslaves(): yield slave.to_dynoption(subpath, diff --git a/tiramisu/setting.py b/tiramisu/setting.py index a4295a8..5a05ee6 100644 --- a/tiramisu/setting.py +++ b/tiramisu/setting.py @@ -484,7 +484,7 @@ class Settings(object): breaked = False for option, expected in exps: if option.issubdyn(): - option = option.to_dynoption(option_bag.option._rootpath, + option = option.to_dynoption(option_bag.option.rootpath, option_bag.option.impl_getsuffix()) reqpath = option.impl_getpath() #FIXME too later!