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

« back to all changes in this revision

Viewing changes to console/python-console

  • Committer: mattgiuca
  • Date: 2008-01-12 12:41:39 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:196
Added application icons, displayed in the tabs at the top of the IVLE page.
conf.apps: Added a new optional property "icon" to the App class.
    All the apps with tabs now have an icon set.
Added directory: media/images/apps, containing PNG icons (from Nuvola) for
    each of the 5 apps.
dispatch.html: Reads the icon property and writes <img> tags into the tabs,
    displaying the icons.

On a personal note, this looks really awesome! It was a complete accident that
the icons pop up out of the top of the tabs, but I love the look of it.

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
 
7
import web
 
8
import md5
 
9
import codeop
6
10
import cjson
7
 
import codeop
 
11
import cgi
8
12
import cStringIO
9
 
import md5
10
 
import os
11
 
import Queue
12
13
import signal
13
 
import socket
14
 
import sys
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!
 
14
 
 
15
globs = {}
 
16
globs['__builtins__'] = globals()['__builtins__']
 
17
locls = {}
 
18
compiler = codeop.CommandCompiler()
 
19
curr_cmd = ''
 
20
 
 
21
def do_chat(txt):
 
22
    global curr_cmd
 
23
    if curr_cmd == '':
 
24
        curr_cmd = txt
 
25
    else:
 
26
        curr_cmd = curr_cmd + '\n' + txt
 
27
    try:
 
28
        cmd = compiler(curr_cmd)
 
29
        if cmd is None:
 
30
            # The command was incomplete,
 
31
            # so send back a None, so the
 
32
            # client can print a '...'
 
33
            web.output(cjson.encode(None))
 
34
        else:
 
35
            # The command was complete,
 
36
            # so evaluate it!
 
37
            out = cStringIO.StringIO()
 
38
            sys.stdout = out
 
39
            sys.stderr = out
 
40
            signal.alarm(5)
 
41
            res = eval(cmd, globs, locls)
 
42
            signal.alarm(0)
 
43
            v = (out.getvalue(), res, None)
 
44
            web.output(cjson.encode(v))
 
45
            curr_cmd = ''
 
46
    except Exception, exc:
24
47
        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
 
        self.out = cStringIO.StringIO()
38
 
        Thread.__init__(self)
39
 
 
40
 
    def execCmd(self, cmd):
41
 
        try:
42
 
            sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
43
 
            sys.stdout = self.out
44
 
            sys.stderr = self.out
45
 
            signal.alarm(5)
46
 
            res = eval(cmd, self.globs, self.locls)
47
 
            signal.alarm(0)
48
 
            self.cmdQ.put({"okay":(self.out.getvalue(),res)})
49
 
            self.curr_cmd = ''
50
 
            self.out = cStringIO.StringIO()
51
 
        except Exception, exc:
52
 
            signal.alarm(0)
53
 
            self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
54
 
            self.curr_cmd = ''
55
 
            self.out = cStringIO.StringIO()
56
 
 
57
 
    def run(self):
58
 
        self.init_state()
59
 
        compiler = codeop.CommandCompiler()
60
 
 
61
 
        while True:
62
 
            ln = self.lineQ.get()
63
 
            if 'chat' in ln:
64
 
                if self.curr_cmd == '':
65
 
                    self.curr_cmd = ln['chat']
66
 
                else:
67
 
                    self.curr_cmd = self.curr_cmd + '\n' + ln['chat']
68
 
                try:
69
 
                    cmd = compiler(self.curr_cmd)
70
 
                    if cmd is None:
71
 
                        # The command was incomplete,
72
 
                        # so send back a None, so the
73
 
                        # client can print a '...'
74
 
                        self.cmdQ.put({"more":None})
75
 
                    else:
76
 
                        self.execCmd(cmd)
77
 
                except Exception, exc:
78
 
                    signal.alarm(0)
79
 
                    self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
80
 
                    self.curr_cmd = ''
81
 
                    self.out = cStringIO.StringIO()
82
 
            if 'block' in ln:
83
 
                # throw away a partial command.
84
 
                try:
85
 
                    cmd = compile(ln['block'], "<web session>", 'exec');
86
 
                    self.execCmd(cmd)
87
 
                except Exception, exc:
88
 
                    signal.alarm(0)
89
 
                    self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
90
 
                    self.curr_cmd = ''
91
 
                    self.out = cStringIO.StringIO()
92
 
 
93
 
    def init_state(self):
94
 
        self.globs = {}
95
 
        self.globs['__builtins__'] = globals()['__builtins__']
96
 
        self.locls = {}
97
 
        self.curr_cmd = ''
98
 
 
99
 
def daemonize():
100
 
    if os.fork():   # launch child and...
101
 
        os._exit(0) # kill off parent
102
 
    os.setsid()
103
 
    if os.fork():   # launch child and...
104
 
        os._exit(0) # kill off parent again.
105
 
    os.umask(077)
 
48
        v = (None, None, str(exc))
 
49
        web.output(cjson.encode(v))
 
50
        curr_cmd = ''
 
51
 
 
52
urls = (
 
53
    '/',            'index',
 
54
    '/index.html',  'index',
 
55
    '/(.*\.js)',    'jscript',
 
56
    '/(.*\.css)',   'style',
 
57
    '/chat',        'chat')
106
58
 
107
59
# The global 'magic' is the secret that the client and server share
108
60
# which is used to create and md5 digest to authenticate requests.
109
61
# It is assigned a real value at startup.
110
62
magic = ''
111
63
 
112
 
cmdQ = Queue.Queue()
113
 
lineQ = Queue.Queue()
114
 
interpThread = PythonRunner(cmdQ, lineQ)
 
64
class index:
 
65
    def GET(self):
 
66
        inp = web.input()
 
67
 
 
68
        # Authenticate
 
69
        digest = md5.new('hello' + magic).digest().encode('hex')
 
70
        if inp.digest != digest:
 
71
            web.ctx.status = '401 Unauthorized'
 
72
            return
 
73
 
 
74
        # Okay, so the authentication succeeded,
 
75
        # so all we need to do is send back the static
 
76
        # HTML for the console app.
 
77
        web.output(file("index.html", "r").read())
 
78
 
 
79
class jscript:
 
80
    def GET(self, name):
 
81
        web.output(file(name, "r").read())
 
82
 
 
83
class style:
 
84
    def GET(self, name):
 
85
        web.output(file(name, "r").read())
 
86
 
 
87
class chat:
 
88
    def POST(self):
 
89
        inp = web.input()
 
90
        sys.stderr.write(str(inp) + "\n")
 
91
 
 
92
        # Authenticate
 
93
        digest = md5.new(inp.text + magic).digest().encode('hex')
 
94
        if inp.digest != digest:
 
95
            web.ctx.status = '401 Unauthorized'
 
96
            return
 
97
 
 
98
        # Okay, so the authentication succeeded,
 
99
        # so now we have the trivial matter of actually
 
100
        # executing the python....
 
101
        do_chat(inp.text)
115
102
 
116
103
if __name__ == "__main__":
117
 
    port = int(sys.argv[1])
 
104
    # FIXME jail!
118
105
    magic = sys.argv[2]
119
 
 
120
 
    # Attempt to open the socket.
121
 
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
122
 
    s.bind(('', port))
123
 
    s.listen(1)
124
 
 
125
 
    # Excellent! It worked. Let's turn ourself into a daemon,
126
 
    # then get on with the job of being a python interpreter.
127
 
    daemonize()
128
 
 
129
 
    interpThread.setDaemon(True)
130
 
    interpThread.start()
131
 
 
132
 
    while True:
133
 
        (conn, addr) = s.accept()
134
 
        try:
135
 
            # Grab the input
136
 
            buf = cStringIO.StringIO()
137
 
            blk = conn.recv(1024)
138
 
            while blk:
139
 
                buf.write(blk)
140
 
                try:
141
 
                    blk = conn.recv(1024, socket.MSG_DONTWAIT)
142
 
                except:
143
 
                    # Exception thrown if it WOULD block (but we
144
 
                    # told it not to wait) - ie. we are done
145
 
                    blk = None
146
 
            inp = buf.getvalue()
147
 
            msg = cjson.decode(inp)
148
 
            
149
 
            # Check that the message is 
150
 
            digest = md5.new(msg['text'] + magic).digest().encode('hex')
151
 
            if msg['digest'] != digest:
152
 
                conn.close()
153
 
                continue
154
 
 
155
 
            lineQ.put({msg['cmd']:msg['text']})
156
 
            r = cmdQ.get()
157
 
            conn.sendall(cjson.encode(r))
158
 
            conn.close()
159
 
        except Exception, e:
160
 
            conn.close()
 
106
    web.run(urls, globals())