From 1290575a00d880ef57b487501ddd67d1847e9818 Mon Sep 17 00:00:00 2001
From: karnem <mikhail.karnevskiy@desy.de>
Date: Fri, 14 Jun 2019 09:19:56 +0200
Subject: [PATCH] Use templates for parameters table

---
 xfel_calibrate/calibrate.py | 68 ++++++++++++++++++++-----------------
 1 file changed, 37 insertions(+), 31 deletions(-)

diff --git a/xfel_calibrate/calibrate.py b/xfel_calibrate/calibrate.py
index 8290dc6b7..b59ac7d09 100755
--- a/xfel_calibrate/calibrate.py
+++ b/xfel_calibrate/calibrate.py
@@ -17,7 +17,8 @@ from uuid import uuid4
 import warnings
 from .settings import *
 from .notebooks import notebooks
-
+from jinja2 import Template
+import textwrap
 
 # Add a class combining raw description formatting with
 # Metavariable default outputs
@@ -443,59 +444,64 @@ def concurrent_run(temp_path, nb, nbname, args, cparm=None, cval=None,
 
 
 def make_par_table(parms, temp_path, run_uuid):
-    '''
+    """
     Create a table with input parameters if the notebook
 
     :param parms: parameters of the notebook
     :param temp_path: path to temporary directory for job outputs
     :param run_uuid: inset of folder name containing job output
-    '''
+    """
+
+    # Add space in long strings to wrap them in latex
     def split_len(seq, length):
-        l = [seq[i:i+length] for i in range(0, len(seq), length)]
+        l = [seq[i:i + length] for i in range(0, len(seq), length)]
         return ' '.join(l)
 
-    # prepare strings and estimate their length
+    # Prepare strings and estimate their length
     l_parms = []
     len_parms = [0, 0]
     max_len = [30, 30]
     for p in parms:
         name = p.name.replace('_', '-')
-        if len(name)>max_len[0]:
+        if len(name) > max_len[0]:
             len_parms[0] = max_len[0]
             name = split_len(name, max_len[0])
 
         value = str(p.value)
-        if len(value)>max_len[1]:
+        if len(value) > max_len[1]:
             len_parms[1] = max_len[1]
             value = split_len(value, max_len[1])
         if p.type is str:
             value = "``{}''".format(value)
         value = value.replace('_', '\\_')
-
         comment = str(p.comment)[1:].replace('_', '\\_')
-
-        l_parms.append([name, value, comment ])
-
-    with open("{}/slurm_tmp_{}/InputParameters.rst".format(temp_path, run_uuid), "w") as finfile:
-        finfile.write("Input of the calibration pipeline \n")
-        finfile.write("================================= \n\n")
-        finfile.write(".. math::\n")
-        col_type = ['l', 'c', 'p{.3\\textwidth}']
-
-        # fix column width is needed
-        if len_parms[0]==max_len[0]:
-            col_type[0] = 'p{.3\\textwidth}'
-        if len_parms[1]==max_len[1]:
-            col_type[1] = 'p{.3\\textwidth}'
-
-        finfile.write("    \\begin{{tabular}}{{ {} {} {} }}\n".format(*col_type))
-        finfile.write("    \\hline\n")
-        for p in l_parms:
-            tmpl = "    \\textbf{{-{{}}-{} }} & {} & {}\\\\\n"
-            finfile.write(tmpl.format(*p))
-
-        finfile.write("    \\hline\n")
-        finfile.write("    \\end{tabular}\n")
+        l_parms.append([name, value, comment])
+
+    # Fix column width is needed
+    col_type = ['l', 'c', 'p{.3\\textwidth}']
+    if len_parms[0] == max_len[0]:
+        col_type[0] = 'p{.3\\textwidth}'
+    if len_parms[1] == max_len[1]:
+        col_type[1] = 'p{.3\\textwidth}'
+
+    tmpl = Template('''
+                    Input of the calibration pipeline 
+                    =================================
+                    
+                    .. math::
+                    
+                        \\begin{tabular}{ {% for k in p %}{{ k }}{%- endfor %}  } 
+                        \hline
+                        {% for k in lines %}
+                        {{ k[0] }} & {{ k[1] }} & {{ k[2] }} \\\\
+                        {%- endfor %}
+                        \hline
+                        \end{tabular}
+                    ''')
+
+    f_name = "{}/slurm_tmp_{}/InputParameters.rst".format(temp_path, run_uuid)
+    with open(f_name, "w") as finfile:
+        finfile.write(textwrap.dedent(tmpl.render(p=col_type, lines=l_parms)))
 
 
 def run():
-- 
GitLab