Add support for --pycode-begin/end-- sections
authorClifford Wolf <clifford@clifford.at>
Mon, 8 May 2017 13:01:56 +0000 (15:01 +0200)
committerClifford Wolf <clifford@clifford.at>
Mon, 8 May 2017 13:07:10 +0000 (15:07 +0200)
sbysrc/sby_core.py

index 3a1ba62f9a510d0ca7d4fb89fe58a68ddef86d99..17c02546888d57d383348e61d17024ed4b798211 100644 (file)
@@ -168,6 +168,25 @@ class SbyJob:
         key = None
 
         with open(filename, "r") as f:
+            with open("%s/config.sby" % workdir, "w") as cfgfile:
+                pycode = None
+                for line in f:
+                    line = line.rstrip("\n")
+                    line = line.rstrip("\r")
+                    if line == "--pycode-begin--":
+                        pycode = ""
+                    elif line == "--pycode-end--":
+                        gdict = globals().copy()
+                        gdict["cfgfile"] = cfgfile
+                        exec("def output(*args, **kwargs):\n  print(*args, **kwargs, file=cfgfile)\n" + pycode, gdict)
+                        pycode = None
+                    else:
+                        if pycode is None:
+                            print(line, file=cfgfile)
+                        else:
+                            pycode += line + "\n"
+
+        with open("%s/config.sby" % workdir, "r") as f:
             for line in f:
                 raw_line = line
                 line = line.strip()