Skip to content

Commit 60165a4

Browse files
committed
In easyconfig.update(), change local copy of 'value' argument to name 'inval' for clarity; exclude case of string input being applied to dictionary parameter. Add comments to make use of param_value explicit in context.
1 parent 4fd2205 commit 60165a4

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

easybuild/framework/easyconfig/easyconfig.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -578,36 +578,41 @@ def update(self, key, value, allow_duplicate=True):
578578
NOTE: For dictionaries, 'allow_duplicate' will be ignored.
579579
"""
580580
if isinstance(value, string_type):
581-
lval = [value]
581+
inval = [value]
582582
elif isinstance(value, (list, dict, tuple)):
583-
lval = value
583+
inval = value
584584
else:
585585
msg = "Can't update configuration value for %s, because the attempted"
586586
msg += " update value, '%s', is not a string, list or dictionary."
587587
raise EasyBuildError(msg, key, value)
588588

589+
# For dictionaries, input value cannot be a string; must be iterable
590+
if isinstance(self[key], dict) and not isinstance(value, string_type):
591+
msg = "Can't update configuration value for %s, because the attempted"
592+
msg += "update value, '%s', is not iterable (list, tuple, dict)."
593+
raise EasyBuildError(msg, key, value)
594+
595+
# Make copy of current configuration value so we can modify it
589596
param_value = self[key]
590597
if isinstance(param_value, string_type):
591-
for item in lval:
598+
for item in inval:
592599
# re.search: only add value to string if it's not there yet (surrounded by whitespace)
593600
if allow_duplicate or (not re.search(r'(^|\s+)%s(\s+|$)' % re.escape(item), param_value)):
594601
param_value = param_value + ' %s ' % item
595-
elif isinstance(param_value, list):
596-
for item in lval:
597-
if allow_duplicate or item not in param_value:
598-
param_value = param_value + [item]
599-
elif isinstance(param_value, tuple):
602+
elif isinstance(param_value, (list, tuple)):
600603
param_value = list(param_value)
601-
for item in lval:
604+
for item in inval:
602605
if allow_duplicate or item not in param_value:
603606
param_value.append(item)
604-
param_value = tuple(param_value)
607+
if isinstance(self[key], tuple): # Cast back to original type
608+
param_value = tuple(param_value)
605609
elif isinstance(param_value, dict):
606-
param_value.update(lval)
610+
param_value.update(inval)
607611
else:
608612
msg = "Can't update configuration value for %s, because it's not a string, list or dictionary."
609613
raise EasyBuildError(msg, key)
610614

615+
# Replace modified value back into configuration, preserving type
611616
self[key] = param_value
612617

613618
def set_keys(self, params):

0 commit comments

Comments
 (0)