From 51d14f30a410c36b55fc6791a748006d74c4dbbe Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Mon, 25 Jan 2016 16:22:28 +0100 Subject: [PATCH] can set owner for a slave --- test/test_option_owner.py | 18 +++++++++++++++++- tiramisu/storage/dictionary/value.py | 8 +++++--- tiramisu/value.py | 13 ++++++++----- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/test/test_option_owner.py b/test/test_option_owner.py index 6065051..b0b3359 100644 --- a/test/test_option_owner.py +++ b/test/test_option_owner.py @@ -3,7 +3,7 @@ do_autopath() from py.test import raises -from tiramisu.setting import owners +from tiramisu.setting import owners, groups from tiramisu.config import Config from tiramisu.option import ChoiceOption, BoolOption, IntOption, FloatOption, \ StrOption, OptionDescription, SymLinkOption @@ -131,6 +131,7 @@ def test_setowner_optiondescription(): descr1 = OptionDescription('tiramisu', '', [gcdummy]) descr = OptionDescription('tiramisu', '', [descr1]) cfg = Config(descr) + cfg raises(ConfigError, 'cfg.cfgimpl_get_values().getowner(descr1)') raises(ConfigError, 'cfg.cfgimpl_get_values().setowner(descr1, owners.user)') @@ -145,3 +146,18 @@ def test_setowner_symlinkoption(): cfg.tiramisu.dummy = True assert cfg.cfgimpl_get_values().getowner(s) == owners.user raises(ConfigError, 'cfg.cfgimpl_get_values().setowner(s, owners.user)') + + +def test_owner_masterslaves(): + b = IntOption('int', 'Test int option', default=[0], multi=True) + c = StrOption('str', 'Test string option', multi=True) + descr = OptionDescription("int", "", [b, c]) + descr.impl_set_group_type(groups.master) + cfg = Config(descr) + raises(ConfigError, 'cfg.cfgimpl_get_values().setowner(c, owners.user)') + + cfg.int.append(1) + cfg.str[0] = 'yes' + assert cfg.cfgimpl_get_values().getowner(c, 0) == owners.user + cfg.cfgimpl_get_values().setowner(c, owners.user, 0) + assert cfg.cfgimpl_get_values().getowner(c, 0) == owners.user diff --git a/tiramisu/storage/dictionary/value.py b/tiramisu/storage/dictionary/value.py index 97085dc..ef4ad19 100644 --- a/tiramisu/storage/dictionary/value.py +++ b/tiramisu/storage/dictionary/value.py @@ -115,10 +115,12 @@ class Values(Cache): idx = self._values[0].index(path) if isinstance(self._values[3][idx], list): if index is None: - raise ValueError('list but no index') - owner = list(self._values[3][idx])[index] = owner + raise ValueError('Slave need index to set owner') + towner = list(self._values[3][idx]) + towner[index] = owner + owner = towner elif index is not None: - raise ValueError('index set but not a list') + raise ValueError('Only slave option needs index to set owner') lst = list(self._values[3]) lst[idx] = owner values = list(self._values) diff --git a/tiramisu/value.py b/tiramisu/value.py index 0f5d283..9740a44 100644 --- a/tiramisu/value.py +++ b/tiramisu/value.py @@ -506,7 +506,7 @@ class Values(object): only_default=only_default, index=index) return owner - def setowner(self, opt, owner): + def setowner(self, opt, owner, index=None): """ sets a owner to an option @@ -523,10 +523,11 @@ class Values(object): props = self._getcontext().cfgimpl_get_settings().validate_properties(opt, False, True, - path) + path, + index=index) if props: raise props - self._p_.setowner(path, owner) + self._p_.setowner(path, owner, index=index) def is_default_owner(self, opt, validate_properties=True, validate_meta=True, index=None): @@ -752,7 +753,8 @@ class Multi(list): True, False, True, index=index) - def append(self, value=undefined, force=False, setitem=True, validate=True): + def append(self, value=undefined, force=False, setitem=True, validate=True, + force_permissive=False): """the list value can be updated (appened) only if the option is a master """ @@ -769,7 +771,8 @@ class Multi(list): if 'validator' in setting_properties: fake_context = context._gen_fake_values() fake_multi = fake_context.cfgimpl_get_values()._get_cached_value( - self.opt, path=self.path, validate=False) + self.opt, path=self.path, validate=False, + force_permissive=force_permissive) fake_multi.append(value, validate=False, force=True) self._validate(value, fake_context, index, True) if not '_index' in self.__slots__ and self.opt.impl_is_submulti():