From 5618041de2af9884687ae2d90c74056c0043eb20 Mon Sep 17 00:00:00 2001
From: Karl-Hermann Wieners <karl-hermann.wieners@mpimet.mpg.de>
Date: Tue, 15 Dec 2020 12:28:35 +0100
Subject: [PATCH] Config: changed to allow values with native variables to be
 interpolated correctly

---
 CHANGES.txt  |  5 +++++
 expconfig.py | 19 ++++++++++++++++++-
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/CHANGES.txt b/CHANGES.txt
index 90d25f6..456b8db 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -14,6 +14,11 @@ Global
 
 * Changed directory creation to only create parts w/o native variables
 
+Configuration
+-------------
+
+* Changed to allow values with native variables to be interpolated correctly
+
 Release 1.0.9
 =============
 
diff --git a/expconfig.py b/expconfig.py
index ed06b15..ddd7ccb 100644
--- a/expconfig.py
+++ b/expconfig.py
@@ -261,7 +261,23 @@ class ExpConfig(ConfigObj):
                 if isinstance(value, (list, tuple)):
                     value = map(eval_expression, value)
                 elif isinstance(value, basestring):
-                    value = eval_expression(value)                    
+                    value = eval_expression(value)
+                if isinstance(value, (list, tuple)):
+                    value = [v.replace('$', '$$') for v in value]
+                elif isinstance(value, basestring):
+                    value = value.replace('$', '$$')
+            except (InterpolationError, ValueError) as error:
+                raise ExpConfigError(error.message, key)
+            section[key] = value
+
+        # Undo remaining changes from walk with eval_key
+        def uneval_key(section, key):
+            try:
+                value = section[key]
+                if isinstance(value, (list, tuple)):
+                    value = [v.replace('$$', '$') for v in value]
+                elif isinstance(value, basestring):
+                    value = value.replace('$$', '$')
             except (InterpolationError, ValueError) as error:
                 raise ExpConfigError(error.message, key)
             section[key] = value
@@ -454,6 +470,7 @@ class ExpConfig(ConfigObj):
 
         config_lines.seek(0)
         ConfigObj.__init__(self, config_lines, interpolation=False)
+        self.walk(uneval_key)
         
         self.experiment_id = self[ExpConfig.id_name]
         self.experiment_kind = re.sub(r'-\w+$', '', experiment_type)
-- 
GitLab