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

« back to all changes in this revision

Viewing changes to console/python-console

  • Committer: drtomc
  • Date: 2008-01-30 22:53:00 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:340
Mostly fix the block-of-text to the console problem. The tutorial system should call "block" rather than "chat" to send the block of text.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
        # stop the clock!
24
24
        signal.alarm(0)
25
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
 
26
        ln = self.lineQ.get()
 
27
        if 'chat' in ln:
 
28
            # restart the clock:
 
29
            # Some of our 5 seconds may have elapsed, but never mind.
 
30
            signal.alarm(5)
 
31
            return ln['chat']
32
32
 
33
33
class PythonRunner(Thread):
34
34
    def __init__(self, cmdQ, lineQ):
41
41
        compiler = codeop.CommandCompiler()
42
42
 
43
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})
 
44
            ln = self.lineQ.get()
 
45
            if 'chat' in ln:
 
46
                mode = 
 
47
                if self.curr_cmd == '':
 
48
                    self.curr_cmd = ln['chat']
56
49
                else:
57
 
                    # The command was complete,
58
 
                    # so evaluate it!
 
50
                    self.curr_cmd = self.curr_cmd + '\n' + ln['chat']
 
51
                try:
 
52
                    cmd = compiler(self.curr_cmd)
 
53
                    if cmd is None:
 
54
                        # The command was incomplete,
 
55
                        # so send back a None, so the
 
56
                        # client can print a '...'
 
57
                        self.cmdQ.put({"more":None})
 
58
                    else:
 
59
                        # The command was complete,
 
60
                        # so evaluate it!
 
61
                        sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
 
62
                        self.out = cStringIO.StringIO()
 
63
                        sys.stdout = out
 
64
                        sys.stderr = out
 
65
                        signal.alarm(5)
 
66
                        res = eval(cmd, globs, locls)
 
67
                        signal.alarm(0)
 
68
                        self.cmdQ.put({"okay":(self.out.getvalue(),res)})
 
69
                        self.curr_cmd = ''
 
70
                except Exception, exc:
 
71
                    signal.alarm(0)
 
72
                    self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
 
73
                    self.curr_cmd = ''
 
74
            if 'block' in ln:
 
75
                # throw away a partial command.
 
76
                self.curr_cmd = ''
 
77
                try:
 
78
                    cmd = compile(ln['block'], "<web session>", 'exec');
 
79
                    
59
80
                    sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
60
 
                    out = cStringIO.StringIO()
 
81
                    self.out = cStringIO.StringIO()
61
82
                    sys.stdout = out
62
83
                    sys.stderr = out
63
84
                    signal.alarm(5)
64
85
                    res = eval(cmd, globs, locls)
65
86
                    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 = ''
 
87
                    self.cmdQ.put({"okay":(self.out.getvalue(),res)})
 
88
                    self.curr_cmd = ''
 
89
                except Exception, exc:
 
90
                    signal.alarm(0)
 
91
                    self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
72
92
 
73
93
    def init_state(self):
74
94
        self.globs = {}
81
101
    '/index.html',  'index',
82
102
    '/(.*\.js)',    'jscript',
83
103
    '/(.*\.css)',   'style',
84
 
    '/chat',        'chat')
 
104
    '/chat',        'chat',
 
105
    '/block',       'block')
85
106
 
86
107
# The global 'magic' is the secret that the client and server share
87
108
# which is used to create and md5 digest to authenticate requests.
126
147
        # Okay, so the authentication succeeded,
127
148
        # so now we have the trivial matter of actually
128
149
        # executing the python....
129
 
        lineQ.put(inp.text)
 
150
        lineQ.put({'chat':inp.text})
 
151
        r = cmdQ.get()
 
152
        sys.__stderr__.write(cjson.encode(r) + "\n")
 
153
        web.output(cjson.encode(r))
 
154
 
 
155
class block:
 
156
 
 
157
    def POST(self):
 
158
        inp = web.input()
 
159
 
 
160
        # Authenticate
 
161
        digest = md5.new(inp.text + magic).digest().encode('hex')
 
162
        if inp.digest != digest:
 
163
            web.output("401 Unauthorized")
 
164
            web.ctx.status = '401 Unauthorized'
 
165
            return
 
166
 
 
167
        # Okay, so the authentication succeeded,
 
168
        # so now we have the trivial matter of actually
 
169
        # executing the python....
 
170
        lineQ.put({'block':inp.text})
130
171
        r = cmdQ.get()
131
172
        sys.__stderr__.write(cjson.encode(r) + "\n")
132
173
        web.output(cjson.encode(r))