Skip to content

Commit 2fee44d

Browse files
committed
Add support for updating dictionary or tuple keys in self.cfg
1 parent c89945d commit 2fee44d

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

easybuild/framework/easyconfig/easyconfig.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -575,14 +575,15 @@ def copy(self, validate=None):
575575
def update(self, key, value, allow_duplicate=True):
576576
"""
577577
Update a string configuration value with a value (i.e. append to it).
578+
NOTE: For dictionaries, 'allow_duplicate' will be ignored.
578579
"""
579580
if isinstance(value, string_type):
580581
lval = [value]
581-
elif isinstance(value, list):
582+
elif isinstance(value, (list, dict, tuple)):
582583
lval = value
583584
else:
584-
msg = "Can't update configuration value for %s, because the "
585-
msg += "attempted update value, '%s', is not a string or list."
585+
msg = "Can't update configuration value for %s, because the attempted"
586+
msg += " update value, '%s', is not a string, list or dictionary."
586587
raise EasyBuildError(msg, key, value)
587588

588589
param_value = self[key]
@@ -595,8 +596,17 @@ def update(self, key, value, allow_duplicate=True):
595596
for item in lval:
596597
if allow_duplicate or item not in param_value:
597598
param_value = param_value + [item]
599+
elif isinstance(param_value, tuple):
600+
param_value = list(param_value)
601+
for item in lval:
602+
if allow_duplicate or item not in param_value:
603+
param_value.append(item)
604+
param_value = tuple(param_value)
605+
elif isinstance(param_value, dict):
606+
param_value.update(lval)
598607
else:
599-
raise EasyBuildError("Can't update configuration value for %s, because it's not a string or list.", key)
608+
msg = "Can't update configuration value for %s, because it's not a string, list or dictionary."
609+
raise EasyBuildError(msg, key)
600610

601611
self[key] = param_value
602612

test/framework/easyconfig.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,10 @@ def test_update(self):
17441744
ec.update('patches', ['foo2.patch', 'bar2.patch'], allow_duplicate=False)
17451745
self.assertEqual(ec['patches'], patches_tmp)
17461746

1747+
# for dictionary values: extend
1748+
ec.update('sanity_check_paths', {'key1': 'value1'})
1749+
self.assertTrue(ec['sanity_check_paths'].endswith("'key1': 'value1'}"))
1750+
17471751
def test_hide_hidden_deps(self):
17481752
"""Test use of --hide-deps on hiddendependencies."""
17491753
test_dir = os.path.dirname(os.path.abspath(__file__))

0 commit comments

Comments
 (0)