Skip to content
Snippets Groups Projects
Commit c86dd350 authored by Karl-Hermann Wieners's avatar Karl-Hermann Wieners
Browse files

Config: added incremental adding and removing (+=, -=)

Currently for lists only
parent 81190e8c
Branches basic_dataset
No related tags found
No related merge requests found
......@@ -12,7 +12,12 @@ Release 1.2.0
Global
------
* Fixed "missing config" test for python3
* Fixed "missing config" test for python3
Config
------
* Added incremental adding and removing (+=, -=), currently for lists only
Release 1.1.6
=============
......
This diff is collapsed.
No preview for this file type
......@@ -162,6 +162,59 @@ class ExpConfig(ConfigObj):
break
return config_name
# Incremental list assignments (+=, -=)
def add_to_list(section, key, base_key):
if isinstance(section[key], (list, tuple)):
section[base_key].extend(section[key])
else:
section[base_key].append(section[key])
def remove_from_list(section, key, base_key):
values = section[key]
if not isinstance(values, (list, tuple)):
values = [values]
for value in values:
try:
section[base_key].remove(value)
except ValueError:
pass
list_assign_re = re.compile(r'(.*?)\s*([-+])$')
def list_assign(section):
del_keys = []
for key in section.scalars:
is_list_assign = re.match(list_assign_re, key)
if is_list_assign:
base_key = is_list_assign.group(1)
list_op = is_list_assign.group(2)
if not base_key in section:
section[base_key] = []
if not isinstance(section[base_key], (list, tuple)):
section[base_key] = [section[base_key]]
section.main.interpolation = 'template'
base_value = section[base_key]
section.main.interpolation = False
section[base_key] = base_value
if list_op == '+':
add_to_list(section, key, base_key)
elif list_op == '-':
remove_from_list(section, key, base_key)
del_keys.append(key)
for key in del_keys:
del section[key]
for key in section.sections:
list_assign(section[key])
# Helper functions for value definitions
def read_value(value):
if os.path.exists(value):
stream = open(value)
......@@ -414,6 +467,7 @@ class ExpConfig(ConfigObj):
if os.path.exists(setup_config_name):
pre_config.merge(ConfigObj(setup_config_name, interpolation=False))
split_jobs(pre_config)
list_assign(pre_config)
register_version(pre_config, config_versions)
lib_config_name = get_config_name(ExpConfig.exp_lib_dir,
......@@ -421,6 +475,7 @@ class ExpConfig(ConfigObj):
if os.path.exists(lib_config_name):
pre_config.merge(ConfigObj(lib_config_name, interpolation=False))
split_jobs(pre_config)
list_assign(pre_config)
register_version(pre_config, config_versions)
else:
feedback.warning("cannot find experiment config for '%s', "+
......@@ -432,6 +487,7 @@ class ExpConfig(ConfigObj):
if os.path.exists(lib_config_name):
pre_config.merge(ConfigObj(lib_config_name, interpolation=False))
split_jobs(pre_config)
list_assign(pre_config)
register_version(pre_config, config_versions)
else:
feedback.warning("cannot find config for option '%s', using "+
......@@ -444,6 +500,7 @@ class ExpConfig(ConfigObj):
if os.path.exists(lib_config_name):
pre_config.merge(ConfigObj(lib_config_name, interpolation=False))
list_assign(pre_config)
register_version(pre_config, config_versions)
# Warn user if at least one config had no version info
......@@ -456,6 +513,7 @@ class ExpConfig(ConfigObj):
interpolation=False)
pre_config.merge(experiment_config)
split_jobs(pre_config)
list_assign(pre_config)
# Add extra dictionary
pre_config.merge(extra_dict)
......
......@@ -433,6 +433,59 @@ class ContentTestCase(MkexpSimpleTestCase):
result = align(result)
self.assertMultiLineEqual(expected, result)
class StaticAssignmentTestCase(MkexpSimpleTestCase):
def test_add_to_list(self):
self.run_test(u"""
%{LIST1}
%{LIST2}
%{LIST3}
""", u"""
['a']
['b', 'c']
['d', 'e', 'f', 'g']
""", u"""
LIST1 += a
LIST2 = b
LIST2 += c
LIST3 = d, e
LIST3 + = f, g
""")
def test_remove_from_list(self):
self.run_test(u"""
%{LIST1}
%{LIST2}
%{LIST3}
""", u"""
[]
['b', 'c']
['d', 'f']
""", u"""
LIST1 = a
LIST1 -= a
LIST2 = b, c
LIST2 -= z
LIST3 = d, e, f, g
LIST3 - = e, g
""")
def test_order_add_remove_list(self):
self.run_test(u"""
%{LIST1}
%{LIST2}
""", u"""
['a', 'b']
['a', 'b', 'c']
""", u"""
LIST1 = a, b
LIST1 += c
LIST1 -= c
LIST2 = a, b
LIST2 -= c
LIST2 += c
""")
class NamelistTestCase(MkexpSimpleTestCase):
def test_namelist_comments(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment