tiramisu/tiramisu/option/leadership.py

240 lines
11 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2019-02-23 19:06:23 +01:00
"Leadership support"
2021-02-24 20:30:04 +01:00
# Copyright (C) 2014-2021 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
# ____________________________________________________________
import weakref
from itertools import chain
from typing import List, Iterator, Optional, Any
from ..i18n import _
2019-10-27 11:09:15 +01:00
from ..setting import groups, undefined, OptionBag, Settings, ALLOWED_LEADER_PROPERTIES
from ..value import Values
from .optiondescription import OptionDescription
2019-02-23 19:06:23 +01:00
from .syndynoptiondescription import SynDynLeadership
from .baseoption import BaseOption
from .option import Option
2019-10-27 11:09:15 +01:00
from ..error import LeadershipError
2019-09-28 16:32:48 +02:00
from ..autolib import Calculation, ParamOption
2019-02-23 19:06:23 +01:00
class Leadership(OptionDescription):
__slots__ = ('leader',
'followers')
def __init__(self,
name: str,
doc: str,
children: List[BaseOption],
properties=None) -> None:
2019-02-23 19:06:23 +01:00
super().__init__(name,
doc,
children,
properties=properties)
self._group_type = groups.leadership
followers = []
2018-04-09 21:37:49 +02:00
if len(children) < 2:
2019-02-23 19:06:23 +01:00
raise ValueError(_('a leader and a follower are mandatories in leadership "{}"'
'').format(name))
2019-02-23 19:06:23 +01:00
leader = children[0]
for idx, child in enumerate(children):
if __debug__:
if child.impl_is_symlinkoption():
2019-02-23 19:06:23 +01:00
raise ValueError(_('leadership "{0}" shall not have '
"a symlinkoption").format(self.impl_get_display_name()))
if not isinstance(child, Option):
2019-02-23 19:06:23 +01:00
raise ValueError(_('leadership "{0}" shall not have '
'a subgroup').format(self.impl_get_display_name()))
if not child.impl_is_multi():
2019-02-23 19:06:23 +01:00
raise ValueError(_('only multi option allowed in leadership "{0}" but option '
'"{1}" is not a multi'
'').format(self.impl_get_display_name(),
child.impl_get_display_name()))
2019-09-28 16:32:48 +02:00
if idx != 0:
default = child.impl_getdefault()
2021-03-18 08:57:22 +01:00
if default != []:
if child.impl_is_submulti() and isinstance(default, tuple):
for val in default:
if not isinstance(val, Calculation):
calculation = False
else:
# empty default is valid
calculation = True
else:
calculation = isinstance(default, Calculation)
if not calculation:
raise ValueError(_('not allowed default value for follower option "{0}" '
'in leadership "{1}"'
'').format(child.impl_get_display_name(),
self.impl_get_display_name()))
if idx != 0:
2019-02-23 19:06:23 +01:00
# remove empty property for follower
2019-11-19 18:46:05 +01:00
child._properties = frozenset(child._properties - {'empty', 'unique'})
2019-02-23 19:06:23 +01:00
followers.append(child)
2017-12-02 22:53:57 +01:00
child._add_dependency(self)
2019-02-23 19:06:23 +01:00
child._leadership = weakref.ref(self)
2019-09-28 16:32:48 +02:00
if __debug__:
callback, callback_params = leader.impl_get_callback()
options = []
if callback is not None and callback_params is not None:
for callbk in chain(callback_params.args, callback_params.kwargs.values()):
if isinstance(callbk, ParamOption) and callbk.option in followers:
raise ValueError(_("callback of leader's option shall "
"not refered to a follower's ones"))
2019-10-27 11:09:15 +01:00
for prop in leader.impl_getproperties():
if prop not in ALLOWED_LEADER_PROPERTIES and not isinstance(prop, Calculation):
raise LeadershipError(_('leader cannot have "{}" property').format(prop))
2019-02-23 19:06:23 +01:00
def is_leader(self,
opt: Option) -> bool:
2019-02-23 19:06:23 +01:00
leader = self.get_leader()
return opt == leader or (opt.impl_is_dynsymlinkoption() and opt.opt == leader)
2019-02-23 19:06:23 +01:00
def get_leader(self) -> Option:
2017-12-02 22:53:57 +01:00
return self._children[1][0]
2019-02-23 19:06:23 +01:00
def get_followers(self) -> Iterator[Option]:
for follower in self._children[1][1:]:
yield follower
def in_same_group(self,
opt: Option) -> bool:
if opt.impl_is_dynsymlinkoption():
opt = opt.opt
return opt in self._children[1]
2019-12-24 15:24:20 +01:00
async def reset(self,
values: Values,
2020-01-22 20:46:18 +01:00
option_bag: OptionBag) -> None:
2018-08-02 22:35:40 +02:00
config_bag = option_bag.config_bag.copy()
2018-08-17 23:11:25 +02:00
config_bag.remove_validation()
2019-02-23 19:06:23 +01:00
for follower in self.get_followers():
2018-08-01 08:37:58 +02:00
soption_bag = OptionBag()
2019-02-23 19:06:23 +01:00
soption_bag.set_option(follower,
2018-08-01 08:37:58 +02:00
None,
config_bag)
2019-12-24 15:24:20 +01:00
soption_bag.properties = await config_bag.context.cfgimpl_get_settings().getproperties(soption_bag)
2020-01-22 20:46:18 +01:00
await values.reset(soption_bag)
2019-12-24 15:24:20 +01:00
async def follower_force_store_value(self,
values,
value,
option_bag,
owner,
2019-12-25 20:44:56 +01:00
dyn=None) -> None:
2019-11-19 18:46:05 +01:00
settings = option_bag.config_bag.context.cfgimpl_get_settings()
if value:
rgevalue = range(len(value))
2019-12-25 20:44:56 +01:00
if dyn is None:
dyn = self
for follower in await dyn.get_children(option_bag.config_bag):
2019-11-19 18:46:05 +01:00
foption_bag = OptionBag()
foption_bag.set_option(follower,
None,
option_bag.config_bag)
2019-12-24 15:24:20 +01:00
if 'force_store_value' in await settings.getproperties(foption_bag):
2019-11-19 18:46:05 +01:00
for index in rgevalue:
foption_bag = OptionBag()
foption_bag.set_option(follower,
index,
option_bag.config_bag)
2019-12-24 15:24:20 +01:00
foption_bag.properties = await settings.getproperties(foption_bag)
await values._setvalue(foption_bag,
await values.getvalue(foption_bag),
2020-01-22 20:46:18 +01:00
owner)
2019-12-24 15:24:20 +01:00
async def pop(self,
values: Values,
index: int,
option_bag: OptionBag,
followers: Optional[List[Option]]=undefined) -> None:
2019-02-23 19:06:23 +01:00
if followers is undefined:
# followers are not undefined only in SynDynLeadership
followers = self.get_followers()
2018-08-02 22:35:40 +02:00
config_bag = option_bag.config_bag.copy()
2018-08-17 23:11:25 +02:00
config_bag.remove_validation()
2019-02-23 19:06:23 +01:00
for follower in followers:
follower_path = follower.impl_getpath()
2020-01-22 20:46:18 +01:00
followerlen = await values._p_.get_max_length(config_bag.connection,
follower_path)
2018-08-01 08:37:58 +02:00
soption_bag = OptionBag()
2019-02-23 19:06:23 +01:00
soption_bag.set_option(follower,
2018-08-01 08:37:58 +02:00
index,
config_bag)
# do not check force_default_on_freeze or force_metaconfig_on_freeze
2018-08-02 22:35:40 +02:00
soption_bag.properties = set()
2019-12-24 15:24:20 +01:00
is_default = await values.is_default_owner(soption_bag,
validate_meta=False)
if not is_default and followerlen > index:
2020-01-22 20:46:18 +01:00
await values._p_.resetvalue_index(config_bag.connection,
follower_path,
index)
2019-02-23 19:06:23 +01:00
if followerlen > index + 1:
for idx in range(index + 1, followerlen):
2020-01-22 20:46:18 +01:00
if await values._p_.hasvalue(config_bag.connection,
follower_path,
idx):
await values._p_.reduce_index(config_bag.connection,
follower_path,
2019-12-24 15:24:20 +01:00
idx)
def reset_cache(self,
path: str,
config_bag: 'ConfigBag',
resetted_opts: List[Option]) -> None:
self._reset_cache(path,
2019-02-23 19:06:23 +01:00
self.get_leader(),
self.get_followers(),
config_bag,
2018-04-09 21:37:49 +02:00
resetted_opts)
def _reset_cache(self,
path: str,
2019-02-23 19:06:23 +01:00
leader: Option,
followers: List[Option],
config_bag: 'ConfigBag',
resetted_opts: List[Option]) -> None:
super().reset_cache(path,
config_bag,
resetted_opts)
2019-02-23 19:06:23 +01:00
leader.reset_cache(leader.impl_getpath(),
config_bag,
2018-04-09 21:37:49 +02:00
None)
2019-02-23 19:06:23 +01:00
for follower in followers:
spath = follower.impl_getpath()
follower.reset_cache(spath,
config_bag,
2019-02-23 19:06:23 +01:00
None)
2019-12-24 15:24:20 +01:00
# do not reset dependencies option
# resetted_opts.append(spath)
2019-02-23 19:06:23 +01:00
def impl_is_leadership(self) -> None:
2017-12-02 22:53:57 +01:00
return True
def to_dynoption(self,
rootpath: str,
suffix: str,
ori_dyn) -> SynDynLeadership:
2019-02-23 19:06:23 +01:00
return SynDynLeadership(self,
rootpath,
suffix,
ori_dyn)