From 2f3b6ad7cf5fd5968328062f9273e248b45d2e5c Mon Sep 17 00:00:00 2001
From: Karl-Hermann Wieners <karl-hermann.wieners@mpimet.mpg.de>
Date: Tue, 4 Apr 2023 16:25:34 +0200
Subject: [PATCH] Tools: disable interpolation in config by default

---
 compconfig    |  2 +-
 diffconfig    |  4 ++--
 expconfig.py  | 23 +++++++++++------------
 getconfig     |  4 ++--
 mkexp         |  9 ++++-----
 selconfig     |  4 ++--
 setconfig     |  5 ++---
 test.py       | 10 ++++++++++
 unmergeconfig |  5 ++---
 9 files changed, 36 insertions(+), 30 deletions(-)

diff --git a/compconfig b/compconfig
index e83db52..e5ef1bd 100755
--- a/compconfig
+++ b/compconfig
@@ -62,7 +62,7 @@ try:
     config_data = list()
     for i, config_file in enumerate(config_files):
         config_data.append(ConfigObj(config_file, 
-                interpolation=False, file_error=True, write_empty_values=True))
+                file_error=True, write_empty_values=True))
 except IOError as error:
     die(error.message)
 
diff --git a/diffconfig b/diffconfig
index 5dab60c..a0be1cb 100755
--- a/diffconfig
+++ b/diffconfig
@@ -55,9 +55,9 @@ try:
     config_file1 = args.config1
     config_file2 = args.config2
     config_data1 = ConfigObj(config_file1, 
-                interpolation=False, file_error=True,write_empty_values=True)
+                file_error=True,write_empty_values=True)
     config_data2 = ConfigObj(config_file2, 
-                interpolation=False, file_error=True)
+                file_error=True)
 except IOError as error:
     die(error.message)
 
diff --git a/expconfig.py b/expconfig.py
index 6c95dbb..9c08c84 100644
--- a/expconfig.py
+++ b/expconfig.py
@@ -86,6 +86,7 @@ class ConfigObj(configobj.ConfigObj):
         default_args = {
             'encoding': _preferred_encoding,
             'default_encoding': _preferred_encoding,
+            'interpolation': False,
         }
         for kw in default_args:
             if not kw in kwargs:
@@ -425,8 +426,8 @@ class ExpConfig(ConfigObj):
         pre_config = None
         setup_config_name = get_config_name('', ExpConfig.setup_config_name)
         if os.path.exists(setup_config_name):
-            pre_config = ConfigObj(setup_config_name, interpolation=False)
-        user_config = ConfigObj(experiment_config_name, interpolation=False)
+            pre_config = ConfigObj(setup_config_name)
+        user_config = ConfigObj(experiment_config_name)
         if pre_config:
             pre_config.merge(user_config)
         else:
@@ -469,7 +470,7 @@ class ExpConfig(ConfigObj):
 
         # Start from empty configuration
 
-        pre_config = ConfigObj(interpolation=False)
+        pre_config = ConfigObj()
         config_versions = []
 
         # Get default experiment id from file name
@@ -492,12 +493,12 @@ class ExpConfig(ConfigObj):
 
         lib_config_name = get_config_name(ExpConfig.exp_lib_dir,
                                           ExpConfig.default_name+'.config')
-        pre_config.merge(ConfigObj(lib_config_name, interpolation=False))
+        pre_config.merge(ConfigObj(lib_config_name))
         split_shared_sections(pre_config)
         register_version(pre_config, config_versions)
 
         if os.path.exists(setup_config_name):
-            pre_config.merge(ConfigObj(setup_config_name, interpolation=False))
+            pre_config.merge(ConfigObj(setup_config_name))
             split_shared_sections(pre_config)
             list_assign(pre_config)
             register_version(pre_config, config_versions)
@@ -505,7 +506,7 @@ class ExpConfig(ConfigObj):
         lib_config_name = get_config_name(ExpConfig.exp_lib_dir, 
                                           experiment_type+'.config')
         if os.path.exists(lib_config_name):
-            pre_config.merge(ConfigObj(lib_config_name, interpolation=False))
+            pre_config.merge(ConfigObj(lib_config_name))
             split_shared_sections(pre_config)
             list_assign(pre_config)
             register_version(pre_config, config_versions)
@@ -517,7 +518,7 @@ class ExpConfig(ConfigObj):
             lib_config_name = get_config_name(ExpConfig.opt_lib_dir, 
                                               option+'.config')
             if os.path.exists(lib_config_name):
-                pre_config.merge(ConfigObj(lib_config_name, interpolation=False))
+                pre_config.merge(ConfigObj(lib_config_name))
                 split_shared_sections(pre_config)
                 list_assign(pre_config)
                 register_version(pre_config, config_versions)
@@ -531,7 +532,7 @@ class ExpConfig(ConfigObj):
                                           environment+'.config')
 
         if os.path.exists(lib_config_name):
-            pre_config.merge(ConfigObj(lib_config_name, interpolation=False))
+            pre_config.merge(ConfigObj(lib_config_name))
             list_assign(pre_config)
             register_version(pre_config, config_versions)
 
@@ -541,8 +542,7 @@ class ExpConfig(ConfigObj):
 
         # Re-read config to allow overriding default settings
         # TODO: probably nicer if default experiment is given as argument
-        experiment_config = ConfigObj(experiment_config_name,
-                                      interpolation=False)
+        experiment_config = ConfigObj(experiment_config_name)
         pre_config.merge(experiment_config)
         split_shared_sections(pre_config)
         list_assign(pre_config)
@@ -603,7 +603,6 @@ class ExpConfig(ConfigObj):
         pre_config = None
 
         config_lines.seek(0)
-        ConfigObj.__init__(self, io.TextIOWrapper(config_lines),
-                           interpolation=False)
+        ConfigObj.__init__(self, io.TextIOWrapper(config_lines))
         self.walk(uneval_key)
 
diff --git a/getconfig b/getconfig
index e2400cd..48d9601 100755
--- a/getconfig
+++ b/getconfig
@@ -39,8 +39,8 @@ try:
 except IOError as error:
     die("'{0}': {1}".format(error.filename, error.strerror))
 
-config_data = ConfigObj(update_data.get_config_file(), interpolation=False,
-                        write_empty_values=True, indent_type=args.indent_string)
+config_data = ConfigObj(update_data.get_config_file(), write_empty_values=True,
+                        indent_type=args.indent_string)
 for d in update_data.get_config_dicts():
     config_data.merge(d)
 
diff --git a/mkexp b/mkexp
index 322b60d..e147d7b 100755
--- a/mkexp
+++ b/mkexp
@@ -401,7 +401,7 @@ template_env.tests['set'] = is_set
 # Read and store configuration info from input and experiments' library
 # Store environment as default for control settings, then add config from files
 
-extra_dict = ConfigObj(interpolation=False)
+extra_dict = ConfigObj()
 for assign_dict in expargparse.assigns_to_dicts(args):
     extra_dict.merge(assign_dict)
 extra_dict['mkexp_input'] = 'Generated by {0} ({1}) {2}'.format(
@@ -504,7 +504,7 @@ def extend(subjob, jobs_config, extended_jobs, job_dict=None):
             extend(extended_job, jobs_config, extended_jobs, job_dict)
 
         # Add global job settings
-        pre_config = ConfigObj(interpolation=False)
+        pre_config = ConfigObj()
         if job_dict:
             pre_config.merge(job_dict)
 
@@ -548,8 +548,7 @@ for subjob, subconfig in jobs_config.items():
         config_lines = io.BytesIO()
         config.write(config_lines)
         config_lines.seek(0)
-        job_config = ConfigObj(io.TextIOWrapper(config_lines),
-                               interpolation=False)
+        job_config = ConfigObj(io.TextIOWrapper(config_lines))
 
         # Check namelist override
         if 'namelists' in subconfig:
@@ -595,7 +594,7 @@ for subjob, subconfig in jobs_config.items():
                 if use_template:
                     if use_template.lower() == 'true':
                         use_template = namelist
-                    namelist_config = ConfigObj(job_config, interpolation=False)
+                    namelist_config = ConfigObj(job_config)
                     namelist_config['_'] = job_config
                     namelist_config.merge(expconfig.odict(groups))
                     expconfig.merge_comments(namelist_config, groups)
diff --git a/selconfig b/selconfig
index 3ba0c35..1134935 100755
--- a/selconfig
+++ b/selconfig
@@ -45,13 +45,13 @@ try:
     config_file = args.config
     if config_file == '-':
         config_file = sys.stdin
-    config_data = ConfigObj(config_file, interpolation=False, file_error=True)
+    config_data = ConfigObj(config_file, file_error=True)
 except IOError as error:
     die(error.message)
 
 # Walk config to the appropriate section and create output structure
 
-selected_data = ConfigObj(interpolation=False, write_empty_values=True,
+selected_data = ConfigObj(write_empty_values=True,
                           indent_type='  ')
 if args.section:
     config = config_data
diff --git a/setconfig b/setconfig
index 6060a05..0e74e59 100755
--- a/setconfig
+++ b/setconfig
@@ -51,14 +51,13 @@ try:
     config_file = args.config
     if config_file == '-':
         config_file = sys.stdin
-    config_data = ConfigObj(config_file, interpolation=False, file_error=True,
+    config_data = ConfigObj(config_file, file_error=True,
                             write_empty_values=True)
     # Add settings from other files
     for add_file in args.add:
         if add_file == '-':
             add_file = sys.stdin
-        config_data.merge(ConfigObj(add_file, interpolation=False,
-                                    file_error=True))
+        config_data.merge(ConfigObj(add_file, file_error=True))
 except IOError as error:
     die(error.message)
 
diff --git a/test.py b/test.py
index e627d0c..fdb1335 100644
--- a/test.py
+++ b/test.py
@@ -8,6 +8,8 @@ import subprocess
 import sys
 import unittest
 
+import expconfig
+
 from os.path import join
 
 
@@ -1708,6 +1710,14 @@ class UnicodeTestCase(MkexpSimpleTestCase):
             VAR = ÄÖÜäöüß😉
         """)
 
+class ExpConfigTestCase(unittest.TestCase):
+
+    def test_default_interpolation_false(self):
+        config = expconfig.ConfigObj({
+            'var1': 'value1',
+            'var2': '$var1',
+        })
+        self.assertEqual(config['var2'], '$var1')
 
 if __name__ == '__main__':
     unittest.main()
diff --git a/unmergeconfig b/unmergeconfig
index 4d2337f..f83eb73 100755
--- a/unmergeconfig
+++ b/unmergeconfig
@@ -97,9 +97,8 @@ args = command_line.parse_args()
 # File handling
 
 try:
-    base_config = ConfigObj(args.base_config, interpolation=False,
-        file_error=True)
-    config = ConfigObj(args.config, interpolation=False, file_error=True)
+    base_config = ConfigObj(args.base_config, file_error=True)
+    config = ConfigObj(args.config, file_error=True)
 except IOError as error:
     die(error.args[0])
 
-- 
GitLab