]> git.cworth.org Git - zombocom-ai/blobdiff - interpret-cairo-to-svg.py
Hook the code panel up to actually execute something
[zombocom-ai] / interpret-cairo-to-svg.py
diff --git a/interpret-cairo-to-svg.py b/interpret-cairo-to-svg.py
new file mode 100755 (executable)
index 0000000..bc3ad35
--- /dev/null
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+
+import cairo
+import tempfile
+import os
+import sys
+
+OUTPUT_DIR_PREFIX='/srv/cworth.org/zombocom'
+OUTPUT_DIR="{}/busart".format(OUTPUT_DIR_PREFIX)
+
+input = sys.stdin.read()
+
+# Do at least a modicum of a safety check
+if "import" in input:
+    sys.stderr.write("Error: Cowardly refusing to interpret script with 'import'")
+    sys.exit(1)
+
+(fd, filename) = tempfile.mkstemp(suffix=".svg", prefix="busart", dir=OUTPUT_DIR);
+os.close(fd)
+os.chmod(filename, 0o644);
+
+# Also delete our import for some more safety
+del tempfile
+del os
+del sys
+
+with cairo.SVGSurface(filename, 512, 512) as surface:
+    cr = cairo.Context(surface);
+    del cairo
+    cr.set_line_width(6);
+    exec(input)
+
+web_file = filename.removeprefix(OUTPUT_DIR_PREFIX);
+
+print(web_file);
+
+