~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to console/python-console

  • Committer: mattgiuca
  • Date: 2008-01-25 06:20:32 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:313
test/test_framework: Updated examples, a bit of better descriptions, sample
    partial solutions, etc.

Added all sample subject material.
This has been copied from test/test_framework and modified slightly.
There is now a subject "sample" with 2 worksheets. The 1st worksheet has 3
exercises. These work in IVLE by default.

setup.py: Added code to install subjects into the designated directory. This
means that installing IVLE will result in the sample subjects being
immediately available.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
# usage:
4
4
#   python-console <port> <magic>
5
5
 
 
6
import sys
6
7
import web
7
8
import md5
8
9
import codeop
9
10
import cjson
10
 
 
11
 
globs = {}
12
 
globs['__builtins__'] = globals()['__builtins__']
13
 
locls = {}
14
 
compiler = codeop.CommandCompiler()
15
 
curr_cmd = ''
16
 
 
17
 
def do_chat(txt):
18
 
    if curr_cmd == '':
19
 
        curr_cmd = txt
20
 
    else:
21
 
        curr_cmd = curr_cmd + '\n' + txt
22
 
    try:
23
 
        cmd = compiler(curr_cmd)
24
 
        if cmd is None:
25
 
            # The command was incomplete,
26
 
            # so send back a None, so the
27
 
            # client can print a '...'
28
 
            web.output(cjson.encode(None))
29
 
        else:
30
 
            # The command was complete,
31
 
            # so evaluate it!
32
 
            out = cStringIO.StringIO()
33
 
            sys.stdout = out
34
 
            sys.stderr = out
35
 
            signal.alarm(5)
36
 
            res = eval(cmd, globs, locls)
37
 
            signal.alarm(0)
38
 
            v = (out.getvalue(), res, None)
39
 
            web.output(json.encode(v))
40
 
            curr_cmd = ''
41
 
    except Exception, exc:
42
 
        v = (None, None, str(exc))
43
 
        web.output(json.encode(v))
44
 
        curr_cmd = ''
 
11
import cgi
 
12
import cStringIO
 
13
import signal
 
14
import Queue
 
15
from threading import Thread
 
16
 
 
17
class StdinFromWeb(object):
 
18
    def __init__(self, cmdQ, lineQ):
 
19
        self.cmdQ = cmdQ
 
20
        self.lineQ = lineQ
 
21
 
 
22
    def readline(self):
 
23
        # stop the clock!
 
24
        signal.alarm(0)
 
25
        self.cmdQ.put({"input":None})
 
26
        l = self.lineQ.get()
 
27
        # restart the clock:
 
28
        # Some of our 5 seconds may have elapsed, but
 
29
        # never mind.
 
30
        signal.alarm(5)
 
31
        return l
 
32
 
 
33
class PythonRunner(Thread):
 
34
    def __init__(self, cmdQ, lineQ):
 
35
        self.cmdQ = cmdQ
 
36
        self.lineQ = lineQ
 
37
        Thread.__init__(self)
 
38
 
 
39
    def run(self):
 
40
        self.init_state()
 
41
        compiler = codeop.CommandCompiler()
 
42
 
 
43
        while True:
 
44
            l = self.lineQ.get()
 
45
            if self.curr_cmd == '':
 
46
                self.curr_cmd = l
 
47
            else:
 
48
                self.curr_cmd = self.curr_cmd + '\n' + l
 
49
            try:
 
50
                cmd = compiler(self.curr_cmd)
 
51
                if cmd is None:
 
52
                    # The command was incomplete,
 
53
                    # so send back a None, so the
 
54
                    # client can print a '...'
 
55
                    self.cmdQ.put({"more":None})
 
56
                else:
 
57
                    # The command was complete,
 
58
                    # so evaluate it!
 
59
                    sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
 
60
                    out = cStringIO.StringIO()
 
61
                    sys.stdout = out
 
62
                    sys.stderr = out
 
63
                    signal.alarm(5)
 
64
                    res = eval(cmd, globs, locls)
 
65
                    signal.alarm(0)
 
66
                    self.cmdQ.put({"okay":(out.getvalue(),res)})
 
67
                    curr_cmd = ''
 
68
            except Exception, exc:
 
69
                signal.alarm(0)
 
70
                self.cmdQ.put({"exc":str(exc)})
 
71
                curr_cmd = ''
 
72
 
 
73
    def init_state(self):
 
74
        self.globs = {}
 
75
        self.globs['__builtins__'] = globals()['__builtins__']
 
76
        self.locls = {}
 
77
        self.curr_cmd = ''
45
78
 
46
79
urls = (
47
80
    '/',            'index',
 
81
    '/index.html',  'index',
48
82
    '/(.*\.js)',    'jscript',
49
83
    '/(.*\.css)',   'style',
50
84
    '/chat',        'chat')
78
112
        web.output(file(name, "r").read())
79
113
 
80
114
class chat:
 
115
 
81
116
    def POST(self):
82
117
        inp = web.input()
83
118
 
84
119
        # Authenticate
85
120
        digest = md5.new(inp.text + magic).digest().encode('hex')
86
121
        if inp.digest != digest:
 
122
            web.output("401 Unauthorized")
87
123
            web.ctx.status = '401 Unauthorized'
88
124
            return
89
125
 
90
126
        # Okay, so the authentication succeeded,
91
127
        # so now we have the trivial matter of actually
92
128
        # executing the python....
93
 
        web.output(do_chat(inp.text))
 
129
        lineQ.put(inp.text)
 
130
        r = cmdQ.get()
 
131
        sys.__stderr__.write(cjson.encode(r) + "\n")
 
132
        web.output(cjson.encode(r))
 
133
 
 
134
cmdQ = Queue.Queue()
 
135
lineQ = Queue.Queue()
 
136
interpThread = PythonRunner(cmdQ, lineQ)
94
137
 
95
138
if __name__ == "__main__":
96
 
    # FIXME jail!
97
139
    magic = sys.argv[2]
 
140
    interpThread.setDaemon(True)
 
141
    interpThread.start()
98
142
    web.run(urls, globals())