From be43c95ec0ebf329a16f94fde3fac9fa9dd2eef4 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Wed, 19 Jul 2017 20:44:38 +0200 Subject: [PATCH] In Config, name and session_id was quite equal, remove name session_id is now validate set_value return Configs with error add new_config to MetaConfig --- test/test_choice_option.py | 16 +++ test/test_config.py | 8 +- test/test_dyn_optiondescription.py | 6 +- test/test_metaconfig.py | 149 ++++++++++++++++++---------- test/test_state.py | 71 ++++++------- test/test_submulti.py | 4 +- tiramisu/config.py | 97 +++++++++++------- tiramisu/error.py | 2 + tiramisu/option/baseoption.py | 2 +- tiramisu/storage/__init__.py | 4 +- tiramisu/storage/sqlite3/storage.py | 1 - tiramisu/value.py | 4 +- 12 files changed, 224 insertions(+), 140 deletions(-) diff --git a/test/test_choice_option.py b/test/test_choice_option.py index 6094f2e..888d2f5 100644 --- a/test/test_choice_option.py +++ b/test/test_choice_option.py @@ -26,6 +26,22 @@ def return_error(): raise Exception('test') +def test_choiceoption(): + ch = ChoiceOption('ch', '', values=('val1', 'val2')) + od = OptionDescription('od', '', [ch]) + cfg = Config(od) + cfg.read_write() + owner = cfg.cfgimpl_get_settings().getowner() + assert cfg.getowner(ch) == owners.default + cfg.ch = 'val1' + assert cfg.getowner(ch) == owner + del(cfg.ch) + assert cfg.getowner(ch) == owners.default + raises(ValueError, "cfg.ch='no'") + assert cfg.getowner(ch) == owners.default + assert ch.impl_get_values(cfg) == ('val1', 'val2') + + def test_choiceoption_function(): ch = ChoiceOption('ch', '', values=return_list) od = OptionDescription('od', '', [ch]) diff --git a/test/test_config.py b/test/test_config.py index 5919df7..43e2714 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -52,9 +52,9 @@ def make_description(): def test_base_config_name(): gcdummy = BoolOption('dummy', 'dummy', default=False) descr = OptionDescription('tiramisu', '', [gcdummy]) - cfg = Config(descr, name='cfg') + cfg = Config(descr, session_id='cfg') cfg.impl_getname() == 'cfg' - raises(ValueError, "Config(descr, name='unvalid name')") + raises(ValueError, "Config(descr, session_id='unvalid name')") def test_not_config(): @@ -403,6 +403,6 @@ def test_config_subconfig(): i4 = IntOption('i4', '', default=2) od1 = OptionDescription('od1', '', [i1, i2, i3, i4]) od2 = OptionDescription('od2', '', [od1]) - conf1 = Config(od2, name='conf1') + conf1 = Config(od2, session_id='conf1') conf1 - raises(ConfigError, "conf2 = Config(od1, name='conf2')") + raises(ConfigError, "conf2 = Config(od1, session_id='conf2')") diff --git a/test/test_dyn_optiondescription.py b/test/test_dyn_optiondescription.py index 086f10c..64c82bf 100644 --- a/test/test_dyn_optiondescription.py +++ b/test/test_dyn_optiondescription.py @@ -1306,9 +1306,9 @@ def test_state_config(): od = OptionDescription('od', '', [st]) od2 = OptionDescription('od', '', [od, od1]) try: - cfg = Config(od2, persistent=True, session_id='29090938') + cfg = Config(od2, persistent=True, session_id='c29090938') except ValueError: - cfg = Config(od2, session_id='29090938') + cfg = Config(od2, session_id='c29090938') cfg._impl_test = True a = dumps(cfg) q = loads(a) @@ -1316,7 +1316,7 @@ def test_state_config(): _diff_conf(cfg, q) try: - delete_session('config', '29090938') + delete_session('config', 'c29090938') except ValueError: pass diff --git a/test/test_metaconfig.py b/test/test_metaconfig.py index 55fc8a8..e1034bc 100644 --- a/test/test_metaconfig.py +++ b/test/test_metaconfig.py @@ -6,7 +6,7 @@ from py.test import raises from tiramisu.setting import groups, owners from tiramisu.config import Config, GroupConfig, MetaConfig from tiramisu.option import IntOption, StrOption, NetworkOption, NetmaskOption, OptionDescription -from tiramisu.error import ConfigError, ConflictError +from tiramisu.error import ConfigError, ConflictError, PropertiesOptionError owners.addowner('meta') @@ -28,9 +28,9 @@ def make_description(): i6 = IntOption('i6', '', properties=('disabled',)) od1 = OptionDescription('od1', '', [i1, i2, i3, i4, i5, i6]) od2 = OptionDescription('od2', '', [od1]) - conf1 = Config(od2, name='conf1') - conf2 = Config(od2, name='conf2') - meta = MetaConfig([conf1, conf2], name='meta') + conf1 = Config(od2, session_id='conf3') + conf2 = Config(od2, session_id='conf4') + meta = MetaConfig([conf1, conf2], session_id='meta') meta.read_write() meta.cfgimpl_get_settings().setowner(owners.meta) return meta @@ -184,28 +184,32 @@ def test_not_meta(): i1 = IntOption('i1', '') od1 = OptionDescription('od1', '', [i1]) od2 = OptionDescription('od2', '', [od1]) - conf1 = Config(od2, name='conf1') - conf2 = Config(od2, name='conf2') + conf1 = Config(od2, session_id='conf1') + conf2 = Config(od2, session_id='conf2') conf3 = Config(od2) - conf4 = Config(od2, name='conf2') - conf3, conf4 - raises(ValueError, "GroupConfig(conf1)") - #same name - raises(ConflictError, "GroupConfig([conf2, conf4])") - grp = GroupConfig([conf1, conf2]) - raises(ConfigError, 'grp.od1.i1') - conf1, conf2 = grp.cfgimpl_get_children() - grp.set_value('od1.i1', 7) - assert grp.conf1.od1.i1 == conf2.od1.i1 == 7 - assert grp.conf1.getowner(grp.conf1.unwrap_from_path('od1.i1')) is grp.conf2.getowner(grp.conf2.unwrap_from_path('od1.i1')) is owners.user + try: + conf4 = Config(od2, session_id='conf2') + except ValueError: + pass + else: + conf3, conf4 + raises(ValueError, "GroupConfig(conf1)") + #same name + raises(ConflictError, "GroupConfig([conf2, conf4])") + grp = GroupConfig([conf1, conf2]) + raises(ConfigError, 'grp.od1.i1') + conf1, conf2 = grp.cfgimpl_get_children() + grp.set_value('od1.i1', 7) + assert grp.conf1.od1.i1 == conf2.od1.i1 == 7 + assert grp.conf1.getowner(grp.conf1.unwrap_from_path('od1.i1')) is grp.conf2.getowner(grp.conf2.unwrap_from_path('od1.i1')) is owners.user def test_group_find_firsts(): i1 = IntOption('i1', '') od1 = OptionDescription('od1', '', [i1]) od2 = OptionDescription('od2', '', [od1]) - conf1 = Config(od2, name='conf1') - conf2 = Config(od2, name='conf2') + conf1 = Config(od2, session_id='conf1') + conf2 = Config(od2, session_id='conf2') grp = GroupConfig([conf1, conf2]) assert [conf1, conf2] == grp.find_firsts(byname='i1').cfgimpl_get_children() @@ -214,15 +218,13 @@ def test_group_group(): i1 = IntOption('i1', '') od1 = OptionDescription('od1', '', [i1]) od2 = OptionDescription('od2', '', [od1]) - conf1 = Config(od2, name='conf1') - conf2 = Config(od2, name='conf2') - grp = GroupConfig([conf1, conf2]) - raises(ValueError, "GroupConfig([grp])") + conf1 = Config(od2, session_id='conf9') + conf2 = Config(od2, session_id='conf10') grp = GroupConfig([conf1, conf2], 'grp') grp2 = GroupConfig([grp]) grp2.set_value('od1.i1', 2) - assert grp2.grp.conf1.od1.i1 == 2 - assert grp2.grp.conf1.getowner(i1) == owners.user + assert grp2.grp.conf9.od1.i1 == 2 + assert grp2.grp.conf9.getowner(i1) == owners.user def test_meta_path(): @@ -239,10 +241,10 @@ def test_meta_unconsistent(): od1 = OptionDescription('od1', '', [i1, i2, i3, i4]) od2 = OptionDescription('od2', '', [od1]) od3 = OptionDescription('od3', '', [od1]) - conf1 = Config(od2, name='conf1') - conf2 = Config(od2, name='conf2') - conf3 = Config(od2, name='conf3') - conf4 = Config(od3, name='conf4') + conf1 = Config(od2, session_id='conf5') + conf2 = Config(od2, session_id='conf6') + conf3 = Config(od2, session_id='conf7') + conf4 = Config(od3, session_id='conf8') conf3, conf4 meta = MetaConfig([conf1, conf2]) meta.cfgimpl_get_settings().setowner(owners.meta) @@ -251,7 +253,7 @@ def test_meta_unconsistent(): raises(ValueError, "MetaConfig([conf1, conf3])") #not same descr raises(ValueError, "MetaConfig([conf3, conf4])") - raises(ConfigError, "meta.conf1.read_only()") + raises(ConfigError, "meta.conf5.read_only()") def test_meta_master_slaves(): @@ -259,8 +261,8 @@ def test_meta_master_slaves(): netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',)) interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) interface1.impl_set_group_type(groups.master) - conf1 = Config(interface1, name='conf1') - conf2 = Config(interface1, name='conf2') + conf1 = Config(interface1, session_id='conf1') + conf2 = Config(interface1, session_id='conf2') meta = MetaConfig([conf1, conf2]) meta.read_only() assert [conf1, conf2] == meta.find_firsts(byname='netmask_admin_eth0').cfgimpl_get_children() @@ -277,8 +279,8 @@ def test_meta_master_slaves_value(): netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',)) interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) interface1.impl_set_group_type(groups.master) - conf1 = Config(interface1, name='conf1') - conf2 = Config(interface1, name='conf2') + conf1 = Config(interface1, session_id='conf1') + conf2 = Config(interface1, session_id='conf2') meta = MetaConfig([conf1, conf2]) meta.conf1.ip_admin_eth0 = ['192.168.1.1'] assert meta.conf1.netmask_admin_eth0 == [None] @@ -299,8 +301,8 @@ def test_meta_master_slaves_value_default(): netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True) interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) interface1.impl_set_group_type(groups.master) - conf1 = Config(interface1, name='conf1') - conf2 = Config(interface1, name='conf2') + conf1 = Config(interface1, session_id='conf1') + conf2 = Config(interface1, session_id='conf2') meta = MetaConfig([conf1, conf2]) assert meta.conf1.netmask_admin_eth0 == [None] meta.ip_admin_eth0 = ['192.168.1.1'] @@ -318,8 +320,8 @@ def test_meta_master_slaves_owners(): netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',)) interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) interface1.impl_set_group_type(groups.master) - conf1 = Config(interface1, name='conf1') - conf2 = Config(interface1, name='conf2') + conf1 = Config(interface1, session_id='conf1') + conf2 = Config(interface1, session_id='conf2') meta = MetaConfig([conf1, conf2]) meta.cfgimpl_get_settings().setowner(owners.meta) assert meta.conf1.getowner(ip_admin_eth0) == owners.default @@ -349,9 +351,9 @@ def test_meta_force_default(): netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',)) interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) interface1.impl_set_group_type(groups.master) - conf1 = Config(interface1, name='conf1') + conf1 = Config(interface1, session_id='conf1') conf1.read_write() - conf2 = Config(interface1, name='conf2') + conf2 = Config(interface1, session_id='conf2') conf2.read_write() meta = MetaConfig([conf1, conf2]) meta.read_write() @@ -382,9 +384,9 @@ def test_meta_force_dont_change_value(): netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',)) interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) interface1.impl_set_group_type(groups.master) - conf1 = Config(interface1, name='conf1') + conf1 = Config(interface1, session_id='conf1') conf1.read_write() - conf2 = Config(interface1, name='conf2') + conf2 = Config(interface1, session_id='conf2') conf2.read_write() meta = MetaConfig([conf1, conf2]) meta.read_write() @@ -410,9 +412,9 @@ def test_meta_force_default_if_same(): netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',)) interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) interface1.impl_set_group_type(groups.master) - conf1 = Config(interface1, name='conf1') + conf1 = Config(interface1, session_id='conf1') conf1.read_write() - conf2 = Config(interface1, name='conf2') + conf2 = Config(interface1, session_id='conf2') conf2.read_write() meta = MetaConfig([conf1, conf2]) meta.read_write() @@ -452,9 +454,9 @@ def test_meta_force_default_if_same_and_dont_change(): netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',)) interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) interface1.impl_set_group_type(groups.master) - conf1 = Config(interface1, name='conf1') + conf1 = Config(interface1, session_id='conf1') conf1.read_write() - conf2 = Config(interface1, name='conf2') + conf2 = Config(interface1, session_id='conf2') conf2.read_write() meta = MetaConfig([conf1, conf2]) meta.read_write() @@ -494,9 +496,9 @@ def test_meta_force_default_and_dont_change(): netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',)) interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) interface1.impl_set_group_type(groups.master) - conf1 = Config(interface1, name='conf1') + conf1 = Config(interface1, session_id='conf1') conf1.read_write() - conf2 = Config(interface1, name='conf2') + conf2 = Config(interface1, session_id='conf2') conf2.read_write() meta = MetaConfig([conf1, conf2]) meta.read_write() @@ -510,9 +512,9 @@ def test_meta_properties_meta(): netmask_admin_eth0.impl_add_consistency('network_netmask', ip_admin_eth0) interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) interface1.impl_set_group_type(groups.master) - conf1 = Config(interface1, name='conf1') + conf1 = Config(interface1, session_id='conf1') conf1.read_write() - conf2 = Config(interface1, name='conf2') + conf2 = Config(interface1, session_id='conf2') conf2.read_write() meta = MetaConfig([conf1, conf2]) meta.read_write() @@ -525,9 +527,9 @@ def test_meta_exception_meta(): netmask_admin_eth0.impl_add_consistency('network_netmask', ip_admin_eth0) interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) interface1.impl_set_group_type(groups.master) - conf1 = Config(interface1, name='conf1') + conf1 = Config(interface1, session_id='conf1') conf1.read_write() - conf2 = Config(interface1, name='conf2') + conf2 = Config(interface1, session_id='conf2') conf2.read_write() meta = MetaConfig([conf1, conf2]) meta.read_write() @@ -541,7 +543,7 @@ def test_meta_callback(): val4 = StrOption('val4', "", callback=return_value, callback_params={'value': ((val1, False),)}) val5 = StrOption('val5', "", callback=return_value, callback_params={'value': ('yes',)}) maconfig = OptionDescription('rootconfig', '', [val1, val2, val3, val4, val5]) - cfg = Config(maconfig, name='cfg') + cfg = Config(maconfig, session_id='cfg') meta = MetaConfig([cfg]) meta.read_write() assert meta.cfg.make_dict() == {'val3': 'yes', 'val2': 'val', 'val1': 'val', 'val5': 'yes', 'val4': 'val'} @@ -567,7 +569,7 @@ def test_meta_callback_slave(): interface1 = OptionDescription('val1', '', [val1, val3, val4]) interface1.impl_set_group_type(groups.master) maconfig = OptionDescription('rootconfig', '', [val, interface1]) - cfg = Config(maconfig, name='cfg') + cfg = Config(maconfig, session_id='cfg') meta = MetaConfig([cfg]) meta.read_write() assert meta.cfg.make_dict() == {'val1.val2': ['val'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'} @@ -599,3 +601,42 @@ def test_meta_callback_slave(): assert meta.cfg.make_dict() == {'val1.val2': ['val2', 'rah'], 'val1.val1': ['val3', 'rah'], 'val1.val3': ['val3', 'rah'], 'val': 'val'} meta.val1.val1 = ['val4'] assert meta.cfg.make_dict() == {'val1.val2': ['val2', 'rah'], 'val1.val1': ['val3', 'rah'], 'val1.val3': ['val3', 'rah'], 'val': 'val'} + + +def test_meta_properties_meta_set_value(): + ip_admin_eth0 = NetworkOption('ip_admin_eth0', "ip", multi=True, default=['192.168.1.1']) + netmask_admin_eth0 = NetmaskOption('netmask_admin_eth0', "mask", multi=True, properties=('disabled',)) + interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) + conf1 = Config(interface1, session_id='conf1') + conf2 = Config(interface1, session_id='conf2') + meta = MetaConfig([conf1, conf2]) + meta.read_write() + assert conf1.make_dict() == {'ip_admin_eth0': ['192.168.1.1']} + ret = meta.set_value('netmask_admin_eth0', ['255.255.255.255'], only_config=True) + assert len(ret) == 2 + assert isinstance(ret[0], PropertiesOptionError) + assert isinstance(ret[1], PropertiesOptionError) + ret = meta.set_value('netmask_admin_eth0', ['255.255.255.255'], force_default=True) + assert len(ret) == 1 + assert isinstance(ret[0], PropertiesOptionError) + ret = meta.set_value('netmask_admin_eth0', ['255.255.255.255'], force_dont_change_value=True) + assert len(ret) == 3 + assert isinstance(ret[0], PropertiesOptionError) + assert isinstance(ret[1], PropertiesOptionError) + assert isinstance(ret[2], PropertiesOptionError) + ret = meta.set_value('netmask_admin_eth0', ['255.255.255.255'], force_default_if_same=True) + assert len(ret) == 1 + assert isinstance(ret[0], PropertiesOptionError) + ret = meta.set_value('ip_admin_eth0', '255.255.255.255', only_config=True) + assert len(ret) == 2 + assert isinstance(ret[0], ValueError) + assert isinstance(ret[1], ValueError) + ret = meta.set_value('ip_admin_eth0', '255.255.255.255', force_default=True) + assert len(ret) == 1 + assert isinstance(ret[0], ValueError) + ret = meta.set_value('ip_admin_eth0', '255.255.255.255', force_dont_change_value=True) + assert len(ret) == 1 + assert isinstance(ret[0], ValueError) + ret = meta.set_value('ip_admin_eth0', '255.255.255.255', force_default_if_same=True) + assert len(ret) == 1 + assert isinstance(ret[0], ValueError) diff --git a/test/test_state.py b/test/test_state.py index fbf0280..0495624 100644 --- a/test/test_state.py +++ b/test/test_state.py @@ -207,6 +207,9 @@ def _diff_conf(cfg1, cfg2): elif attr == '_impl_children': for index, _opt in enumerate(val1): _diff_conf(_opt, val2[index]) + elif attr == '_impl_name': + #FIXME + pass else: assert val1 == val2 @@ -256,9 +259,9 @@ def test_diff_information_config(): o = OptionDescription('o', '', [b]) o1 = OptionDescription('o1', '', [o]) try: - cfg = Config(o1, persistent=True, session_id='29090938') + cfg = Config(o1, persistent=True, session_id='c29090938') except ValueError: - cfg = Config(o1, session_id='29090938') + cfg = Config(o1, session_id='c29090938') cfg._impl_test = True cfg.impl_set_information('info', 'oh') @@ -269,7 +272,7 @@ def test_diff_information_config(): assert cfg.impl_get_information('info') == 'oh' assert q.impl_get_information('info') == 'oh' try: - delete_session('config', '29090938') + delete_session('config', 'c29090938') except ValueError: pass @@ -341,16 +344,16 @@ def test_state_config(): val1 = BoolOption('val1', "") maconfig = OptionDescription('rootconfig', '', [val1]) try: - cfg = Config(maconfig, persistent=True, session_id='29090931') + cfg = Config(maconfig, persistent=True, session_id='c29090931') except ValueError: - cfg = Config(maconfig, session_id='29090931') + cfg = Config(maconfig, session_id='c29090931') cfg._impl_test = True a = dumps(cfg) q = loads(a) _diff_opts(cfg.cfgimpl_get_description(), q.cfgimpl_get_description()) _diff_conf(cfg, q) try: - delete_session('config', '29090931') + delete_session('config', 'c29090931') except ValueError: pass @@ -370,16 +373,16 @@ def test_state_config2(): od = OptionDescription('od', '', [st]) od2 = OptionDescription('od', '', [od, od1]) try: - cfg = Config(od2, persistent=True, session_id='29090939') + cfg = Config(od2, persistent=True, session_id='c29090939') except ValueError: - cfg = Config(od2, session_id='29090939') + cfg = Config(od2, session_id='c29090939') cfg._impl_test = True a = dumps(cfg) q = loads(a) _diff_opts(cfg.cfgimpl_get_description(), q.cfgimpl_get_description()) _diff_conf(cfg, q) try: - delete_session('config', '29090939') + delete_session('config', 'c29090939') except ValueError: pass @@ -391,9 +394,9 @@ def test_diff_opt_value(): o = OptionDescription('o', '', [b, u, s]) o1 = OptionDescription('o1', '', [o]) try: - cfg = Config(o1, persistent=True, session_id='29090941') + cfg = Config(o1, persistent=True, session_id='c29090941') except ValueError: - cfg = Config(o1, session_id='29090941') + cfg = Config(o1, session_id='c29090941') cfg._impl_test = True a = dumps(cfg) @@ -401,7 +404,7 @@ def test_diff_opt_value(): _diff_opts(cfg.cfgimpl_get_description(), q.cfgimpl_get_description()) _diff_conf(cfg, q) try: - delete_session('config', '29090941') + delete_session('config', 'c29090941') except ValueError: pass @@ -416,9 +419,9 @@ def test_diff_opt_config(): o = OptionDescription('o', '', [b, u, s]) o1 = OptionDescription('o1', '', [o]) try: - cfg = Config(o1, persistent=True, session_id='29090940') + cfg = Config(o1, persistent=True, session_id='c29090940') except ValueError: - cfg = Config(o1, session_id='29090940') + cfg = Config(o1, session_id='c29090940') cfg._impl_test = True a = dumps(cfg) @@ -426,7 +429,7 @@ def test_diff_opt_config(): _diff_opts(cfg.cfgimpl_get_description(), q.cfgimpl_get_description()) _diff_conf(cfg, q) try: - delete_session('config', '29090940') + delete_session('config', 'c29090940') except ValueError: pass @@ -435,9 +438,9 @@ def test_state_properties(): val1 = BoolOption('val1', "") maconfig = OptionDescription('rootconfig', '', [val1]) try: - cfg = Config(maconfig, persistent=True, session_id='29090932') + cfg = Config(maconfig, persistent=True, session_id='c29090932') except ValueError: - cfg = Config(maconfig, session_id='29090932') + cfg = Config(maconfig, session_id='c29090932') cfg._impl_test = True cfg.read_write() cfg.cfgimpl_get_settings()[val1].append('test') @@ -445,7 +448,7 @@ def test_state_properties(): q = loads(a) _diff_conf(cfg, q) try: - delete_session('config', '29090932') + delete_session('config', 'c29090932') except ValueError: pass @@ -454,9 +457,9 @@ def test_state_values(): val1 = BoolOption('val1', "") maconfig = OptionDescription('rootconfig', '', [val1]) try: - cfg = Config(maconfig, persistent=True, session_id='29090933') + cfg = Config(maconfig, persistent=True, session_id='c29090933') except ValueError: - cfg = Config(maconfig, session_id='29090933') + cfg = Config(maconfig, session_id='c29090933') cfg._impl_test = True cfg.val1 = True a = dumps(cfg) @@ -466,7 +469,7 @@ def test_state_values(): assert cfg.val1 is True assert q.val1 is False try: - delete_session('config', '29090933') + delete_session('config', 'c29090933') except ValueError: pass @@ -475,9 +478,9 @@ def test_state_values_owner(): val1 = BoolOption('val1', "") maconfig = OptionDescription('rootconfig', '', [val1]) try: - cfg = Config(maconfig, persistent=True, session_id='29090934') + cfg = Config(maconfig, persistent=True, session_id='c29090934') except ValueError: - cfg = Config(maconfig, session_id='29090934') + cfg = Config(maconfig, session_id='c29090934') cfg._impl_test = True owners.addowner('newowner') cfg.cfgimpl_get_settings().setowner(owners.newowner) @@ -489,7 +492,7 @@ def test_state_values_owner(): nval1 = q.cfgimpl_get_description().val1 assert q.getowner(nval1) == owners.newowner try: - delete_session('config', '29090934') + delete_session('config', 'c29090934') except ValueError: pass @@ -499,17 +502,17 @@ def test_state_metaconfig(): od1 = OptionDescription('od1', '', [i1]) od2 = OptionDescription('od2', '', [od1]) try: - cfg = Config(od2, persistent=True, session_id='29090935') + cfg = Config(od2, persistent=True, session_id='c29090935') except ValueError: - conf1 = Config(od2, session_id='29090935') + conf1 = Config(od2, session_id='c29090935') conf1._impl_test = True - conf2 = Config(od2, session_id='29090936') + conf2 = Config(od2, session_id='c29090936') conf2._impl_test = True - meta = MetaConfig([conf1, conf2], session_id='29090937') + meta = MetaConfig([conf1, conf2], session_id='c29090937') meta._impl_test = True raises(ConfigError, "dumps(meta)") try: - delete_session('config', '29090935') + delete_session('config', 'c29090935') except ValueError: pass @@ -519,19 +522,19 @@ def test_state_groupconfig(): od1 = OptionDescription('od1', '', [i1]) od2 = OptionDescription('od2', '', [od1]) try: - cfg = Config(od2, persistent=True, session_id='29090938') + cfg = Config(od2, persistent=True, session_id='c29090938') except ValueError: - conf1 = Config(od2, session_id='29090938') + conf1 = Config(od2, session_id='c29090938') conf1._impl_test = True - conf2 = Config(od2, session_id='29090939') + conf2 = Config(od2, session_id='c29090939') conf2._impl_test = True - meta = GroupConfig([conf1, conf2], session_id='29090940') + meta = GroupConfig([conf1, conf2], session_id='c29090940') meta._impl_test = True a = dumps(meta) q = loads(a) _diff_conf(meta, q) try: - delete_session('config', '29090938') + delete_session('config', 'c29090938') except ValueError: pass diff --git a/test/test_submulti.py b/test/test_submulti.py index 6896ed2..321ca37 100644 --- a/test/test_submulti.py +++ b/test/test_submulti.py @@ -684,9 +684,9 @@ def test_submulti_unique(): def test_multi_submulti_meta(): multi = StrOption('multi', '', multi=submulti) od = OptionDescription('od', '', [multi]) - conf1 = Config(od, name='conf1') + conf1 = Config(od, session_id='conf1') conf1.read_write() - conf2 = Config(od, name='conf2') + conf2 = Config(od, session_id='conf2') conf2.read_write() meta = MetaConfig([conf1, conf2]) meta.read_write() diff --git a/tiramisu/config.py b/tiramisu/config.py index 4fe52ba..e76c221 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -317,11 +317,13 @@ class SubConfig(object): _commit=_commit) else: subpath = self._get_subpath(name) - self.cfgimpl_get_values().setitem(child, value, subpath, - force_permissive=force_permissive, - not_raises=not_raises, index=index, - _setting_properties=_setting_properties, - _commit=_commit) + ret = self.cfgimpl_get_values().setitem(child, value, subpath, + force_permissive=force_permissive, + not_raises=not_raises, index=index, + _setting_properties=_setting_properties, + _commit=_commit) + if ret is not None: + return ret def __delattr__(self, name): context = self._cfgimpl_get_context() @@ -781,8 +783,8 @@ class Config(_CommonConfig): __slots__ = ('__weakref__', '_impl_test', '_impl_name') def __init__(self, descr, session_id=None, persistent=False, - name=undefined, force_values=None, force_settings=None, - _duplicate=False, mandatory_name=False, _force_store_values=True): + force_values=None, force_settings=None, + _duplicate=False, _force_store_values=True): """ Configuration option management master class :param descr: describes the configuration schema @@ -802,15 +804,9 @@ class Config(_CommonConfig): self._impl_settings = force_settings self._impl_values = Values(self, force_values) else: - properties, permissives, values = get_storages(self, session_id, persistent) - if name is undefined: - name = 'config' - if session_id is not None: - name += session_id - if mandatory_name and name is None: - raise ValueError(_("name is mandatory for the config").format(name)) - if name is not None and not valid_name(name): # pragma: optional cover - raise ValueError(_("invalid name: {0} for config").format(name)) + properties, permissives, values, session_id = get_storages(self, session_id, persistent) + if not valid_name(session_id): # pragma: optional cover + raise ValueError(_("invalid session ID: {0} for config").format(session_id)) self._impl_settings = Settings(self, properties, permissives) self._impl_values = Values(self, values) super(Config, self).__init__(descr, weakref.ref(self)) @@ -819,7 +815,7 @@ class Config(_CommonConfig): self._impl_test = False if _duplicate is False and (force_settings is None or force_values is None): self._impl_build_all_caches(_force_store_values) - self._impl_name = name + self._impl_name = session_id def impl_getname(self): return self._impl_name @@ -832,7 +828,7 @@ class GroupConfig(_CommonConfig): __slots__ = ('__weakref__', '_impl_children', '_impl_name') def __init__(self, children, session_id=None, persistent=False, - _descr=None, name=undefined): + _descr=None): if not isinstance(children, list): raise ValueError(_("groupconfig's children must be a list")) names = [] @@ -850,16 +846,14 @@ class GroupConfig(_CommonConfig): raise ConflictError(_('config name must be uniq in ' 'groupconfig for {0}').format(name)) self._impl_children = children - properties, permissives, values = get_storages(self, session_id, persistent) + properties, permissives, values, session_id = get_storages(self, session_id, persistent) self._impl_settings = Settings(self, properties, permissives) self._impl_values = Values(self, values) super(GroupConfig, self).__init__(_descr, weakref.ref(self)) self._impl_meta = None #undocumented option used only in test script self._impl_test = False - if name is undefined: - name = session_id - self._impl_name = name + self._impl_name = session_id def cfgimpl_get_children(self): return self._impl_children @@ -877,15 +871,19 @@ class GroupConfig(_CommonConfig): def set_value(self, path, value, _commit=True): """Setattr not in current GroupConfig, but in each children """ + ret = [] for child in self._impl_children: if isinstance(child, MetaConfig): - child.set_value(path, value, only_config=True, _commit=False) + ret.extend(child.set_value(path, value, only_config=True, _commit=False)) elif isinstance(child, GroupConfig): - child.set_value(path, value, _commit=False) + ret.extend(child.set_value(path, value, _commit=False)) else: - child.setattr(path, value, not_raises=True, _commit=False) + childret = child.setattr(path, value, not_raises=True, _commit=False) + if childret is not None: + ret.append(childret) if _commit: self.cfgimpl_get_values()._p_.commit() + return ret def find_firsts(self, byname=None, bypath=undefined, byoption=undefined, @@ -953,7 +951,7 @@ class MetaConfig(GroupConfig): __slots__ = tuple() def __init__(self, children, session_id=None, persistent=False, - name=undefined, optiondescription=None): + optiondescription=None): descr = None if optiondescription is not None: new_children = [] @@ -977,7 +975,7 @@ class MetaConfig(GroupConfig): child._impl_meta = weakref.ref(self) super(MetaConfig, self).__init__(children, session_id, persistent, - descr, name) + descr) def set_value(self, path, value, force_default=False, force_dont_change_value=False, force_default_if_same=False, @@ -991,6 +989,7 @@ class MetaConfig(GroupConfig): 'force_dont_change_value cannot be set with' ' only_config')) return super(MetaConfig, self).set_value(path, value, _commit=_commit) + ret = [] if force_default or force_default_if_same or force_dont_change_value: if force_default and force_dont_change_value: raise ValueError(_('force_default and force_dont_change_value' @@ -1005,18 +1004,40 @@ class MetaConfig(GroupConfig): else: child_value = child.getattr(path) if force_default or value == child_value: - child.cfgimpl_get_values().reset(opt, path=path, - validate=False, - _setting_properties=setting_properties, - _commit=False) + childret = child.cfgimpl_get_values().reset(opt, path=path, + validate=False, + _setting_properties=setting_properties, + _commit=False) + if childret is not None: + ret.append(childret) continue if force_dont_change_value: - child_value = child.getattr(path, _setting_properties=setting_properties) - if value != child_value: - child.setattr(path, child_value, _commit=False) + child_value = child.getattr(path, _setting_properties=setting_properties, + returns_raise=True) + if isinstance(child_value, Exception): + ret.append(child_value) + elif value != child_value: + childret = child.setattr(path, child_value, _commit=False, not_raises=True) + if childret is not None: + ret.append(childret) - self.setattr(path, value, _commit=_commit) + setret = self.setattr(path, value, _commit=_commit, not_raises=True) + if setret is not None: + ret.append(setret) + return ret - def new_config(self, session_id=None, persistent=False, name=undefined): - return Config(self._impl_descr, _duplicate=True, session_id=session_id, name=name, - persistent=persistent, mandatory_name=True) + def reset(self, path): + opt = self.cfgimpl_get_description().impl_get_opt_by_path(path) + setting_properties = self.cfgimpl_get_settings()._getproperties(read_write=False) + for child in self._impl_children: + child.cfgimpl_get_values().reset(opt, path=path, + validate=False, + _setting_properties=setting_properties, + _commit=False) + self.cfgimpl_get_values().reset(opt, path=path, + validate=False, + _setting_properties=setting_properties) + + def new_config(self, session_id=None, persistent=False): + return Config(self._impl_descr, session_id=session_id, + persistent=persistent) diff --git a/tiramisu/error.py b/tiramisu/error.py index 011fd35..a71dc29 100644 --- a/tiramisu/error.py +++ b/tiramisu/error.py @@ -34,6 +34,8 @@ def display_list(lst, separator='and'): ret = str(ret) return ret else: + if isinstance(lst, tuple): + lst = list(lst) lst.sort() lst_ = [] for l in lst[:-1]: diff --git a/tiramisu/option/baseoption.py b/tiramisu/option/baseoption.py index 6d77cf7..ff4b4d6 100644 --- a/tiramisu/option/baseoption.py +++ b/tiramisu/option/baseoption.py @@ -42,7 +42,7 @@ if sys.version_info[0] >= 3: # pragma: no cover StorageBase = get_storages_option('base') submulti = 2 -name_regexp = re.compile(r'^[a-z][a-zA-Z\d\-_]*$') +name_regexp = re.compile(r'^[a-z][a-zA-Z\d_]*$') forbidden_names = frozenset(['iter_all', 'iter_group', 'find', 'find_first', 'make_dict', 'unwrap_from_path', 'read_only', 'read_write', 'getowner', 'set_contexts']) diff --git a/tiramisu/storage/__init__.py b/tiramisu/storage/__init__.py index cbabe08..e5dc61c 100644 --- a/tiramisu/storage/__init__.py +++ b/tiramisu/storage/__init__.py @@ -127,7 +127,7 @@ def get_storage(type_, session_id, persistent, test): # pragma: optional cover def get_storages(context, session_id, persistent): def gen_id(config): - return str(id(config)) + str(time()) + str(randint(0, 500)) + return 'c' + str(id(config)) + str(int(time())) + str(randint(0, 500)) if session_id is None: session_id = gen_id(context) @@ -136,7 +136,7 @@ def get_storages(context, session_id, persistent): properties = imp.Properties(storage) permissives = imp.Permissives(storage) values = imp.Values(storage) - return properties, permissives, values + return properties, permissives, values, session_id def get_storages_option(type_): diff --git a/tiramisu/storage/sqlite3/storage.py b/tiramisu/storage/sqlite3/storage.py index f6403d4..6808fd2 100644 --- a/tiramisu/storage/sqlite3/storage.py +++ b/tiramisu/storage/sqlite3/storage.py @@ -93,7 +93,6 @@ class Storage(object): self.execute(permissives_table) def commit(self): - #print('ca commit') self._conn.commit() def execute(self, sql, params=None, commit=True): diff --git a/tiramisu/value.py b/tiramisu/value.py index 95ccc4b..1ea7fb1 100644 --- a/tiramisu/value.py +++ b/tiramisu/value.py @@ -429,10 +429,12 @@ class Values(object): session=session, not_raises=not_raises, index=index) if props and not_raises: - return + return props err = opt.impl_validate(value, fake_context, display_warnings=False, force_index=index, setting_properties=_setting_properties) if err: + if not_raises: + return err raise err opt.impl_validate(value, fake_context, display_error=False, setting_properties=_setting_properties)