diff --git a/expconfig.py b/expconfig.py index 46ab306ab968a94141026d8082f6fc5d3bef358b..5c2168f878432a2e7c8a755da06e0d223d7c66f6 100644 --- a/expconfig.py +++ b/expconfig.py @@ -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'] diff --git a/mkexp b/mkexp index b8d8006cb581ce14b68c4233a8c38ad47bcf840f..c134ab54acc1777d6aeda1b8b74a4245770d9781 100755 --- a/mkexp +++ b/mkexp @@ -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):