From 0bf928f218725c3b6fefd3c3e184d8350b81fc11 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Vonheiden?= <bjoern.vonheiden@hotmail.de>
Date: Mon, 23 Nov 2020 15:23:48 +0100
Subject: [PATCH] enable the python scripts to load key value pairs for
 configuration

---
 execution/lib/cli_parser.py | 36 ++++++++++++++++++++++++++++++++++++
 execution/theodolite.yaml   |  2 ++
 2 files changed, 38 insertions(+)

diff --git a/execution/lib/cli_parser.py b/execution/lib/cli_parser.py
index f785bce4f..218999b17 100644
--- a/execution/lib/cli_parser.py
+++ b/execution/lib/cli_parser.py
@@ -10,6 +10,36 @@ def env_list_default(env, tf):
         v = [tf(s) for s in v.split(',')]
     return v
 
+
+def key_values_to_dict(kvs):
+    """
+    Given a list with key values in form `Key=Value` it creates a dict from it.
+    """
+    my_dict = {}
+    for kv in kvs:
+        k,v = kv.split("=")
+        my_dict[k] = v
+    return my_dict
+
+
+def env_dict_default(env):
+    """
+    Makes a dict from an environment string.
+    """
+    v = os.environ.get(env)
+    if v is not None:
+        return key_values_to_dict(v.split(','))
+
+
+class StoreDictKeyPair(argparse.Action):
+     def __init__(self, option_strings, dest, nargs=None, **kwargs):
+         self._nargs = nargs
+         super(StoreDictKeyPair, self).__init__(option_strings, dest, nargs=nargs, **kwargs)
+     def __call__(self, parser, namespace, values, option_string=None):
+         my_dict = key_values_to_dict(values)
+         setattr(namespace, self.dest, my_dict)
+
+
 def default_parser(description):
     """
     Returns the default parser that can be used for thodolite and run uc py
@@ -62,6 +92,12 @@ def default_parser(description):
                         metavar='<path>',
                         default=os.environ.get('RESULT_PATH', 'results'),
                         help='A directory path for the results')
+    parser.add_argument("--configurations",
+                        dest="configurations",
+                        action=StoreDictKeyPair,
+                        nargs="+",
+                        default=env_dict_default('CONFIGURATIONS'),
+                        metavar="KEY=VAL")
     return parser
 
 def benchmark_parser(description):
diff --git a/execution/theodolite.yaml b/execution/theodolite.yaml
index 1c1ba6a1f..9ea35ebf9 100644
--- a/execution/theodolite.yaml
+++ b/execution/theodolite.yaml
@@ -36,6 +36,8 @@ spec:
               value: "http://prometheus-operated:9090"
             # - name: NAMESPACE
             #   value: "default"
+            # - name: CONFIGURATIONS
+            #   value: "COMMIT_INTERVAL_MS=100, NUM_STREAM_THREADS=1"
             - name: RESULT_PATH
               value: "results"
             - name: PYTHONUNBUFFERED
-- 
GitLab