From 38e8d33a87e18c9ae5e2940e5a252b13e66646c6 Mon Sep 17 00:00:00 2001
From: Tammo Jan Dijkema <dijkema@astron.nl>
Date: Fri, 27 Sep 2019 15:17:57 +0200
Subject: [PATCH] Prettify parset_concat, allow no args

---
 utils/parset_concat.py | 55 ++++++++++++++++++++++++++++--------------
 1 file changed, 37 insertions(+), 18 deletions(-)

diff --git a/utils/parset_concat.py b/utils/parset_concat.py
index 5c2c87a8..cc4e5983 100755
--- a/utils/parset_concat.py
+++ b/utils/parset_concat.py
@@ -12,7 +12,7 @@ def parse_arguments(args):
     parser.add_argument('step_name', help='name of the step')
     parser.add_argument('step_type', help='type of the step')
     parser.add_argument('--input_parset', default=None, help='input parset')
-    parser.add_argument('step_arguments', help='step arguments', nargs='+')
+    parser.add_argument('step_arguments', help='step arguments', nargs='*')
 
     return parser.parse_args()
 
@@ -25,26 +25,37 @@ def main(args):
                 arguments.step_arguments)
 
 
-def concatenate(parsetfile, step_name, step_type, args):
+def concatenate(lines, step_name, step_type, step_args):
+    """
+    Append a new step to existing parset. Output is printed to stdout.
+
+    Args:
+        lines (List[str]): list of existing lines
+        step_name (str): name of new step
+        step_type (str): type of new step
+        step_args (List[str]): key-value pairs, e.g. ["applybeam=true"]
+
+    Returns:
+        None
+    """
     steps = []
-    if parsetfile and os.path.exists(parsetfile):
-      with open(parsetfile, "r") as f_stream:
-          for line in f_stream.read().splitlines():
-              if line.startswith("steps="):
-                  _, steps = line.split("=")
-                  steps = [step for step in steps.strip().lstrip("[").rstrip("]").split(",") if step]
-              else:
-                  print(line)
+    if lines:
+        for line in lines:
+            if line.startswith("steps="):
+                _, steps = line.split("=")
+                steps = [step for step in steps.strip().lstrip("[").rstrip("]").split(",") if step]
+            else:
+                print(line)
 
     print("%s.type=%s" % (step_name, step_type))
-    for arg in args:
-        if not '=' in arg:
-          logging.error('Argument invalid (es. key=value): %s', arg)
-
-          sys.exit(1)
-        key = '.'.join([step_name, arg.strip()])
+    if step_args:
+        for arg in step_args:
+            if not '=' in arg:
+                logging.error('Argument invalid (e.g. key=value): %s', arg)
+                sys.exit(1)
+            key = '.'.join([step_name, arg.strip()])
+            print(key)
 
-        print(key)
     if step_name in steps:
         logging.error('Step named %s already present in steps: %s', step_name, steps)
         exit(1)
@@ -52,5 +63,13 @@ def concatenate(parsetfile, step_name, step_type, args):
         steps += [step_name]
     print("steps=[%s]" % ",".join(steps))
 
+
+def concatenate_file(parsetfile, step_name, step_type, args):
+    print(args)
+    if parsetfile and os.path.exists(parsetfile):
+      with open(parsetfile, "r") as f_stream:
+          concatenate(f_stream.read().splitlines(), step_name, step_type, args)
+
+
 if __name__=='__main__':
-    main(sys.argv)
\ No newline at end of file
+    main(sys.argv)
-- 
GitLab