449 lines
19 KiB
Python
449 lines
19 KiB
Python
# -*- coding: utf-8 -*-
|
|
"master slave support"
|
|
# Copyright (C) 2014-2017 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 ..i18n import _
|
|
from ..setting import groups, undefined, log, debug
|
|
from .optiondescription import OptionDescription
|
|
from .option import Option
|
|
from ..error import SlaveError, PropertiesOptionError
|
|
|
|
|
|
class MasterSlaves(OptionDescription):
|
|
__slots__ = ('master', 'slaves')
|
|
|
|
def __init__(self,
|
|
name,
|
|
doc,
|
|
children,
|
|
requires=None,
|
|
properties=None):
|
|
|
|
super(MasterSlaves, self).__init__(name,
|
|
doc,
|
|
children,
|
|
requires=requires,
|
|
properties=properties)
|
|
self._group_type = groups.master
|
|
slaves = []
|
|
master = children[0]
|
|
if not children:
|
|
raise ValueError(_('children is mandatory in masterslaves "{}"').format(name))
|
|
for child in children[1:]:
|
|
if child.impl_getdefault() != []:
|
|
raise ValueError(_("not allowed default value for option {0} "
|
|
"in master/slave object {1}").format(child.impl_getname(),
|
|
name))
|
|
slaves.append(child)
|
|
child._add_dependency(self)
|
|
for idx, child in enumerate(children):
|
|
if child.impl_is_symlinkoption(): # pragma: optional cover
|
|
raise ValueError(_("master group {0} shall not have "
|
|
"a symlinkoption").format(self.impl_getname()))
|
|
if not isinstance(child, Option): # pragma: optional cover
|
|
raise ValueError(_("master group {0} shall not have "
|
|
"a subgroup").format(self.impl_getname()))
|
|
if not child.impl_is_multi(): # pragma: optional cover
|
|
raise ValueError(_("not allowed option {0} "
|
|
"in group {1}"
|
|
": this option is not a multi"
|
|
"").format(child.impl_getname(), self.impl_getname()))
|
|
# no empty property for save
|
|
if idx != 0:
|
|
properties = list(child._properties)
|
|
properties.remove('empty')
|
|
child._properties = tuple(properties)
|
|
callback, callback_params = master.impl_get_callback()
|
|
if callback is not None and callback_params != {}:
|
|
for callbacks in callback_params.values():
|
|
for callbk in callbacks:
|
|
if isinstance(callbk, tuple):
|
|
if callbk[0] in slaves:
|
|
raise ValueError(_("callback of master's option shall "
|
|
"not refered a slave's ones"))
|
|
#everything is ok, store references
|
|
for child in children:
|
|
child._master_slaves = weakref.ref(self)
|
|
master._add_dependency(self)
|
|
|
|
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)
|
|
|
|
def getmaster(self, opt):
|
|
master = self._children[1][0]
|
|
if opt is not None and opt.impl_is_dynsymlinkoption():
|
|
suffix = opt.impl_getsuffix()
|
|
name = master.impl_getname() + suffix
|
|
base_path = opt._dyn.split('.')[0] + '.'
|
|
path = base_path + name
|
|
master = master._impl_to_dyn(name, path)
|
|
return master
|
|
|
|
def getslaves(self, opt):
|
|
if opt.impl_is_dynsymlinkoption():
|
|
for slave in self._children[1][1:]:
|
|
suffix = opt.impl_getsuffix()
|
|
name = slave.impl_getname() + suffix
|
|
base_path = opt._dyn.split('.')[0] + '.'
|
|
path = base_path + name
|
|
yield slave._impl_to_dyn(name, path)
|
|
else:
|
|
for slave in self._children[1][1:]:
|
|
yield slave
|
|
|
|
def in_same_group(self, opt):
|
|
if opt.impl_is_dynsymlinkoption():
|
|
c_opt = opt._opt
|
|
else:
|
|
c_opt = opt
|
|
return c_opt in self._children[1]
|
|
|
|
def reset(self,
|
|
opt,
|
|
values,
|
|
setting_properties,
|
|
_commit=True,
|
|
force_permissive=False):
|
|
|
|
for slave in self.getslaves(opt):
|
|
slave_path = slave.impl_getpath(values._getcontext())
|
|
values.reset(slave,
|
|
slave_path,
|
|
setting_properties,
|
|
validate=False,
|
|
_commit=_commit,
|
|
force_permissive=force_permissive)
|
|
|
|
def pop(self,
|
|
opt,
|
|
path,
|
|
values,
|
|
index,
|
|
setting_properties,
|
|
force_permissive=False):
|
|
|
|
for slave in self.getslaves(opt):
|
|
slave_path = slave.impl_getpath(values._getcontext())
|
|
slavelen = values._p_.get_max_length(slave_path)
|
|
if not values.is_default_owner(slave,
|
|
slave_path,
|
|
setting_properties,
|
|
validate_meta=False,
|
|
index=index,
|
|
force_permissive=force_permissive):
|
|
#FIXME # just for raise if needed
|
|
#multi = values.get_cached_value(slave,
|
|
# validate=False,
|
|
# validate_properties=False,
|
|
# )
|
|
#if isinstance(multi, Exception):
|
|
# raise multi
|
|
if slavelen > index:
|
|
values._p_.resetvalue_index(slave_path,
|
|
index)
|
|
if slavelen > index + 1:
|
|
for idx in range(index + 1, slavelen):
|
|
values._p_.reduce_index(slave_path,
|
|
idx)
|
|
|
|
|
|
def getitem(self,
|
|
values,
|
|
opt,
|
|
path,
|
|
validate,
|
|
force_permissive,
|
|
trusted_cached_properties,
|
|
validate_properties,
|
|
setting_properties=undefined,
|
|
self_properties=undefined,
|
|
index=None,
|
|
check_frozen=False):
|
|
if self.is_master(opt):
|
|
return self._getmaster(values,
|
|
opt,
|
|
path,
|
|
validate,
|
|
force_permissive,
|
|
validate_properties,
|
|
self_properties,
|
|
index,
|
|
setting_properties,
|
|
check_frozen)
|
|
else:
|
|
return self._getslave(values,
|
|
opt,
|
|
path,
|
|
validate,
|
|
force_permissive,
|
|
trusted_cached_properties,
|
|
validate_properties,
|
|
setting_properties,
|
|
self_properties,
|
|
index,
|
|
check_frozen)
|
|
|
|
def _getmaster(self,
|
|
values,
|
|
opt,
|
|
path,
|
|
validate,
|
|
force_permissive,
|
|
validate_properties,
|
|
self_properties,
|
|
index,
|
|
setting_properties,
|
|
check_frozen):
|
|
return values.get_cached_value(opt,
|
|
path=path,
|
|
validate=validate,
|
|
force_permissive=force_permissive,
|
|
self_properties=self_properties,
|
|
index=index,
|
|
setting_properties=setting_properties)
|
|
|
|
def _getslave(self, values, opt, path, validate, force_permissive,
|
|
trusted_cached_properties, validate_properties, setting_properties,
|
|
self_properties, index, check_frozen):
|
|
"""
|
|
if master has length 0:
|
|
return []
|
|
if master has length bigger than 0:
|
|
if default owner:
|
|
if has callback:
|
|
if return a list:
|
|
list same length as master: return list
|
|
list is smaller than master: return list + None
|
|
list is greater than master: raise SlaveError
|
|
if has default value:
|
|
list same length as master: return list
|
|
list is smaller than master: return list + None
|
|
list is greater than master: raise SlaveError
|
|
if has default_multi value:
|
|
return default_multi * master's length
|
|
if has value:
|
|
list same length as master: return list
|
|
list is smaller than master: return list + None
|
|
list is greater than master: raise SlaveError
|
|
"""
|
|
master = self.getmaster(opt)
|
|
context = values._getcontext()
|
|
masterp = master.impl_getpath(context)
|
|
try:
|
|
mastervalue = values.get_cached_value(master,
|
|
path=masterp,
|
|
validate=validate,
|
|
force_permissive=force_permissive,
|
|
validate_properties=validate_properties,
|
|
self_properties=self_properties,
|
|
from_masterslave=True,
|
|
setting_properties=setting_properties,
|
|
check_frozen=check_frozen)
|
|
except PropertiesOptionError as mastervalue:
|
|
mastervalue.set_orig_opt(opt)
|
|
raise mastervalue
|
|
masterlen = len(mastervalue)
|
|
#self._master_is_meta = values._is_meta(master, masterp, force_permissive=force_permissive)
|
|
multi = list() # values._get_multi(opt, path)
|
|
if validate_properties:
|
|
props = context.cfgimpl_get_settings().validate_properties(opt, False,
|
|
check_frozen,
|
|
value=multi,
|
|
path=path,
|
|
force_permissive=force_permissive,
|
|
setting_properties=setting_properties)
|
|
if props:
|
|
return props
|
|
#FIXME shouldn't have index!!!
|
|
if index is None:
|
|
indexes = range(0, masterlen)
|
|
else:
|
|
indexes = [index]
|
|
for idx in indexes:
|
|
try:
|
|
value = values.get_cached_value(opt,
|
|
path,
|
|
validate,
|
|
force_permissive,
|
|
trusted_cached_properties,
|
|
validate_properties,
|
|
index=idx,
|
|
# not self_properties,
|
|
# depends to index
|
|
#self_properties=self_properties,
|
|
setting_properties=setting_properties,
|
|
from_masterslave=True,
|
|
check_frozen=check_frozen)
|
|
except PropertiesOptionError as perr:
|
|
err = perr
|
|
if index is None:
|
|
multi.append(err)
|
|
else:
|
|
multi = err
|
|
if index is None:
|
|
multi.append(value)
|
|
else:
|
|
multi = value
|
|
return multi
|
|
|
|
def validate(self,
|
|
values,
|
|
opt,
|
|
index,
|
|
path,
|
|
setitem):
|
|
if self.is_master(opt):
|
|
#for regen slave path
|
|
base_path = '.'.join(path.split('.')[:-1]) + '.'
|
|
for slave in self.getslaves(opt):
|
|
slave_path = base_path + slave.impl_getname()
|
|
slavelen = values._p_.get_max_length(slave_path)
|
|
self.validate_slave_length(index,
|
|
slavelen,
|
|
slave.impl_getname(),
|
|
opt)
|
|
else:
|
|
val_len = self.get_length(values)
|
|
if isinstance(val_len, Exception):
|
|
return val_len
|
|
self.validate_slave_length(val_len,
|
|
index,
|
|
opt.impl_getname(),
|
|
opt,
|
|
setitem=setitem)
|
|
|
|
def get_length(self,
|
|
values,
|
|
validate=True,
|
|
force_permissive=False,
|
|
master=None,
|
|
masterp=None,
|
|
setting_properties=undefined):
|
|
"""get master len with slave option"""
|
|
if master is None:
|
|
master = self.getmaster(None)
|
|
if masterp is None:
|
|
masterp = master.impl_getpath(values._getcontext())
|
|
value = self._getmaster(values,
|
|
master,
|
|
masterp,
|
|
validate,
|
|
force_permissive,
|
|
validate,
|
|
undefined,
|
|
None,
|
|
setting_properties,
|
|
False)
|
|
if isinstance(value, Exception):
|
|
return value
|
|
return len(value)
|
|
|
|
def validate_slave_length(self,
|
|
masterlen,
|
|
valuelen,
|
|
name,
|
|
opt,
|
|
setitem=False):
|
|
if valuelen > masterlen or (valuelen < masterlen and setitem):
|
|
if debug: # pragma: no cover
|
|
log.debug('validate_slave_length: masterlen: {0}, valuelen: {1}, '
|
|
'setitem: {2}'.format(masterlen, valuelen, setitem))
|
|
if not opt.impl_is_master_slaves('master'):
|
|
opt = self.getmaster(opt)
|
|
raise SlaveError(_("invalid len for the slave: {0}"
|
|
" which has {1} as master").format(
|
|
name, opt.impl_getname()))
|
|
|
|
def reset_cache(self,
|
|
opt,
|
|
path,
|
|
obj,
|
|
type_,
|
|
resetted_opts):
|
|
context = obj._getcontext()
|
|
#FIXME pb avec dyn, devrait etre une option
|
|
mopt = self.getmaster(None)
|
|
mpath = mopt.impl_getpath(context)
|
|
mopt.reset_cache(mopt,
|
|
mpath,
|
|
obj,
|
|
type_,
|
|
resetted_opts)
|
|
for slave in self.getslaves(mopt):
|
|
spath = slave.impl_getpath(context)
|
|
slave.reset_cache(slave,
|
|
spath,
|
|
obj,
|
|
type_,
|
|
resetted_opts)
|
|
|
|
def impl_getchild(self,
|
|
name,
|
|
setting_properties,
|
|
context=undefined,
|
|
dyn=True):
|
|
return super(MasterSlaves, self).impl_getchild(name,
|
|
setting_properties,
|
|
context,
|
|
dyn)
|
|
|
|
def impl_validate(self,
|
|
context,
|
|
force_permissive,
|
|
setting_properties,
|
|
masterlen=None,
|
|
slavelen=None,
|
|
opt=None,
|
|
setitem=False):
|
|
values = context.cfgimpl_get_values()
|
|
if masterlen is None:
|
|
master = self.getmaster(opt)
|
|
masterp = master.impl_getpath(context)
|
|
|
|
mastervalue = values.get_cached_value(master, path=masterp,
|
|
force_permissive=force_permissive,
|
|
setting_properties=setting_properties)
|
|
if isinstance(mastervalue, Exception):
|
|
return mastervalue
|
|
masterlen = len(mastervalue)
|
|
else:
|
|
master = opt
|
|
if slavelen is not None:
|
|
self.validate_slave_length(masterlen, slavelen, opt.impl_getname(), master, setitem=setitem)
|
|
else:
|
|
for slave in self.getslaves(master):
|
|
slave_path = slave.impl_getpath(context)
|
|
slavelen = values._p_.get_max_length(slave_path)
|
|
self.validate_slave_length(masterlen, slavelen, slave.impl_getname(), master)
|
|
|
|
def impl_validate_value(self,
|
|
option,
|
|
value,
|
|
context):
|
|
if option.impl_is_master_slaves('master') and isinstance(value, list):
|
|
if len(value) < context._impl_length:
|
|
return ValueError(_('cannot reduce length of master "{}"'
|
|
'').format(option.impl_get_display_name()))
|