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

Added configurable variable format to namelist values. Variables $${x} are now...

Added configurable variable format to namelist values. Variables $${x} are now recognized as script native variables. Defining .var_format allows different script syntax, e.g. XML style (.var_format = <%s>)
parent 62646284
No related branches found
No related tags found
No related merge requests found
......@@ -89,9 +89,11 @@ def move_file_to_backup(file_name, backup_name):
# Namelist formatting
def format_atom(value):
def format_atom(value, var_format):
'''Format atomic value for use in namelists'''
result = repr(value)
# Format replacement variables
result = re.sub(r'\$\{(\w+)\}', lambda x: var_format%x.group(1), result)
# Strip quotes from all-numeric strings
result = re.sub('^(["'r"'])([-+]?(\d+\.\d*|\.?\d+)([de][-+]?\d+)?)\1$", r'\2',
result, flags=re.I)
......@@ -99,10 +101,11 @@ def format_atom(value):
result = re.sub(r'^0+([1-9]\d*|0)$', r'\1', result)
#
result = re.sub('^(["'r"'])\.?t(rue)?\.?\1$", '.true.', result, flags=re.I)
result = re.sub('^(["'r"'])\.?f(alse)?\.?\1$", '.false.', result, flags=re.I)
result = \
re.sub('^(["'r"'])\.?f(alse)?\.?\1$", '.false.', result, flags=re.I)
return result
def format_value(value, indent):
def format_value(value, indent, var_format):
'''Format list, tuple, or atomic value for use in namelists'''
if isinstance(value, (list, tuple)):
width = 79 - indent
......@@ -110,7 +113,7 @@ def format_value(value, indent):
line = ''
lines = []
for element in value:
formatted = format_atom(element)
formatted = format_atom(element, var_format)
if len(line) + len(sep) + len(formatted) > width:
lines.append(line)
line = formatted
......@@ -120,7 +123,7 @@ def format_value(value, indent):
if line:
lines.append(line)
return (',\n' + ' '*indent).join(lines)
return format_atom(value)
return format_atom(value, var_format)
def keyword_warning(key):
feedback.warning("keyword '"+key+"' is deprecated, use '."+key+"' instead")
......@@ -138,7 +141,7 @@ def cut_remove_list(section, key):
del section[key]
return remove_list
def format_namelist(section):
def format_namelist(section, var_format):
'''Format config section as a namelist'''
# Settings
base_indent = 4
......@@ -171,7 +174,8 @@ def format_namelist(section):
line = ''
line = re.sub(r'^#', ' !', line)
lines.write(' '*base_indent+key+' = '+
format_value(value, indent)+line+'\n')
format_value(value, indent, var_format)+
line+'\n')
for line in section.comments.get(group, []):
if line:
match = re.match(r'#\s*(\w+)\s*=\s*(.*?)\s*$', line)
......@@ -180,7 +184,8 @@ def format_namelist(section):
value = match.group(2)
indent = base_indent + 2 + len(key) + 3
lines.write(' '*base_indent+'! '+key+' = '+
format_value(value, indent)+'\n')
format_value(value, indent, var_format)+
'\n')
lines.write('/\n')
return lines.getvalue()
......@@ -360,6 +365,7 @@ for subjob, subconfig in config['jobs'].iteritems():
del pre_config
# Prepare namelists for inclusion in scripts
var_format = job_config['JOB'].get('.var_format', '${%s}')
for namelist, groups in job_config['namelists'].iteritems():
if isinstance(groups, dict):
namelist_name = re.sub(r'\W', '_', namelist.upper())
......@@ -372,7 +378,8 @@ for subjob, subconfig in config['jobs'].iteritems():
job_config[namelist_name] = \
expand_template(groups, get_template_name(namelist))
else:
job_config[namelist_name] = format_namelist(groups)
job_config[namelist_name] = \
format_namelist(groups, var_format)
# Generate job script
expand_template_file(job_config,
......
......@@ -58,7 +58,6 @@ class MkexpTestCase(unittest.TestCase):
class RunningTestCase(MkexpTestCase):
def test_missing_config_file(self):
......@@ -122,6 +121,26 @@ class ContentTestCase(MkexpTestCase):
self.assertIn("key1 = local", result)
self.assertIn("key2 = global", result)
def test_var_statement(self):
exp_id = "test_var_statement"
job_id = "job"
writeconfig(exp_id, """
EXP_TYPE =
[namelists]
[[namelist]]
[[[group]]]
key = abc$${var}def
[jobs]
.var_format = <<<%s>>>
[["""+job_id+"""]]
""")
writetemplate(exp_id, job_id, """
%{NAMELIST}
""")
ignore = output(script("mkexp "+exp_id+".config"))
result = readfile(join("test", "experiments", exp_id, exp_id+"."+job_id))
self.assertIn("key = 'abc<<<var>>>def'", result)
if __name__ == '__main__':
......
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