From 5b5f06a612bb0a2742f8511182479014e4d90eff Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sun, 7 Oct 2018 15:54:08 +0200 Subject: [PATCH] do same api for property and permissive --- test/test_masterslaves.py | 2 +- test/test_option.py | 6 +- test/test_option_setting.py | 10 ---- test/test_permissive.py | 81 ++++++++++++++++++++++++++ test/test_symlink.py | 4 +- tiramisu/api.py | 69 ++++++++++++---------- tiramisu/setting.py | 35 ++++++----- tiramisu/storage/dictionary/setting.py | 11 ++-- tiramisu/storage/sqlite3/setting.py | 8 ++- 9 files changed, 159 insertions(+), 67 deletions(-) diff --git a/test/test_masterslaves.py b/test/test_masterslaves.py index dfd9b5f..f96eb08 100644 --- a/test/test_masterslaves.py +++ b/test/test_masterslaves.py @@ -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(): diff --git a/test/test_option.py b/test/test_option.py index ea8be5a..c4bc9aa 100644 --- a/test/test_option.py +++ b/test/test_option.py @@ -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(): diff --git a/test/test_option_setting.py b/test/test_option_setting.py index f2cb6e9..5a812c6 100644 --- a/test/test_option_setting.py +++ b/test/test_option_setting.py @@ -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])]) diff --git a/test/test_permissive.py b/test/test_permissive.py index dcc3921..3e9ace8 100644 --- a/test/test_permissive.py +++ b/test/test_permissive.py @@ -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()") diff --git a/test/test_symlink.py b/test/test_symlink.py index 255b910..eaa701f 100644 --- a/test/test_symlink.py +++ b/test/test_symlink.py @@ -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(): diff --git a/tiramisu/api.py b/tiramisu/api.py index a004ad9..3bf0d0a 100644 --- a/tiramisu/api.py +++ b/tiramisu/api.py @@ -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'): - 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)) + 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)) 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], @@ -358,17 +356,17 @@ class TiramisuOptionPermissive(CommonTiramisuOption): def get(self): """Get permissives value""" return self._settings.getpermissives(self._option_bag.option, - self._option_bag.path) + self._option_bag.path) 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): @@ -953,9 +945,29 @@ class TiramisuContextPermissive(TiramisuContext): """Import config permissives""" self._config_bag.context.cfgimpl_get_settings()._pp_.importation(permissives) self._config_bag.context.cfgimpl_reset_cache(None, - None) + 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 diff --git a/tiramisu/setting.py b/tiramisu/setting.py index 4c487b3..94f6db7 100644 --- a/tiramisu/setting.py +++ b/tiramisu/setting.py @@ -659,25 +659,32 @@ 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() + assert not opt.impl_is_symlinkoption(), _("can't reset properties to " + "the symlinkoption \"{}\"" + "").format(opt.impl_get_display_name()) + path = option_bag.path + 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: - if opt is not None: - path = option_bag.path - else: - path = None - self._p_.delproperties(path) + 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) #____________________________________________________________ diff --git a/tiramisu/storage/dictionary/setting.py b/tiramisu/storage/dictionary/setting.py index 4546187..e742840 100644 --- a/tiramisu/storage/dictionary/setting.py +++ b/tiramisu/storage/dictionary/setting.py @@ -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]) diff --git a/tiramisu/storage/sqlite3/setting.py b/tiramisu/storage/sqlite3/setting.py index dcece1f..0089966 100644 --- a/tiramisu/storage/sqlite3/setting.py +++ b/tiramisu/storage/sqlite3/setting.py @@ -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'])}