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

« back to all changes in this revision

Viewing changes to console/python-console

  • Committer: William Grant
  • Date: 2009-07-04 11:51:46 UTC
  • mto: (1294.4.2 ui-the-third)
  • mto: This revision was merged to the branch mainline in revision 1353.
  • Revision ID: grantw@unimelb.edu.au-20090704115146-q6bv2b07g9hcyein
Replace all base view __init__s with one at the root that sets the context.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
 
3
 
# usage:
4
 
#   python-console <port> <magic>
5
 
 
6
 
import sys
7
 
import web
8
 
import md5
9
 
import codeop
10
 
import cjson
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
 
        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
 
 
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
 
            ln = self.lineQ.get()
45
 
            if 'chat' in ln:
46
 
                mode = 
47
 
                if self.curr_cmd == '':
48
 
                    self.curr_cmd = ln['chat']
49
 
                else:
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
 
                    
80
 
                    sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
81
 
                    self.out = cStringIO.StringIO()
82
 
                    sys.stdout = out
83
 
                    sys.stderr = out
84
 
                    signal.alarm(5)
85
 
                    res = eval(cmd, globs, locls)
86
 
                    signal.alarm(0)
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))})
92
 
 
93
 
    def init_state(self):
94
 
        self.globs = {}
95
 
        self.globs['__builtins__'] = globals()['__builtins__']
96
 
        self.locls = {}
97
 
        self.curr_cmd = ''
98
 
 
99
 
urls = (
100
 
    '/chat',        'chat',
101
 
    '/block',       'block')
102
 
 
103
 
# The global 'magic' is the secret that the client and server share
104
 
# which is used to create and md5 digest to authenticate requests.
105
 
# It is assigned a real value at startup.
106
 
magic = ''
107
 
 
108
 
class chat:
109
 
 
110
 
    def POST(self):
111
 
        inp = web.input()
112
 
 
113
 
        # Authenticate
114
 
        digest = md5.new(inp.text + magic).digest().encode('hex')
115
 
        if inp.digest != digest:
116
 
            web.output("401 Unauthorized")
117
 
            web.ctx.status = '401 Unauthorized'
118
 
            return
119
 
 
120
 
        # Okay, so the authentication succeeded,
121
 
        # so now we have the trivial matter of actually
122
 
        # executing the python....
123
 
        lineQ.put({'chat':inp.text})
124
 
        r = cmdQ.get()
125
 
        sys.__stderr__.write(cjson.encode(r) + "\n")
126
 
        web.output(cjson.encode(r))
127
 
 
128
 
class block:
129
 
 
130
 
    def POST(self):
131
 
        inp = web.input()
132
 
 
133
 
        # Authenticate
134
 
        digest = md5.new(inp.text + magic).digest().encode('hex')
135
 
        if inp.digest != digest:
136
 
            web.output("401 Unauthorized")
137
 
            web.ctx.status = '401 Unauthorized'
138
 
            return
139
 
 
140
 
        # Okay, so the authentication succeeded,
141
 
        # so now we have the trivial matter of actually
142
 
        # executing the python....
143
 
        lineQ.put({'block':inp.text})
144
 
        r = cmdQ.get()
145
 
        sys.__stderr__.write(cjson.encode(r) + "\n")
146
 
        web.output(cjson.encode(r))
147
 
 
148
 
cmdQ = Queue.Queue()
149
 
lineQ = Queue.Queue()
150
 
interpThread = PythonRunner(cmdQ, lineQ)
151
 
 
152
 
if __name__ == "__main__":
153
 
    magic = sys.argv[2]
154
 
    interpThread.setDaemon(True)
155
 
    interpThread.start()
156
 
    web.run(urls, globals())