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(): def test_iter_not_group():
api = Config(OptionDescription("name", "descr", [])) api = Config(OptionDescription("name", "descr", []))
api.property.read_write() 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(): 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').list('optiondescription', group_type=groups.notfamily1))) == 1
assert len(list(api.option('od.od').list('option'))) == 1 assert len(list(api.option('od.od').list('option'))) == 1
assert len(list(api.option('od.od2').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(): 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'))) == 2
assert len(list(api.option.list('optiondescription', group_type=groups.family))) == 1 assert len(list(api.option.list('optiondescription', group_type=groups.family))) == 1
assert len(list(api.option.list('optiondescription', group_type=groups.notfamily))) == 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(): def test_optiondescription_group_redefined():

View File

@ -421,16 +421,6 @@ def test_reset_properties():
assert api.option('gc.dummy').property.get() == set() 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(): def test_properties_cached():
b1 = BoolOption("b1", "", properties=('test',)) b1 = BoolOption("b1", "", properties=('test',))
descr = OptionDescription("opt", "", [OptionDescription("sub", "", [b1])]) descr = OptionDescription("opt", "", [OptionDescription("sub", "", [b1])])

View File

@ -45,6 +45,72 @@ def test_permissive():
assert set(props) == {'disabled'} 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(): def test_permissive_mandatory():
descr = make_description() descr = make_description()
api = Config(descr) api = Config(descr)
@ -290,3 +356,18 @@ def test_remove_option_permissive():
api.forcepermissive.option('od1.var1').permissive.set(frozenset()) api.forcepermissive.option('od1.var1').permissive.set(frozenset())
assert api.forcepermissive.option('od1.var1').permissive.get() == frozenset() assert api.forcepermissive.option('od1.var1').permissive.get() == frozenset()
raises(PropertiesOptionError, "api.option('od1.var1').value.get()") 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 = Config(descr)
api.property.read_write() api.property.read_write()
raises(TypeError, "api.option('c').property.add('new')") 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(): def test_symlink_getpermissive():
@ -80,7 +80,7 @@ def test_symlink_addpermissive():
api = Config(descr) api = Config(descr)
api.property.read_write() api.property.read_write()
raises(TypeError, "api.option('c').permissive.set(frozenset(['new']))") 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(): def test_symlink_getproperties():

View File

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

View File

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

View File

@ -44,11 +44,6 @@ class Properties(Cache):
print('getproperties', path, ret) print('getproperties', path, ret)
return ret return ret
def reset_all_properties(self):
if DEBUG: # pragma: no cover
print('reset_all_properties')
self._properties.clear()
def delproperties(self, path): def delproperties(self, path):
if DEBUG: # pragma: no cover if DEBUG: # pragma: no cover
print('delproperties', path) print('delproperties', path)
@ -96,3 +91,9 @@ class Permissives(Cache):
def importation(self, permissives): def importation(self, permissives):
self._permissives = 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: else:
return set(self._sqlite_decode(value[0])) 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): def delproperties(self, path):
path = self._sqlite_encode_path(path) path = self._sqlite_encode_path(path)
self._storage.execute("DELETE FROM property WHERE path = ? AND session_id = ?", self._storage.execute("DELETE FROM property WHERE path = ? AND session_id = ?",
@ -110,6 +107,11 @@ class Permissives(Sqlite3DB):
print('getpermissive', path, ret, id(self)) print('getpermissive', path, ret, id(self))
return ret 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): def exportation(self):
"""return all modified permissives in a dictionary """return all modified permissives in a dictionary
example: {'path1': set(['perm1', 'perm2'])} example: {'path1': set(['perm1', 'perm2'])}