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

Changed new script generation

* Added evaluation of expressions (eval(...))
* Re-wrote splitting of date strings (now uses split_date(...))
parent beaa28ec
No related branches found
No related tags found
No related merge requests found
......@@ -16,8 +16,9 @@ import feedback
class ExpConfigError(InterpolationError):
def __init__(self, message, key):
message = message.rstrip('.!')
InterpolationError.__init__(self,
"{0} while reading key '{1}'".format(message[:-1], key))
"{0} while reading key '{1}'".format(message, key))
class ExpConfig(ConfigObj):
'''Read and store configuration info from input and experiments' library
......@@ -137,12 +138,32 @@ class ExpConfig(ConfigObj):
)
))
)
def split_date(value):
'''Re-format datetime string to list for use in namelists'''
match = re.match(r'^0*(\d+)-0*(\d+)-0*(\d+)'
r'([T ]0*(\d+)(:0*(\d+)(:0*(\d+))?)?)?$', value)
if match:
numbers = match.groups('0')
else:
raise ValueError("invalid date/time '{0}'".format(value))
return [numbers[i] for i in [0,1,2,4,6,8]]
# Interpolate and evaluate keys if they are an expression
def eval_key(section, key):
try:
section[key] = section[key]
except InterpolationError as error:
value = section[key]
if isinstance(value, basestring):
match = re.match(r'^eval\((.*)\)$', value)
if match:
value = str(eval(match.group(1)))
else:
match = re.match(r'^split_date\((.*)\)$', value)
if match:
value = split_date(match.group(1))
except (InterpolationError, ValueError) as error:
raise ExpConfigError(error.message, key)
section[key] = value
self.walk(eval_key)
self.experiment_id = self['EXP_ID']
......
......@@ -73,15 +73,6 @@ def expand_template(template_dict, template_name, expanded_name):
# Namelist formatting
def format_datetime(result):
'''Re-format datetime string to list for use in namelists'''
match = re.match('^(["'r"'])0*(\d+)-0*(\d+)-0*(\d+)"
r"([T ]0*(\d+)(:0*(\d+)(:0*(\d+))?)?)?\1$", result)
if match:
numbers = match.groups('0')
result = ', '.join([numbers[i] for i in [1,2,3,5,7,9]])
return result
def format_atom(value):
'''Format atomic value for use in namelists'''
result = repr(value)
......@@ -93,7 +84,6 @@ def format_atom(value):
#
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 = format_datetime(result)
return result
def format_value(value):
......
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