]> git.cworth.org Git - zombocom-ai/commitdiff
Hook the code panel up to actually execute something
authorCarl Worth <cworth@cworth.org>
Fri, 23 Dec 2022 02:04:13 +0000 (18:04 -0800)
committerCarl Worth <cworth@cworth.org>
Fri, 23 Dec 2022 19:51:10 +0000 (11:51 -0800)
And switch from SvgTurtle to Cairo instead, since I actually wrote
that and all.

There's no puzzle here yet, but it does have all the interactivity
necessary to do something that's hopefully interesting.

bus.html
index.js
interpret-cairo-to-svg.py [new file with mode: 0755]
run-turtle.py [changed mode: 0755->0644]

index 845b048ec24039fd7792e2e9c1b81a1a649f7cb4..214174cca0d57069b2991fde27ec3425d795682c 100644 (file)
--- a/bus.html
+++ b/bus.html
        Magic School Bus Central Processor
       </h1>
       <form id="form">
-       <textarea id="code" rows="10" width="100%">t.forward(100);
-t.right(90);
-t.forward(100);
-       </textarea>
+       <textarea id="code" rows="10" width="100%">cr.move_to(10,10);
+cr.line_to(100, 100);
+cr.set_line_width(6);
+cr.stroke();</textarea>
        <button type="submit">Run code</button>
       </form>
       <img id="output"></img>
index 9b7270b7673e03e1ae78c803b68c6af054551b9f..b0a115f79dd8d0e1393cde0600838203df5e19a7 100644 (file)
--- a/index.js
+++ b/index.js
@@ -15,7 +15,7 @@ const port = 2122;
 
 const python_path = '/usr/bin/python3'
 const generate_image_script = '/home/cworth/src/zombocom-ai/generate-image.py'
-const run_turtle_script = '/home/cworth/src/zombocom-ai/run-turtle.py'
+const interpret_cairo_script = '/home/cworth/src/zombocom-ai/interpret-cairo-to-svg.py'
 const state_file = 'zombocom-state.json'
 const targets_dir = '/srv/cworth.org/zombocom/targets'
 const images_dir = '/srv/cworth.org/zombocom/images'
@@ -197,7 +197,7 @@ io_bus.on("connection", (socket) => {
 
     socket.on('run', code => {
        try {
-           output = child_process.execFileSync(python_path, [run_turtle_script, code], { input: code });
+           output = child_process.execFileSync(python_path, [interpret_cairo_script, code], { input: code });
            // Grab just first line of output
            const nl = output.indexOf("\n");
            if (nl === -1)
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);
+
+
old mode 100755 (executable)
new mode 100644 (file)
index fb74b02..bb117fd
@@ -1,32 +1,35 @@
 #!/usr/bin/env python3
 
-from svg_turtle import SvgTurtle
+import cairo
 import tempfile
 import os
+import sys
 
 OUTPUT_DIR_PREFIX='/srv/cworth.org/zombocom'
 OUTPUT_DIR="{}/busart".format(OUTPUT_DIR_PREFIX)
 
-t = SvgTurtle(512,512);
+input = sys.stdin.read()
 
-t.pencolor('red');
-
-t.penup();
-t.right(180);
-t.forward(200);
-t.right(180);
-t.pendown();
-
-for i in range(50):
-    t.forward(100);
-    t.left(123);
+# 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)
-
-t.save_as(filename);
 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);