do same api for property and permissive

This commit is contained in:
Emmanuel Garette 2018-10-07 15:54:08 +02:00
parent 290f687f6c
commit 5b5f06a612
9 changed files with 159 additions and 67 deletions

View File

@ -191,7 +191,7 @@ def test_iter_on_empty_group():
def test_iter_not_group():
api = Config(OptionDescription("name", "descr", []))
api.property.read_write()
raises(TypeError, "list(api.option.list(type='optiondescription', group_type='family'))")
raises(AssertionError, "list(api.option.list(type='optiondescription', group_type='family'))")
def test_groups_with_master():

View File

@ -140,7 +140,8 @@ def test_optiondescription_list():
assert len(list(api.option('od').list('optiondescription', group_type=groups.notfamily1))) == 1
assert len(list(api.option('od.od').list('option'))) == 1
assert len(list(api.option('od.od2').list('option'))) == 1
raises(APIError, "list(api.option.list('unknown'))")
raises(AssertionError, "list(api.option('od').list('unknown'))")
raises(AssertionError, "list(api.option('od').list('option', group_type='toto'))")
def test_optiondescription_group():
@ -157,7 +158,8 @@ def test_optiondescription_group():
assert len(list(api.option.list('optiondescription'))) == 2
assert len(list(api.option.list('optiondescription', group_type=groups.family))) == 1
assert len(list(api.option.list('optiondescription', group_type=groups.notfamily))) == 1
raises(APIError, "list(api.option.list('unknown'))")
raises(AssertionError, "list(api.option.list('unknown'))")
raises(AssertionError, "list(api.option.list('option', group_type='toto'))")
def test_optiondescription_group_redefined():

View File

@ -421,16 +421,6 @@ def test_reset_properties():
assert api.option('gc.dummy').property.get() == set()
def test_reset_properties_all():
descr = make_description()
api = Config(descr)
assert api.option('gc.dummy').property.get() == set()
api.option('gc.dummy').property.add('frozen')
assert api.option('gc.dummy').property.get() == {'frozen'}
api.property.reset(all=True)
assert api.option('gc.dummy').property.get() == set()
def test_properties_cached():
b1 = BoolOption("b1", "", properties=('test',))
descr = OptionDescription("opt", "", [OptionDescription("sub", "", [b1])])

View File

@ -45,6 +45,72 @@ def test_permissive():
assert set(props) == {'disabled'}
def test_permissive_add():
descr = make_description()
api = Config(descr)
api.property.read_write()
api.property.read_write()
props = frozenset()
try:
api.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
api.unrestraint.permissive.add('disabled')
assert api.unrestraint.permissive.get() == frozenset(['hidden', 'disabled'])
props = frozenset()
try:
api.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
api.property.add('permissive')
api.option('u1').value.get()
api.property.pop('permissive')
props = frozenset()
try:
api.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
def test_permissive_pop():
descr = make_description()
api = Config(descr)
api.property.read_write()
api.property.read_write()
props = frozenset()
try:
api.forcepermissive.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
api.unrestraint.permissive.add('disabled')
assert api.unrestraint.permissive.get() == frozenset(['hidden', 'disabled'])
api.forcepermissive.option('u1').value.get()
api.unrestraint.permissive.pop('disabled')
props = frozenset()
try:
api.forcepermissive.option('u1').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert set(props) == {'disabled'}
def test_permissive_reset():
descr = make_description()
api = Config(descr)
api.property.read_write()
assert api.unrestraint.permissive.get() == frozenset(['hidden'])
#
api.unrestraint.permissive.set(frozenset(['disabled']))
assert api.unrestraint.permissive.get() == frozenset(['disabled'])
#
api.unrestraint.permissive.reset()
assert api.unrestraint.permissive.get() == frozenset()
def test_permissive_mandatory():
descr = make_description()
api = Config(descr)
@ -290,3 +356,18 @@ def test_remove_option_permissive():
api.forcepermissive.option('od1.var1').permissive.set(frozenset())
assert api.forcepermissive.option('od1.var1').permissive.get() == frozenset()
raises(PropertiesOptionError, "api.option('od1.var1').value.get()")
def test_reset_option_permissive():
var1 = UnicodeOption('var1', '', u'value', properties=('hidden',))
od1 = OptionDescription('od1', '', [var1])
rootod = OptionDescription('rootod', '', [od1])
api = Config(rootod)
api.property.read_write()
raises(PropertiesOptionError, "api.option('od1.var1').value.get()")
api.forcepermissive.option('od1.var1').permissive.set(frozenset(['hidden']))
assert api.forcepermissive.option('od1.var1').permissive.get() == frozenset(['hidden'])
assert api.option('od1.var1').value.get() == 'value'
api.forcepermissive.option('od1.var1').permissive.reset()
assert api.forcepermissive.option('od1.var1').permissive.get() == frozenset()
raises(PropertiesOptionError, "api.option('od1.var1').value.get()")

View File

@ -60,7 +60,7 @@ def test_symlink_addproperties():
api = Config(descr)
api.property.read_write()
raises(TypeError, "api.option('c').property.add('new')")
raises(TypeError, "api.option('c').property.reset()")
raises(AssertionError, "api.option('c').property.reset()")
def test_symlink_getpermissive():
@ -80,7 +80,7 @@ def test_symlink_addpermissive():
api = Config(descr)
api.property.read_write()
raises(TypeError, "api.option('c').permissive.set(frozenset(['new']))")
raises(TypeError, "api.option('c').permissive.reset()")
raises(AssertionError, "api.option('c').permissive.reset()")
def test_symlink_getproperties():

View File

@ -123,11 +123,10 @@ class CommonTiramisuOption(CommonTiramisu):
def _test_slave_index(self) -> None:
option = self._option_bag.option
if not option.impl_is_optiondescription():
if self._option_bag.index is None and option.impl_is_master_slaves('slave'):
if not option.impl_is_optiondescription() and \
self._option_bag.index is None and \
option.impl_is_master_slaves('slave'):
raise APIError(_('index must be set with the slave option "{}"').format(self._option_bag.path))
elif self._option_bag.index is not None and not option.impl_is_master_slaves('slave'):
raise APIError(_('index must be set only with a slave option, not for "{}"').format(self._option_bag.path))
def __getattr__(self, name):
raise APIError(_('unknown method {}').format(name))
@ -344,7 +343,6 @@ class TiramisuOptionPermissive(CommonTiramisuOption):
_allow_optiondescription = True
_slave_need_index = False
# FIXME should have same api than property
def __init__(self,
name: str,
subconfig: Union[KernelConfig, SubConfig],
@ -362,13 +360,13 @@ class TiramisuOptionPermissive(CommonTiramisuOption):
def set(self, permissives):
"""Set permissives value"""
option = self._option_bag.option
self._settings.setpermissives(self._option_bag,
permissives=permissives)
def reset(self):
"""Reset all personalised permissive"""
self.set(frozenset())
self._settings.reset_permissives(self._option_bag,
self._option_bag.config_bag.context)
class TiramisuOptionInformation(CommonTiramisuOption):
@ -662,11 +660,9 @@ class _TiramisuOptionDescription(_TiramisuOption):
type='option',
group_type=None):
"""List options in an optiondescription (only for optiondescription)"""
if type not in ('all', 'option', 'optiondescription'):
raise APIError(_('unknown list type {}').format(type))
if group_type is not None and not isinstance(group_type,
groups.GroupType):
raise TypeError(_("unknown group_type: {0}").format(group_type))
assert type in ('all', 'option', 'optiondescription'), _('unknown list type {}').format(type)
assert group_type is None or isinstance(group_type, groups.GroupType), \
_("unknown group_type: {0}").format(group_type)
def _filter(opt):
if self._config_bag.properties:
name = opt.impl_getname()
@ -888,22 +884,20 @@ class TiramisuContextProperty(TiramisuContext):
def add(self, prop):
"""Add a config property"""
props = self.get()
props = set(self.get())
props.add(prop)
self.set(frozenset(props))
del self._config_bag.properties
def pop(self, prop):
"""Remove a config property"""
props = self.get()
props = set(self.get())
if prop in props:
props.remove(prop)
self.set(frozenset(props))
del self._config_bag.properties
def get(self):
"""Get all config properties"""
return set(self._config_bag.properties)
return self._config_bag.properties
def set(self, props):
"""Personalise config properties"""
@ -912,13 +906,11 @@ class TiramisuContextProperty(TiramisuContext):
context)
del self._config_bag.properties
def reset(self,
all=False):
def reset(self):
"""Remove config properties"""
context = self._config_bag.context
context.cfgimpl_get_settings().reset(None,
context,
all_properties=all)
context)
del self._config_bag.properties
def exportation(self):
@ -956,6 +948,26 @@ class TiramisuContextPermissive(TiramisuContext):
None)
del self._config_bag.permissives
def reset(self):
"""Remove config permissives"""
context = self._config_bag.context
context.cfgimpl_get_settings().reset_permissives(None,
context)
del self._config_bag.properties
def add(self, prop):
"""Add a config permissive"""
props = set(self.get())
props.add(prop)
self.set(frozenset(props))
def pop(self, prop):
"""Remove a config permissive"""
props = set(self.get())
if prop in props:
props.remove(prop)
self.set(frozenset(props))
class TiramisuContextOption(TiramisuContext):
@ -1026,12 +1038,9 @@ class TiramisuContextOption(TiramisuContext):
None,
subconfig,
self._config_bag)
if type not in ('all', 'option', 'optiondescription'):
raise APIError(_('unknown list type {}').format(type))
if group_type is not None and not isinstance(group_type,
groups.GroupType):
raise TypeError(_("unknown group_type: {0}").format(group_type))
assert type in ('all', 'option', 'optiondescription'), _('unknown list type {}').format(type)
assert group_type is None or isinstance(group_type, groups.GroupType), \
_("unknown group_type: {0}").format(group_type)
option = self._config_bag.context.cfgimpl_get_description()
for toption in _walk(option):
yield toption

View File

@ -659,27 +659,34 @@ class Settings(object):
def reset(self,
option_bag,
context,
all_properties=False):
context):
if option_bag is None:
opt = None
path = None
else:
opt = option_bag.option
assert all_properties is False or option_bag is None, _('opt and all_properties must not be'
' set together in reset')
if opt and opt.impl_is_symlinkoption():
raise TypeError(_("can't reset properties to the symlinkoption \"{}\""
"").format(opt.impl_get_display_name()))
if all_properties:
self._p_.reset_all_properties()
else:
if opt is not None:
assert not opt.impl_is_symlinkoption(), _("can't reset properties to "
"the symlinkoption \"{}\""
"").format(opt.impl_get_display_name())
path = option_bag.path
else:
path = None
self._p_.delproperties(path)
context.cfgimpl_reset_cache(option_bag)
def reset_permissives(self,
option_bag,
context):
if option_bag is None:
opt = None
path = None
else:
opt = option_bag.option
assert not opt.impl_is_symlinkoption(), _("can't reset permissives to "
"the symlinkoption \"{}\""
"").format(opt.impl_get_display_name())
path = option_bag.path
self._pp_.delpermissive(path)
context.cfgimpl_reset_cache(option_bag)
#____________________________________________________________
# validate properties
def calc_raises_properties(self,

View File

@ -44,11 +44,6 @@ class Properties(Cache):
print('getproperties', path, ret)
return ret
def reset_all_properties(self):
if DEBUG: # pragma: no cover
print('reset_all_properties')
self._properties.clear()
def delproperties(self, path):
if DEBUG: # pragma: no cover
print('delproperties', path)
@ -96,3 +91,9 @@ class Permissives(Cache):
def importation(self, permissives):
self._permissives = permissives
def delpermissive(self, path):
if DEBUG: # pragma: no cover
print('delpermissive', path)
if path in self._permissives:
del(self._permissives[path])

View File

@ -48,9 +48,6 @@ class Properties(Sqlite3DB):
else:
return set(self._sqlite_decode(value[0]))
def reset_all_properties(self):
self._storage.execute("DELETE FROM property WHERE session_id = ?", (self._session_id,))
def delproperties(self, path):
path = self._sqlite_encode_path(path)
self._storage.execute("DELETE FROM property WHERE path = ? AND session_id = ?",
@ -110,6 +107,11 @@ class Permissives(Sqlite3DB):
print('getpermissive', path, ret, id(self))
return ret
def delpermissive(self, path):
path = self._sqlite_encode_path(path)
self._storage.execute("DELETE FROM permissive WHERE path = ? AND session_id = ?",
(path, self._session_id))
def exportation(self):
"""return all modified permissives in a dictionary
example: {'path1': set(['perm1', 'perm2'])}