From 3140ef8509759f11c16adc04cd3268ffe4aaee7d Mon Sep 17 00:00:00 2001
From: Marko Mecina <marko.mecina@univie.ac.at>
Date: Thu, 29 Jul 2021 18:30:34 +0200
Subject: [PATCH] add converters from JSON spec to py and csv

---
 Ccs/ccs_function_lib.py   | 37 +++++++++++++++++++++++++++---------
 Tst/json_to_barescript.py | 40 +++++++++++++++++++++++++++++++++++++++
 Tst/json_to_csv.py        | 35 ++++++++++++++++++++++++++++++++++
 3 files changed, 103 insertions(+), 9 deletions(-)
 create mode 100755 Tst/json_to_barescript.py
 create mode 100755 Tst/json_to_csv.py

diff --git a/Ccs/ccs_function_lib.py b/Ccs/ccs_function_lib.py
index 573dea3..e7a65c1 100644
--- a/Ccs/ccs_function_lib.py
+++ b/Ccs/ccs_function_lib.py
@@ -2640,12 +2640,12 @@ def source_to_srec(data, outfile, memaddr=0x40180000, header=None, bytes_per_lin
 def get_tc_list(ccf_descr=None):
 
     if ccf_descr is None:
-        cmds = scoped_session_idb.execute('SELECT ccf_cname, ccf_descr, ccf_descr2, ccf_type,ccf_stype, ccf_npars, '
-                                          'cpc_descr, cpc_dispfmt, cdf_eltype FROM ccf LEFT JOIN cdf ON cdf.cdf_cname=ccf.ccf_cname '
+        cmds = scoped_session_idb.execute('SELECT ccf_cname, ccf_descr, ccf_descr2, ccf_type, ccf_stype, ccf_npars, '
+                                          'cpc_descr, cpc_dispfmt, cdf_eltype, cpc_pname, cdf_value, cpc_inter, cpc_radix FROM ccf LEFT JOIN cdf ON cdf.cdf_cname=ccf.ccf_cname '
                                           'LEFT JOIN cpc ON cpc.cpc_pname=cdf.cdf_pname').fetchall()
     else:
-        cmds = scoped_session_idb.execute('SELECT ccf_cname, ccf_descr, ccf_descr2, ccf_type,ccf_stype, ccf_npars, '
-                                          'cpc_descr, cpc_dispfmt, cdf_eltype FROM ccf LEFT JOIN cdf ON cdf.cdf_cname=ccf.ccf_cname '
+        cmds = scoped_session_idb.execute('SELECT ccf_cname, ccf_descr, ccf_descr2, ccf_type, ccf_stype, ccf_npars, '
+                                          'cpc_descr, cpc_dispfmt, cdf_eltype, cpc_pname, cdf_value, cpc_inter, cpc_radix FROM ccf LEFT JOIN cdf ON cdf.cdf_cname=ccf.ccf_cname '
                                           'LEFT JOIN cpc ON cpc.cpc_pname=cdf.cdf_pname WHERE ccf_descr="{}"'.format(ccf_descr)).fetchall()
 
     scoped_session_idb.close()
@@ -2689,16 +2689,16 @@ def get_tc_calibration_and_parameters(ccf_descr=None):
     return calibrations_dict
 
 
-def make_tc_template(ccf_descr, pool_name='LIVE', preamble='cfl.Tcsend_DB', options='', comment=True):
+def make_tc_template(ccf_descr, pool_name='LIVE', preamble='cfl.Tcsend_DB', options='', comment=True, add_parcfg=False):
     try:
         cmd, pars = list(get_tc_list(ccf_descr).items())[0]
     except IndexError:
         raise IndexError('"{}" not found in IDB.'.format(ccf_descr))
     # print(tc_template(cmd, pars, pool_name=pool_name, preamble=preamble, options=options, comment=True))
-    return tc_template(cmd, pars, pool_name=pool_name, preamble=preamble, options=options, comment=comment)
+    return tc_template(cmd, pars, pool_name=pool_name, preamble=preamble, options=options, comment=comment, add_parcfg=add_parcfg)
 
 
-def tc_template(cmd, pars, pool_name='LIVE', preamble='cfl.Tcsend_DB', options='', comment=True):
+def tc_template(cmd, pars, pool_name='LIVE', preamble='cfl.Tcsend_DB', options='', comment=True, add_parcfg=False):
     if comment:
         commentstr = "# TC({},{}): {} [{}]\n# {}\n".format(*cmd[3:], cmd[1], cmd[0], cmd[2])
         newline = '\n'
@@ -2706,12 +2706,31 @@ def tc_template(cmd, pars, pool_name='LIVE', preamble='cfl.Tcsend_DB', options='
         commentstr = ''
         newline = ''
 
+    parcfg = ''
+    if add_parcfg:
+        for par in pars:
+            if par[2] == 'E':
+                if par[4] is not None:
+                    if par[5] == 'E':
+                        parval = '"{}"'.format(par[4])
+                    elif par[6] == 'H':
+                        parval = '0x{}'.format(par[4])
+                    else:
+                        parval = par[4]
+                else:
+                    parval = par[4]
+                line = '{} = {}  # {}\n'.format(par[0], parval, par[3])
+            elif par[2] == 'F':
+                line = '# {} = {}  # {} [NOT EDITABLE]\n'.format(par[0], par[4], par[3])
+            else:
+                line = ''
+            parcfg += line
+
     parstr = ', '.join(parsinfo_to_str(pars))
-    # print(parstr)
     if len(parstr) > 0:
         parstr = ', ' + parstr
     exe = "{}('{}'{}, pool_name='{}'{})".format(preamble, cmd[1], parstr, pool_name, options)
-    return commentstr + exe + newline
+    return commentstr + parcfg + exe + newline
 
 
 def parsinfo_to_str(pars, separator=None):
diff --git a/Tst/json_to_barescript.py b/Tst/json_to_barescript.py
new file mode 100755
index 0000000..48cb0e2
--- /dev/null
+++ b/Tst/json_to_barescript.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+
+import datetime
+import json
+import sys
+
+jfile = sys.argv[1]
+# = 'IASW-FFT-1-TS-1.csv.json'
+
+data = json.load(open(jfile, 'r'))
+date = datetime.datetime.now().strftime('%Y-%m-%d')
+
+script = ''
+
+script += '#--------------------------------------------\n'
+script += '# ' + data['_name'] + '\n'
+script += '# ' + data['_description'] + '\n'
+script += '# Version: ' + data['_version'] + '\n'
+script += '# Author: UVIE\n# Date: {}\n'.format(date)
+script += '#--------------------------------------------\n\n\n'
+
+script += '# Precond.\n# {}\n\n\n'.format(data['_precon_descr'])
+
+for step in data['sequences'][0]['steps']:
+    comment = '# COMMENT: {}\n'.format(step['_step_comment'].strip()) if step['_step_comment'] != '' else ''
+
+    txt = '# STEP {}\n' \
+          '# {}\n' \
+          '{}\n' \
+          '# VERIFICATION: {}\n{}\n\n'.format(step['_step_number'], step['_description'].strip(), step['_command_code'].strip(), step['_verification_description'].strip(), comment)
+
+    script += txt
+
+script += '# Postcond.\n# {}\n'.format(data['_postcon_descr'])
+
+outpath = '/'.join(jfile.split('/')[:-1]) + '/'
+outfile = outpath + data['_name'] + '-' + '-'.join(data['_version'].split('-')[-2:]) + '.py'
+
+with open(outfile, 'w') as fd:
+    fd.write(script)
diff --git a/Tst/json_to_csv.py b/Tst/json_to_csv.py
new file mode 100755
index 0000000..33a044c
--- /dev/null
+++ b/Tst/json_to_csv.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+
+import datetime
+import json
+import sys
+
+jfile = sys.argv[1]
+# = 'IASW-FFT-1-TS-1.csv.json'
+
+data = json.load(open(jfile, 'r'))
+
+header = 'Item|Description|Verification|TestResult'
+name = '{}|{}|Test spec. version: {}|'.format(data['_name'], data['_description'], data['_version'])
+date = 'Date||{}|'.format(datetime.datetime.now().strftime('%Y-%m-%d'))
+precond = 'Precond.|{}||'.format(data['_precon_descr'])
+postcond = 'Postcond.|{}||'.format(data['_postcon_descr'])
+steps = []
+
+for step in data['sequences'][0]['steps']:
+
+    line = 'Step {}|{}|{}|'.format(step['_step_number'], step['_description'], step['_verification_description'])
+    steps.append(line)
+
+    if step['_step_comment'] != '':
+        comment = 'Comment|{}||'.format(step['_step_comment'])
+        steps.append(comment)
+
+
+outpath = '/'.join(jfile.split('/')[:-1]) + '/'
+outfile = outpath + data['_name'] + '-' + '-'.join(data['_version'].split('-')[-2:]) + '.csv_PIPE'
+
+with open(outfile, 'w') as fd:
+    buf = '\n'.join([header, name, date, precond] + steps + [postcond])
+    buf = buf.replace('_', '\\_')
+    fd.write(buf)
-- 
GitLab