cannot append properties for option with forbidden_set_properties

This commit is contained in:
Emmanuel Garette 2016-01-25 15:49:40 +01:00
parent 2842abdd46
commit f4307b9122
2 changed files with 53 additions and 6 deletions

View File

@ -429,3 +429,45 @@ def test_properties_cached():
option option
c._setattr('sub.b1', True, force_permissive=True) c._setattr('sub.b1', True, force_permissive=True)
assert str(setting[b1]) in ["['test']", "[u'test']"] assert str(setting[b1]) in ["['test']", "[u'test']"]
def test_append_properties_force_store_value():
gcdummy = BoolOption('dummy', 'dummy', default=False, properties=('force_store_value',))
gcgroup = OptionDescription('gc', '', [gcdummy])
descr = OptionDescription('tiramisu', '', [gcgroup])
cfg = Config(descr)
setting = cfg.cfgimpl_get_settings()
option = cfg.cfgimpl_get_description().gc.dummy
assert tuple(option._properties) == tuple(['force_store_value'])
assert not 'test' in setting[option]
setting[option].append('test')
assert tuple(option._properties) == tuple(['force_store_value'])
assert 'test' in setting[option]
def test_reset_properties_force_store_value():
gcdummy = BoolOption('dummy', 'dummy', default=False, properties=('force_store_value',))
gcgroup = OptionDescription('gc', '', [gcdummy])
descr = OptionDescription('tiramisu', '', [gcgroup])
cfg = Config(descr)
setting = cfg.cfgimpl_get_settings()
option = cfg.cfgimpl_get_description().gc.dummy
assert setting._p_.get_modified_properties() == {}
setting.append('frozen')
assert setting._p_.get_modified_properties() == {None: set(('frozen', 'expire', 'cache', 'validator', 'warnings'))}
setting.reset()
assert setting._p_.get_modified_properties() == {}
setting[option].append('test')
assert setting._p_.get_modified_properties() == {'gc.dummy': set(('test', 'force_store_value'))}
setting.reset()
assert setting._p_.get_modified_properties() == {'gc.dummy': set(('test', 'force_store_value'))}
setting.append('frozen')
assert setting._p_.get_modified_properties() == {None: set(('frozen', 'expire', 'validator', 'cache', 'warnings')), 'gc.dummy': set(('test', 'force_store_value'))}
setting.reset(option)
assert setting._p_.get_modified_properties() == {None: set(('frozen', 'expire', 'validator', 'cache', 'warnings'))}
setting[option].append('test')
assert setting._p_.get_modified_properties() == {None: set(('frozen', 'expire', 'validator', 'cache', 'warnings')), 'gc.dummy': set(('test', 'force_store_value'))}
setting.reset(all_properties=True)
assert setting._p_.get_modified_properties() == {}
raises(ValueError, 'setting.reset(all_properties=True, opt=option)')

View File

@ -263,9 +263,11 @@ class Property(object):
raise ValueError(_('cannot append {0} property for option {1}: ' raise ValueError(_('cannot append {0} property for option {1}: '
'this property is calculated').format( 'this property is calculated').format(
propname, self._opt.impl_getname())) propname, self._opt.impl_getname()))
if propname in forbidden_set_properties:
raise ConfigError(_('cannot add those properties: {0}').format(propname))
self._properties.add(propname) self._properties.add(propname)
if save: if save:
self._setting._setproperties(self._properties, self._path) self._setting._setproperties(self._properties, self._path, force=True)
def remove(self, propname): def remove(self, propname):
"""Removes a property named propname """Removes a property named propname
@ -417,14 +419,17 @@ class Settings(object):
for propname in propnames: for propname in propnames:
self.append(propname) self.append(propname)
def _setproperties(self, properties, path): def _setproperties(self, properties, path, force=False):
"""save properties for specified path """save properties for specified path
(never save properties if same has option properties) (never save properties if same has option properties)
""" """
forbidden_properties = forbidden_set_properties & properties if not force:
if forbidden_properties: forbidden_properties = forbidden_set_properties & properties
raise ConfigError(_('cannot add those properties: {0}').format( if forbidden_properties:
' '.join(forbidden_properties))) print "super ..."
raise Exception('pouet')
raise ConfigError(_('cannot add those properties: {0}').format(
' '.join(forbidden_properties)))
self._p_.setproperties(path, properties) self._p_.setproperties(path, properties)
self._getcontext().cfgimpl_reset_cache() self._getcontext().cfgimpl_reset_cache()