-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuserspace.py
54 lines (45 loc) · 1.27 KB
/
userspace.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# ------------------------------------------------------------------
# userspace.py
#
# Things related to user code execution
# ------------------------------------------------------------------
import time
import vec2d
import pygame
import keymap
import threading
# a 'wait()' function for user code - sleep in blocks to allow
# cancelling
wait_block = 1
def wait(t):
loops = t // wait_block
extra = t - float(loops * wait_block)
while loops > 0:
loops -= 1
time.sleep(wait_block)
if (extra):
time.sleep(extra)
# an 'output()' function for user code - print to the GUI.
# TODO: Make this actually print to the GUI
def output(s):
print s
# the space visible to user code
space = dict(
Vec2d = vec2d.Vec2d,
wait = wait,
output = output,
)
def run(code):
exec(code, space, space)
# reset keybindings to defaults
def reset_keybindings():
space['keybindings'] = {}
# run action associated with a keybinding
def do_key(keycode):
if 'keybindings' not in space or type(space['keybindings']) is not dict:
reset_keybindings()
code = space['keybindings'].get(keymap.key_to_str[keycode])
if code:
from gui import Runner
runner = Runner("keypress", code)
runner.start()