from tiramisu.config import Config
from tiramisu.option import ChoiceOption, BoolOption, IntOption, FloatOption, \
StrOption, OptionDescription
-from tiramisu.error import PropertiesOptionError, ConflictError
+from tiramisu.error import PropertiesOptionError, ConflictError, SlaveError
-def return_value():
+def return_val():
return 'val'
+def return_list():
+ return ['val', 'val']
+
+
+def return_value(value):
+ return value
+
+
def make_description():
gcoption = ChoiceOption('name', 'GC name', ('ref', 'framework'), 'ref')
gcdummy = BoolOption('dummy', 'dummy', default=False)
def test_callback():
- val1 = StrOption('val1', "", callback=return_value)
+ val1 = StrOption('val1', "", callback=return_val)
maconfig = OptionDescription('rootconfig', '', [val1])
cfg = Config(maconfig)
cfg.read_write()
assert cfg.val1 == 'val'
-def test_callback_master_and_slaves():
+def test_callback_value():
+ val1 = StrOption('val1', "", 'val')
+ val2 = StrOption('val2', "", callback=return_value, callback_params={'': (('val1', False),)})
+ maconfig = OptionDescription('rootconfig', '', [val1, val2])
+ cfg = Config(maconfig)
+ cfg.read_write()
+ assert cfg.val1 == 'val'
+ assert cfg.val2 == 'val'
+ cfg.val1 = 'new-val'
+ assert cfg.val1 == 'new-val'
+ assert cfg.val2 == 'new-val'
+ del(cfg.val1)
+ assert cfg.val1 == 'val'
+ assert cfg.val2 == 'val'
+
+
+def test_callback_list():
+ val1 = StrOption('val1', "", callback=return_list)
+ maconfig = OptionDescription('rootconfig', '', [val1])
+ cfg = Config(maconfig)
+ cfg.read_write()
+ raises(ValueError, "cfg.val1")
+
+
+def test_callback_multi():
+ val1 = StrOption('val1', "", callback=return_val, multi=True)
+ maconfig = OptionDescription('rootconfig', '', [val1])
+ cfg = Config(maconfig)
+ cfg.read_write()
+ assert cfg.val1 == ['val']
+ cfg.val1 = ['new-val']
+ assert cfg.val1 == ['new-val']
+ cfg.val1.append('new-val2')
+ assert cfg.val1 == ['new-val', 'new-val2']
+ del(cfg.val1)
+ assert cfg.val1 == ['val']
+
+
+def test_callback_multi_value():
+ val1 = StrOption('val1', "", ['val'], multi=True)
+ val2 = StrOption('val2', "", multi=True, callback=return_value, callback_params={'': (('val1', False),)})
+ maconfig = OptionDescription('rootconfig', '', [val1, val2])
+ cfg = Config(maconfig)
+ cfg.read_write()
+ assert cfg.val1 == ['val']
+ assert cfg.val2 == ['val']
+ cfg.val1 = ['new-val']
+ assert cfg.val1 == ['new-val']
+ assert cfg.val2 == ['new-val']
+ cfg.val1.append('new-val2')
+ assert cfg.val1 == ['new-val', 'new-val2']
+ assert cfg.val2 == ['new-val', 'new-val2']
+ del(cfg.val1)
+ assert cfg.val1 == ['val']
+ assert cfg.val2 == ['val']
+
+
+def test_callback_multi_list():
+ val1 = StrOption('val1', "", callback=return_list, multi=True)
+ maconfig = OptionDescription('rootconfig', '', [val1])
+ cfg = Config(maconfig)
+ cfg.read_write()
+ assert cfg.val1 == ['val', 'val']
+ cfg.val1 = ['new-val']
+ assert cfg.val1 == ['new-val']
+ cfg.val1.append('new-val2')
+ assert cfg.val1 == ['new-val', 'new-val2']
+ del(cfg.val1)
+ assert cfg.val1 == ['val', 'val']
+
+
+def test_callback_master_and_slaves_master():
+ val1 = StrOption('val1', "", multi=True, callback=return_val)
+ val2 = StrOption('val2', "", multi=True)
+ interface1 = OptionDescription('val1', '', [val1, val2])
+ interface1.impl_set_group_type(groups.master)
+ maconfig = OptionDescription('rootconfig', '', [interface1])
+ cfg = Config(maconfig)
+ cfg.read_write()
+ assert cfg.val1.val1 == ['val']
+ cfg.val1.val1.append(None)
+ assert cfg.val1.val1 == ['val', 'val']
+ assert cfg.val1.val2 == [None, None]
+
+
+def test_callback_master_and_slaves_master_list():
+ val1 = StrOption('val1', "", multi=True, callback=return_list)
+ val2 = StrOption('val2', "", multi=True)
+ interface1 = OptionDescription('val1', '', [val1, val2])
+ interface1.impl_set_group_type(groups.master)
+ maconfig = OptionDescription('rootconfig', '', [interface1])
+ cfg = Config(maconfig)
+ cfg.read_write()
+ assert cfg.val1.val1 == ['val', 'val']
+ assert cfg.val1.val2 == [None, None]
+ cfg.val1.val1.append(None)
+ assert cfg.val1.val1 == ['val', 'val', None]
+ assert cfg.val1.val2 == [None, None, None]
+ del(cfg.val1.val1)
+ assert cfg.val1.val1 == ['val', 'val']
+ assert cfg.val1.val2 == [None, None]
+ assert cfg.val1.val1.pop(1)
+ assert cfg.val1.val1 == ['val']
+ assert cfg.val1.val2 == [None]
+
+
+def test_callback_master_and_slaves_slave():
val1 = StrOption('val1', "", multi=True)
- val2 = StrOption('val2', "", multi=True, callback=return_value)
+ val2 = StrOption('val2', "", multi=True, callback=return_val)
interface1 = OptionDescription('val1', '', [val1, val2])
interface1.impl_set_group_type(groups.master)
maconfig = OptionDescription('rootconfig', '', [interface1])
#
cfg.val1.val1.append('val3')
assert cfg.val1.val2 == ['val2', 'val2', 'val']
+
+
+def test_callback_master_and_slaves_slave_list():
+ val1 = StrOption('val1', "", multi=True)
+ val2 = StrOption('val2', "", multi=True, callback=return_list)
+ interface1 = OptionDescription('val1', '', [val1, val2])
+ interface1.impl_set_group_type(groups.master)
+ maconfig = OptionDescription('rootconfig', '', [interface1])
+ cfg = Config(maconfig)
+ cfg.read_write()
+ assert cfg.val1.val2 == []
+ cfg.val1.val1 = ['val1', 'val2']
+ assert cfg.val1.val1 == ['val1', 'val2']
+ assert cfg.val1.val2 == ['val', 'val']
+ cfg.val1.val1 = ['val1']
+ #wrong len
+ raises(SlaveError, 'cfg.val1.val2')
+
+
+def test_callback_master_and_slaves_value():
+ val1 = StrOption('val1', "", multi=True)
+ val2 = StrOption('val2', "", multi=True, callback=return_value, callback_params={'': (('val1.val1', False),)})
+ interface1 = OptionDescription('val1', '', [val1, val2])
+ interface1.impl_set_group_type(groups.master)
+ maconfig = OptionDescription('rootconfig', '', [interface1])
+ cfg = Config(maconfig)
+ cfg.read_write()
+ assert cfg.val1.val1 == []
+ assert cfg.val1.val2 == []
+ #
+ cfg.val1.val1 = ['val1']
+ assert cfg.val1.val1 == ['val1']
+ assert cfg.val1.val2 == ['val1']
+ #
+ cfg.val1.val1.append('val2')
+ assert cfg.val1.val1 == ['val1', 'val2']
+ assert cfg.val1.val2 == ['val1', 'val2']
+ #
+ cfg.val1.val1 = ['val1', 'val2', 'val3']
+ assert cfg.val1.val1 == ['val1', 'val2', 'val3']
+ assert cfg.val1.val2 == ['val1', 'val2', 'val3']
+ #
+ cfg.val1.val1.pop(2)
+ assert cfg.val1.val1 == ['val1', 'val2']
+ assert cfg.val1.val2 == ['val1', 'val2']
+ #
+ cfg.val1.val2 = ['val2', 'val2']
+ assert cfg.val1.val2 == ['val2', 'val2']
+ #
+ cfg.val1.val1.append('val3')
+ assert cfg.val1.val2 == ['val2', 'val2', 'val3']
+
+
#
# ____________________________________________________________
from time import time
+from copy import copy
from tiramisu.error import ConfigError, SlaveError
from tiramisu.setting import owners, multitypes, expires_time
from tiramisu.autolib import carry_out_calculation
def _get_default(self, opt):
meta = self.context.cfgimpl_get_meta()
if meta is not None:
- return meta.cfgimpl_get_values()[opt]
+ value = meta.cfgimpl_get_values()[opt]
else:
- return opt.impl_getdefault()
+ value = opt.impl_getdefault()
+ if opt.impl_is_multi():
+ return copy(value)
+ else:
+ return value
def _get_value(self, opt, validate=True):
"return value or default value if not set"
opt.impl_validate(opt.impl_getdefault(), self.context,
'validator' in setting)
self.context.cfgimpl_reset_cache()
+ if opt.impl_is_multi() and opt.impl_get_multitype() == multitypes.master:
+ for slave in opt.impl_get_master_slaves():
+ self._reset(slave)
del(self._values[opt])
def _is_empty(self, opt, value):
return True
return False
- def _getcallback_value(self, opt):
+ def _getcallback_value(self, opt, index=None):
callback, callback_params = opt._callback
if callback_params is None:
callback_params = {}
return carry_out_calculation(opt._name, config=self.context,
callback=callback,
- callback_params=callback_params)
+ callback_params=callback_params,
+ index=index)
def __getitem__(self, opt):
return self.getitem(opt)
if not no_value_slave:
value = self._getcallback_value(opt)
if opt.impl_is_multi() and opt.impl_get_multitype() == multitypes.slave:
- if isinstance(value, list):
- raise ValueError('callback must not return list '
- 'in slave {0}: {1}'.format(opt._name,
- value))
- value = [value for i in range(lenmaster)]
+ if not isinstance(value, list):
+ value = [value for i in range(lenmaster)]
if opt.impl_is_multi():
value = Multi(value, self.context, opt, validate)
#suppress value if already set
raise SlaveError(_("cannot append a value on a multi option {0}"
" which is a slave").format(self.opt._name))
elif self.opt.impl_get_multitype() == multitypes.master:
- for slave in self.opt.impl_get_master_slaves():
- values = self.context.cfgimpl_get_values()
- if not values.is_default_owner(slave):
- if slave.impl_has_callback():
- dvalue = values._getcallback_value(slave)
- else:
- dvalue = slave.impl_getdefault_multi()
- #get multi without valid properties
- values.getitem(slave, validate_properties=False).append(
- dvalue, force=True)
+ values = self.context.cfgimpl_get_values()
+ if value is None and self.opt.impl_has_callback():
+ value = values._getcallback_value(self.opt)
+ #Force None il return a list
+ if isinstance(value, list):
+ value = None
self._validate(value)
#set value without valid properties
self.context.cfgimpl_get_values()._setvalue(self.opt, self, validate_properties=not force)
super(Multi, self).append(value)
+ if not force and self.opt.impl_get_multitype() == multitypes.master:
+ for slave in self.opt.impl_get_master_slaves():
+ if not values.is_default_owner(slave):
+ if slave.impl_has_callback():
+ index = self.__len__() - 1
+ dvalue = values._getcallback_value(slave, index=index)
+ else:
+ dvalue = slave.impl_getdefault_multi()
+ #get multi without valid properties
+ values.getitem(slave, validate_properties=False).append(
+ dvalue, force=True)
def _validate(self, value):
if value is not None and not self.opt._validate(value):