From e6e76041fcc8f521b769476f76dc08b33b3e43b0 Mon Sep 17 00:00:00 2001
From: Karim Ahmed <karim.ahmed@xfel.eu>
Date: Wed, 14 Jul 2021 19:03:59 +0200
Subject: [PATCH] FEAT / Expose dark configurations in update_config.py

---
 tests/test_update_config.py |  6 ++-
 webservice/update_config.py | 80 ++++++++++++++++++++++++++-----------
 2 files changed, 62 insertions(+), 24 deletions(-)

diff --git a/tests/test_update_config.py b/tests/test_update_config.py
index 090a9ebb3..e06bc0524 100644
--- a/tests/test_update_config.py
+++ b/tests/test_update_config.py
@@ -33,6 +33,7 @@ def test_main_sys_exit(capsys):
             "update_config",
             "--karabo-id", "SPB_DET_AGIPD1M-1",
             "--webservice-address", "inproc://socket",
+            "--dark",
         ]
     ):
 
@@ -68,6 +69,7 @@ def test_main(capsys):
             "--cycle", "000000",
             "--rel-gain", "true",
             "--webservice-address", "inproc://socket",
+            "--correct",
         ],
     ):
         with patch("zmq.Context", return_value=context):
@@ -119,6 +121,7 @@ args_1 = {
     "karabo_id": "SPB_DET_AGIPD1M-1",
     "proposal": 000000,
     "cycle": 000000,
+    "correct": True,
     "apply": False,
     "webservice_port": "tcp://max-exfl016:5555",
     "instrument": None,
@@ -147,7 +150,8 @@ args_2["no_rel_gain"] = True
 def test_add_available_configs_to_arg_parser():
     """Test creating available configuration
     dictionary with update booleans."""
-    available_conf = _add_available_configs_to_arg_parser("SPB_DET_AGIPD1M-1")
+    available_conf = _add_available_configs_to_arg_parser(
+        karabo_id="SPB_DET_AGIPD1M-1", action="correct")
     assert available_conf == EXPECTED_CONF
 
 
diff --git a/webservice/update_config.py b/webservice/update_config.py
index f12bcccf7..5e5960e37 100755
--- a/webservice/update_config.py
+++ b/webservice/update_config.py
@@ -7,6 +7,8 @@ import zmq
 
 # Defining the exposed configurations by the script.
 AGIPD_CONFIGURATIONS = {
+    "correct":
+    {
         "force-hg-if-below": {'typ': int},
         "rel-gain": {'typ': bool},
         "xray-gain": {'typ': bool},
@@ -18,6 +20,21 @@ AGIPD_CONFIGURATIONS = {
                        'msg': "Range list of maximum pulse indices "
                               "(--max-pulses start end step). "
                               "3 max input elements. "},
+    },
+    "dark":
+    {
+        "thresholds-offset-hard-hg": {'typ': list},
+        "thresholds-offset-hard-mg": {'typ': list},
+        "thresholds-offset-hard-lg": {'typ': list},
+        "thresholds-offset-hard-hg-fixed": {'typ': list},
+        "thresholds-offset-hard-mg-fixed": {'typ': list},
+        "thresholds-offset-hard-lg-fixed": {'typ': list},
+        "thresholds-offset-sigma": {'typ': list},
+        "thresholds-noise-hard-hg": {'typ': list},
+        "thresholds-noise-hard-mg": {'typ': list},
+        "thresholds-noise-hard-lg": {'typ': list},
+        "thresholds-noise-sigma": {'typ': list},
+    }
 }
 
 DATA_MAPPING = {
@@ -44,16 +61,24 @@ parser = argparse.ArgumentParser(
     conflict_handler="resolve",
 )
 required_args = parser.add_argument_group('required arguments')
-required_args.add_argument('--karabo-id', type=str,
-                           choices=['SPB_DET_AGIPD1M-1',
-                                    'MID_DET_AGIPD1M-1'])
-required_args.add_argument('--proposal', type=str,
-                           help='The proposal number, without leading p, '
-                                'but with leading zeros.')
+
+required_args.add_argument(
+    '--karabo-id', type=str,
+    choices=['SPB_DET_AGIPD1M-1', 'MID_DET_AGIPD1M-1'])
+required_args.add_argument(
+    '--proposal', type=str,
+    help='The proposal number, without leading p, but with leading zeros.')
 required_args.add_argument('--cycle', type=str, help='The facility cycle.')
-parser.add_argument('--apply', action='store_true',
-                    help='Apply and push the requested '
-                         'configuration update to the git.')
+
+action_group = required_args.add_mutually_exclusive_group()
+action_group.add_argument(
+    '--correct', '-c', action='store_true')
+action_group.add_argument(
+    '--dark', '-d', action='store_true')
+
+parser.add_argument(
+    '--apply', action='store_true',
+    help='Apply and push the requested configuration update to the git.')
 parser.add_argument(
     '--webservice-address',
     type=str,
@@ -69,7 +94,7 @@ parser.add_argument(
 )
 
 
-def _add_available_configs_to_arg_parser(karabo_id: str):
+def _add_available_configs_to_arg_parser(karabo_id: str, action: str):
     """Add the available configuration for the selected detector
     to the argument parser.
 
@@ -79,10 +104,13 @@ def _add_available_configs_to_arg_parser(karabo_id: str):
 
     available_conf = [{}, DATA_MAPPING]
     # adding "no" bools to available configurations
-    for key, val in AVAILABLE_DETECTORS[karabo_id][0].items():
+
+    # Loop over action configurations in available_detectors dictionary.
+    for key, val in AVAILABLE_DETECTORS[karabo_id][0][action].items():
         available_conf[0][key] = val
         if val['typ'] == bool:
             available_conf[0][f'no-{key}'] = {'typ': bool}
+
     for conf in available_conf:
         for option, info in conf.items():
             typ = info['typ']
@@ -126,8 +154,9 @@ def _create_new_config_from_args_input(
     """Create an updated configuration from CLI args
     with data-mapping and correct configs."""
     karabo_id = args["karabo_id"]
-    task = "correct"
-    new_conf = {task: {instrument: {karabo_id: {}}}}
+    action = "dark" if args.get("dark") else "correct"
+    new_conf = {action: {instrument: {karabo_id: {}}}}
+
     for key, value in args.items():
         key = key.replace("_", "-")
         for conf in available_conf:
@@ -138,7 +167,7 @@ def _create_new_config_from_args_input(
                 # convert no arguments to bool false
                 if 'no-' in key and isinstance(value, bool):
                     if key not in AVAILABLE_DETECTORS[karabo_id][0].keys():
-                        new_conf[task][instrument][karabo_id][key.replace('no-', '')] = False  # noqa
+                        new_conf[action][instrument][karabo_id][key.replace('no-', '')] = False  # noqa
                     # avoid saving the "no-"key into the updated config
                     continue
 
@@ -148,11 +177,11 @@ def _create_new_config_from_args_input(
                         new_conf['data-mapping'] = {karabo_id: {key: {}}}
                     new_conf['data-mapping'][karabo_id][key] = value
                 else:
-                    new_conf[task][instrument][karabo_id][key] = value
+                    new_conf[action][instrument][karabo_id][key] = value
     return new_conf
 
 
-def main(testing=False):
+def main():
     # remove help calls, to avoid exiting the argument parser.
     argv = sys.argv[1:]
     add_help = False
@@ -167,10 +196,14 @@ def main(testing=False):
     karabo_id = args["karabo_id"]
     webservice_address = args["webservice_address"]
     instrument = args['instrument']
+    proposal = args['proposal']
+    cycle = args['cycle']
+    action = "dark" if args.get("dark") else "correct"
 
-    # Avoid erros when karabo_id not given through the command line.
-    if karabo_id is not None:
-        available_conf = _add_available_configs_to_arg_parser(karabo_id)
+    # Avoid errors when karabo_id and action are not given.
+    if karabo_id and action:
+        available_conf = _add_available_configs_to_arg_parser(
+            karabo_id, action)
         # check if instrument is not given from CLI (e.g. CALLAB)
         if instrument is None:
             # extract instrument from karabo_id
@@ -183,10 +216,11 @@ def main(testing=False):
 
     args = vars(parser.parse_args(argv))
 
-    proposal = args['proposal']
-    cycle = args['cycle']
-
-    if instrument is None or proposal is None or cycle is None:
+    if (
+        instrument is None or
+        proposal is None or
+        cycle is None
+    ):
         print("Need to define all fields")
         sys.exit(1)
 
-- 
GitLab