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

Changed script generation to allow easier namelist setup (helper variables in...

Changed script generation to allow easier namelist setup (helper variables in namelists, evaluation of lists as strings; see EaSyMS13 IssueID #3621)
parent 666c5e91
No related branches found
No related tags found
No related merge requests found
......@@ -190,21 +190,34 @@ class ExpConfig(ConfigObj):
result = str(result)
return result
def eval_value_string(value):
'''
Evaluate key as python expression,
return as string or sequence of strings.
'''
result = eval_value(value)
if isinstance(result, (list, tuple)):
result = ", ".join(result)
return result
def eval_expression(value):
'''
Check if value is a supported expression.
If so, evaluate and return result, otherwise just pass through.
'''
match = re.match(r'^eval\((.*)\)$', value)
match = re.match(r'^eval\((.*)\)$', value, re.S)
if match:
result = eval_value(match.group(1))
else:
match = re.match(r'^split_date\((.*)\)$', value)
if match:
result = split_date(match.group(1))
else:
result = value
return result
return eval_value(match.group(1))
match = re.match(r'^evals\((.*)\)$', value, re.S)
if match:
return eval_value_string(match.group(1))
match = re.match(r'^split_date\((.*)\)$', value, re.S)
if match:
return split_date(match.group(1))
return value
# Interpolate and evaluate keys if they are an expression
def eval_key(section, key):
......
......@@ -123,15 +123,17 @@ def format_namelist(section):
# Format namelist groups that were not removed
lines = StringIO.StringIO()
for group, contents in section.iteritems():
group = group.lower()
if not any(map(lambda x: re.match(x, group), black_list)):
group_name = re.sub(r' .*$', '', group)
lines.write('&'+group_name+'\n')
for key, value in contents.iteritems():
key = key.lower()
indent = 4 + len(key) + 3
lines.write(' '+key+' = '+format_value(value, indent)+'\n')
lines.write('/\n')
if isinstance(contents, dict):
group = group.lower()
if not any(map(lambda x: re.match(x, group), black_list)):
group_name = re.sub(r' .*$', '', group)
lines.write('&'+group_name+'\n')
for key, value in contents.iteritems():
key = key.lower()
indent = 4 + len(key) + 3
lines.write(' '+key+' = '+
format_value(value, indent)+'\n')
lines.write('/\n')
return lines.getvalue()
#
......
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