From d1fc388774c07788b8241b9a2ed2782f9089c6a8 Mon Sep 17 00:00:00 2001
From: Karim Ahmed <karim.ahmed@xfel.eu>
Date: Tue, 9 Jun 2020 09:40:02 +0200
Subject: [PATCH] avoid errors when karabo_id not given

---
 webservice/update_config.py | 48 +++++++++++++++++++++++--------------
 webservice/webservice.py    |  2 +-
 2 files changed, 31 insertions(+), 19 deletions(-)

diff --git a/webservice/update_config.py b/webservice/update_config.py
index ffddd23d7..01fecad56 100644
--- a/webservice/update_config.py
+++ b/webservice/update_config.py
@@ -24,20 +24,20 @@ available_options = {
    "SPB_DET_AGIPD1M-1": agipd_options,
    "MID_DET_AGIPD1M-1": agipd_options}
 
+formatter = lambda prog: argparse.HelpFormatter(prog, max_help_position=52)
 parser = argparse.ArgumentParser(
-    description='Request update of configuration')
+    description='Request update of configuration', formatter_class=formatter)
 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('--task', type=str, choices=['correct', 'dark'])
 required_args.add_argument('--proposal', type=str,
-                    help='The proposal number, without leading p, but with leading zeros')  # noqa
-required_args.add_argument('--instrument', type=str,
-                    choices=["SPB", "MID", "FXE", "SCS", "SQS", "HED",
-                             "DETLAB", "CALLAB"], help='The instrument')  # noqa
-required_args.add_argument('--cycle', type=str, help='The facility cycle')
-parser.add_argument('--apply', action='store_true')
+                    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.')
 # remove help calls as they will cause the argument parser to exit
 add_help = False
 if "-h" in sys.argv:
@@ -52,29 +52,41 @@ args = vars(known)
 karabo_id = args["karabo_id"]
 bool_keys = []
 
-for det, val in available_options[karabo_id].items():
-    if val == bool:
-        bool_keys.append(det)
-
-for b in bool_keys:
-    available_options[karabo_id]['no-{}'.format(b)] = bool
 
+# Avoid erros when karabo_id not yet given through the command line.
 if karabo_id is not None:
+    for det, val in available_options[karabo_id].items():
+        if val == bool:
+            bool_keys.append(det)
+
+    for b in bool_keys:
+        available_options[karabo_id]['no-{}'.format(b)] = bool
+
     for option, typ in available_options[karabo_id].items():
         if typ == list:
             nargs = '+'
         else:
             nargs = None
 
-        parser.add_argument(f"--{option}", type=typ, nargs=nargs, help=f"[{typ.__name__}]".upper())
+        parser.add_argument(f"--{option}", type=typ, nargs=nargs,
+                            help=f"Type: {typ.__name__}".upper())
+
+parser.add_argument('--instrument', type=str, choices=["CALLAB"],
+                    help='This is only used for testing purposes.')
 
 if add_help:
     sys.argv.append("--help")
 
 args = vars(parser.parse_args())
 
-task = args['task']
-instrument = args['instrument']
+task = "correct"
+# check if instrument is not given from CLI (e.g. CALLAB)
+if args['instrument'] is None:
+    # extract instrument from karabo_id
+    instrument = karabo_id.split("_")[0]
+else:
+    instrument = args['instrument']
+
 proposal = args['proposal']
 cycle = args['cycle']
 
@@ -118,7 +130,7 @@ con = zmq.Context()
 socket = con.socket(zmq.REQ)
 con = socket.connect("tcp://max-exfl016:5555")
 msg = "','".join(["update_conf", "SASEX", args["karabo_id"],
-                  args["instrument"], args["cycle"], args["proposal"],
+                  instrument, args["cycle"], args["proposal"],
                   json.dumps(new_conf), str(args["apply"])])
 socket.send("['{}']".format(msg).encode())
 resp = socket.recv_multipart()[0]
diff --git a/webservice/webservice.py b/webservice/webservice.py
index b3b9b7acb..1e3e638f2 100644
--- a/webservice/webservice.py
+++ b/webservice/webservice.py
@@ -785,7 +785,7 @@ async def server_runner(config, mode):
                             logging.error(Errors.MDC_RESPONSE.format(response))
                         return
                 except Exception as corr_e:
-                    logging.error(f"Correct Error: {corr_e}")
+                    logging.error(f"Error while correction: {str(corr_e)}")
                     response = mdc.update_run_api(rid, {
                         'flg_cal_data_status': 'NA',
                         'cal_pipeline_reply': Errors.REQUEST_FAILED})
-- 
GitLab