From 6e8b570a372d25dbcba50ac2247abb4a8ac20b53 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sun, 9 Mar 2014 20:06:44 +0100 Subject: [PATCH] mandatory_warnings is now in values and add force_cache to values --- test/test_cache.py | 19 +++++++++++++++++++ test/test_mandatory.py | 18 +++++++++--------- tiramisu/config.py | 18 ++---------------- tiramisu/value.py | 38 +++++++++++++++++++++++++++++++++++++- 4 files changed, 67 insertions(+), 26 deletions(-) diff --git a/test/test_cache.py b/test/test_cache.py index f483c76..ef4f1a3 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -253,3 +253,22 @@ def test_reset_cache_only(): c.cfgimpl_reset_cache(only=('settings',)) assert 'u1' in values._p_.get_cached(c) assert 'u1' not in settings._p_.get_cached(c) + + +def test_force_cache(): + u1 = IntOption('u1', '', multi=True) + u2 = IntOption('u2', '') + u3 = IntOption('u3', '', multi=True) + u4 = IntOption('u4', '', properties=('disabled',)) + od = OptionDescription('od1', '', [u1, u2, u3, u4]) + c = Config(od) + c.cfgimpl_get_settings().remove('expire') + + c.cfgimpl_get_values().force_cache() + assert c.cfgimpl_get_values()._p_.get_cached(c) == {'u1': ([], None), 'u3': ([], None), 'u2': (None, None), 'u4': (None, None)} + assert c.cfgimpl_get_settings()._p_.get_cached(c) == {'u4': (set(['disabled']), None), 'u1': (set([]), None), 'u3': (set([]), None), 'u2': (set([]), None)} + c.read_only() + + c.cfgimpl_get_values().force_cache() + assert c.cfgimpl_get_values()._p_.get_cached(c) == {'u1': ([], None), 'u3': ([], None), 'u2': (None, None)} + assert c.cfgimpl_get_settings()._p_.get_cached(c) == {'u4': (set(['disabled']), None), 'u1': (set([]), None), 'u3': (set([]), None), 'u2': (set([]), None)} diff --git a/test/test_mandatory.py b/test/test_mandatory.py index c535e31..5b9878a 100644 --- a/test/test_mandatory.py +++ b/test/test_mandatory.py @@ -1,7 +1,7 @@ import autopath #from py.test import raises -from tiramisu.config import Config, mandatory_warnings +from tiramisu.config import Config from tiramisu.option import StrOption, UnicodeOption, OptionDescription from tiramisu.error import PropertiesOptionError @@ -205,11 +205,11 @@ def test_mandatory_warnings_ro(): except PropertiesOptionError as err: proc = err.proptype assert proc == ['mandatory'] - assert list(mandatory_warnings(config)) == ['str', 'str1', 'unicode2', 'str3'] + assert list(config.cfgimpl_get_values().mandatory_warnings()) == ['str', 'str1', 'unicode2', 'str3'] config.read_write() config.str = 'a' config.read_only() - assert list(mandatory_warnings(config)) == ['str1', 'unicode2', 'str3'] + assert list(config.cfgimpl_get_values().mandatory_warnings()) == ['str1', 'unicode2', 'str3'] def test_mandatory_warnings_rw(): @@ -218,9 +218,9 @@ def test_mandatory_warnings_rw(): config.str = '' config.read_write() config.str - assert list(mandatory_warnings(config)) == ['str', 'str1', 'unicode2', 'str3'] + assert list(config.cfgimpl_get_values().mandatory_warnings()) == ['str', 'str1', 'unicode2', 'str3'] config.str = 'a' - assert list(mandatory_warnings(config)) == ['str1', 'unicode2', 'str3'] + assert list(config.cfgimpl_get_values().mandatory_warnings()) == ['str1', 'unicode2', 'str3'] def test_mandatory_warnings_disabled(): @@ -230,9 +230,9 @@ def test_mandatory_warnings_disabled(): setting = config.cfgimpl_get_settings() config.read_write() config.str - assert list(mandatory_warnings(config)) == ['str', 'str1', 'unicode2', 'str3'] + assert list(config.cfgimpl_get_values().mandatory_warnings()) == ['str', 'str1', 'unicode2', 'str3'] setting[descr.str].append('disabled') - assert list(mandatory_warnings(config)) == ['str1', 'unicode2', 'str3'] + assert list(config.cfgimpl_get_values().mandatory_warnings()) == ['str1', 'unicode2', 'str3'] def test_mandatory_warnings_frozen(): @@ -242,7 +242,7 @@ def test_mandatory_warnings_frozen(): setting = config.cfgimpl_get_settings() config.read_write() config.str - assert list(mandatory_warnings(config)) == ['str', 'str1', 'unicode2', 'str3'] + assert list(config.cfgimpl_get_values().mandatory_warnings()) == ['str', 'str1', 'unicode2', 'str3'] setting[descr.str].append('frozen') config.read_only() - assert list(mandatory_warnings(config)) == ['str', 'str1', 'unicode2', 'str3'] + assert list(config.cfgimpl_get_values().mandatory_warnings()) == ['str', 'str1', 'unicode2', 'str3'] diff --git a/tiramisu/config.py b/tiramisu/config.py index c9fb992..473cdb4 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -716,19 +716,5 @@ class MetaConfig(GroupConfig): def mandatory_warnings(config): - """convenience function to trace Options that are mandatory and - where no value has been set - - :returns: generator of mandatory Option's path - - """ - #if value in cache, properties are not calculated - config.cfgimpl_reset_cache(only=('values',)) - for path in config.cfgimpl_get_description().impl_getpaths( - include_groups=True): - try: - config._getattr(path, force_properties=frozenset(('mandatory',))) - except PropertiesOptionError as err: - if err.proptype == ['mandatory']: - yield path - config.cfgimpl_reset_cache(only=('values',)) + #only for retro-compatibility + config.cfgimpl_get_values().mandatory_warnings() diff --git a/tiramisu/value.py b/tiramisu/value.py index ed70f90..c07ddfc 100644 --- a/tiramisu/value.py +++ b/tiramisu/value.py @@ -21,7 +21,7 @@ from time import time from copy import copy import sys import weakref -from tiramisu.error import ConfigError, SlaveError +from tiramisu.error import ConfigError, SlaveError, PropertiesOptionError from tiramisu.setting import owners, multitypes, expires_time, undefined from tiramisu.autolib import carry_out_calculation from tiramisu.i18n import _ @@ -395,6 +395,42 @@ class Values(object): raise ValueError(_("information's item" " not found: {0}").format(key)) + def mandatory_warnings(self): + """convenience function to trace Options that are mandatory and + where no value has been set + + :returns: generator of mandatory Option's path + + """ + #if value in cache, properties are not calculated + self.reset_cache(False) + context = self.context() + for path in context.cfgimpl_get_description().impl_getpaths( + include_groups=True): + try: + context._getattr(path, + force_properties=frozenset(('mandatory',))) + except PropertiesOptionError as err: + if err.proptype == ['mandatory']: + yield path + self.reset_cache(False) + + def force_cache(self): + """parse all option to force data in cache + """ + context = self.context() + if not 'cache' in context.cfgimpl_get_settings(): + raise PropertiesOptionError(_('can force cache only if cache ' + 'is actived in config')) + #remove all cached properties and value to update "expired" time + context.cfgimpl_reset_cache() + for path in context.cfgimpl_get_description().impl_getpaths( + include_groups=True): + try: + context._getattr(path) + except PropertiesOptionError: + pass + def __getstate__(self): return {'_p_': self._p_}