From: Carl Worth Date: Fri, 23 Dec 2022 02:04:13 +0000 (-0800) Subject: Hook the code panel up to actually execute something X-Git-Url: https://git.cworth.org/git?p=zombocom-ai;a=commitdiff_plain;h=1e320ca83bc7836cc1462bc67e4048c5c074a469 Hook the code panel up to actually execute something 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. --- diff --git a/bus.html b/bus.html index 845b048..214174c 100644 --- a/bus.html +++ b/bus.html @@ -121,10 +121,10 @@ Magic School Bus Central Processor
- +
diff --git a/index.js b/index.js index 9b7270b..b0a115f 100644 --- 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 index 0000000..bc3ad35 --- /dev/null +++ b/interpret-cairo-to-svg.py @@ -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); + + diff --git a/run-turtle.py b/run-turtle.py old mode 100755 new mode 100644 index fb74b02..bb117fd --- a/run-turtle.py +++ b/run-turtle.py @@ -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);