From f74c85cde7c697d09c03e9b57eeed0c6b87d4654 Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (l)" <j.wuttke@fz-juelich.de>
Date: Tue, 24 Nov 2020 17:55:33 +0100
Subject: [PATCH] start new tool normalize-usercode.py

---
 devtools/code-tools/normalize-usercode.py | 37 +++++++++++++++++++++++
 1 file changed, 37 insertions(+)
 create mode 100755 devtools/code-tools/normalize-usercode.py

diff --git a/devtools/code-tools/normalize-usercode.py b/devtools/code-tools/normalize-usercode.py
new file mode 100755
index 00000000000..b1d2436780d
--- /dev/null
+++ b/devtools/code-tools/normalize-usercode.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+"""
+Reads a BornAgain simulation script, and converts into normal form.
+
+Export to normal form is done by BornAgain's ExportToPython function.
+"""
+
+import argparse
+import bornagain as ba
+
+def normalize_script(fname, inplace):
+    with open(fname, 'rb') as f:
+        ti = f.read()
+    c=compile(ti, fname, 'exec')
+    ns = {}
+    exec(c,ns)
+    globals().update(ns)
+    s = get_simulation()
+    s.setSample(get_sample())
+    t = ba.generatePyExportTest(s)
+    if t == ti:
+        print(f'Nothing changed in file {fname}')
+        return
+    if inplace:
+        with open(fname, 'w') as f:
+            f.write(t)
+    else:
+        print(t)
+
+if __name__ == '__main__':
+
+    parser = argparse.ArgumentParser()
+    parser.add_argument("simulation_script", type=str)
+    parser.add_argument("-i", "--in-place", action="store_true")
+    args = parser.parse_args()
+
+    normalize_script(args.simulation_script, args.in_place)
-- 
GitLab