From dc84608348503f0cef9bb7bad9d57bb0124915cc Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Tue, 3 Apr 2018 15:51:45 +0200 Subject: [PATCH] allow force_default_on_freeze for master if frozen --- test/test_option_default.py | 9 +++++ tiramisu/api.py | 26 +++++++++----- tiramisu/option/optiondescription.py | 9 +++-- tiramisu/setting.py | 52 ++++------------------------ tiramisu/value.py | 3 +- 5 files changed, 42 insertions(+), 57 deletions(-) diff --git a/test/test_option_default.py b/test/test_option_default.py index b113fef..1abebe4 100644 --- a/test/test_option_default.py +++ b/test/test_option_default.py @@ -115,6 +115,15 @@ def test_force_default_on_freeze_master(): raises(ConfigError, "Config(descr)") +def test_force_default_on_freeze_master_frozen(): + dummy1 = BoolOption('dummy1', 'Test int option', multi=True, properties=('force_default_on_freeze', 'frozen')) + dummy2 = BoolOption('dummy2', 'Test string option', multi=True) + descr = MasterSlaves("dummy1", "", [dummy1, dummy2]) + descr = OptionDescription("root", "", [descr]) + api = getapi(Config(descr)) + raises(ConfigError, "api.option('dummy1.dummy1').property.pop('frozen')") + + def test_force_default_on_freeze_slave(): dummy1 = BoolOption('dummy1', 'Test int option', multi=True) dummy2 = BoolOption('dummy2', 'Test string option', multi=True, properties=('force_default_on_freeze',)) diff --git a/tiramisu/api.py b/tiramisu/api.py index 98c1204..672738f 100644 --- a/tiramisu/api.py +++ b/tiramisu/api.py @@ -17,7 +17,7 @@ from inspect import ismethod, getdoc from .error import APIError, ConfigError, SlaveError from .i18n import _ -from .setting import ConfigBag, owners, undefined +from .setting import ConfigBag, owners, undefined, FORBIDDEN_SET_PROPERTIES from .option import ChoiceOption from time import time from copy import deepcopy @@ -329,18 +329,28 @@ class TiramisuOptionProperty(CommonTiramisuOption): @count def add(self, prop): - #FIXME not index !! self._get_option() - self.settings.addproperty(self.path, - prop, - self.config_bag) + if prop in FORBIDDEN_SET_PROPERTIES: + raise ConfigError(_('cannot add this property: "{0}"').format( + ' '.join(prop))) + props = self.settings.getproperties(self.path, + None, + self.config_bag, + apply_requires=False) + self.settings.setproperties(self.path, + props | {prop}, + self.config_bag) @count def pop(self, prop): self._get_option() - self.settings.popproperty(self.path, - prop, - self.config_bag) + props = self.settings.getproperties(self.path, + self.index, + self.config_bag, + apply_requires=False) + self.settings.setproperties(self.path, + props - {prop}, + self.config_bag) @count def reset(self): diff --git a/tiramisu/option/optiondescription.py b/tiramisu/option/optiondescription.py index 7966580..98af09d 100644 --- a/tiramisu/option/optiondescription.py +++ b/tiramisu/option/optiondescription.py @@ -75,12 +75,15 @@ class CacheOptionDescription(BaseOption): option._set_readonly() is_multi = option.impl_is_multi() if not option.impl_is_symlinkoption(): - if 'force_store_value' in option.impl_getproperties(): + properties = option.impl_getproperties() + if 'force_store_value' in properties: force_store_values.append((subpath, option)) - if 'force_default_on_freeze' in option.impl_getproperties() and \ + if 'force_default_on_freeze' in properties and \ + 'frozen' not in properties and \ option.impl_is_master_slaves('master'): raise ConfigError(_('a master ({0}) cannot have ' - 'force_default_on_freeze property').format(subpath)) + '"force_default_on_freeze" property without "frozen"' + '').format(subpath)) for cons_id, func, all_cons_opts, params in option.get_consistencies(): option._valid_consistencies(all_cons_opts[1:], init=False) if func not in ALLOWED_CONST_LIST and is_multi: diff --git a/tiramisu/setting.py b/tiramisu/setting.py index bb9c34f..e113ae6 100644 --- a/tiramisu/setting.py +++ b/tiramisu/setting.py @@ -560,8 +560,7 @@ class Settings(object): def setproperties(self, path, properties, - config_bag, - force=False): + config_bag): """save properties for specified path (never save properties if same has option properties) """ @@ -587,11 +586,12 @@ class Settings(object): if opt and opt.impl_is_symlinkoption(): raise TypeError(_("can't assign properties to the SymLinkOption \"{}\"" "").format(opt.impl_get_display_name())) - if not force: - forbidden_properties = FORBIDDEN_SET_PROPERTIES & properties - if forbidden_properties: - raise ConfigError(_('cannot add those properties: {0}').format( - ' '.join(forbidden_properties))) + if 'force_default_on_freeze' in properties and \ + 'frozen' not in properties and \ + opt.impl_is_master_slaves('master'): + raise ConfigError(_('a master ({0}) cannot have ' + '"force_default_on_freeze" property without "frozen"' + '').format(opt.impl_get_display_name())) if not isinstance(properties, frozenset): raise TypeError(_('properties must be a frozenset')) self._p_.setproperties(path, @@ -600,44 +600,6 @@ class Settings(object): self._getcontext().cfgimpl_reset_cache(opt=opt, path=path) - def addproperty(self, - path, - property_, - config_bag): - if property_ in FORBIDDEN_SET_PROPERTIES: - raise ConfigError(_('cannot add this property: "{0}"').format( - ' '.join(property_))) - - self_properties = config_bag.properties - if self_properties is None: - index = None - self_properties = self.getproperties(path, - index, - config_bag, - apply_requires=False) - config_bag.properties = self_properties - self.setproperties(path, - self_properties | {property_}, - config_bag, - force=True) - - def popproperty(self, - path, - property_, - config_bag): - self_properties = config_bag.properties - if self_properties is None: - index = None - self_properties = self.getproperties(path, - index, - config_bag, - apply_requires=False) - config_bag.properties = self_properties - self.setproperties(path, - self_properties - {property_}, - config_bag, - force=True) - def set_context_permissive(self, permissive): self.setpermissive(None, None, permissive) diff --git a/tiramisu/value.py b/tiramisu/value.py index 4cf7ac0..21c9ea7 100644 --- a/tiramisu/value.py +++ b/tiramisu/value.py @@ -88,7 +88,8 @@ class Values(object): if setting_properties and 'cache' in setting_properties and \ self._p_.hascache(path, index): - if 'expire' in setting_properties or 'expire' in config_bag.properties: + if 'expire' in setting_properties or \ + (config_bag.properties and 'expire' in config_bag.properties): ntime = int(time()) is_cached, value = self._p_.getcache(path, ntime,