From 0184f49f9c634acff160805a3442a09954913158 Mon Sep 17 00:00:00 2001
From: Karl-Hermann Wieners <karl-hermann.wieners@mpimet.mpg.de>
Date: Wed, 27 Mar 2013 23:09:22 +0000
Subject: [PATCH] Changed new script generation

* Added evaluation of expressions (eval(...))
* Re-wrote splitting of date strings (now uses split_date(...))
---
 expconfig.py | 29 +++++++++++++++++++++++++----
 mkexp        | 10 ----------
 2 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/expconfig.py b/expconfig.py
index 46ab306..5c2168f 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 b8d8006..c134ab5 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):
-- 
GitLab