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

« back to all changes in this revision

Viewing changes to scripts/python-console

  • Committer: stevenbird
  • Date: 2008-02-20 11:50:03 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:523
Adding ReStructured Text preprocessing of exercise descriptions,
so that the markup for exercises is the same as for the tutorials.

www/apps/tutorial/rst.py
* cut down version of NLTK rst.py, which extends docutils
  with colourised python codeblocks
* modified to work with strings rather than files

www/apps/tutorial/__init__.py
* call rst() main function on exercise description once
  it is extracted from the source XML file

www/apps/tutorialservice/test/TestFramework.py
* clean up docstring

www/apps/tutorialservice/test/parse_exercise.py
* remove backwards compatibility for desc attribute
  (now authors must provide both pass and fail strings)

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 cjson
 
7
import codeop
 
8
import cStringIO
 
9
import md5
 
10
import os
 
11
import Queue
 
12
import signal
 
13
import socket
 
14
import sys
 
15
from threading import Thread
 
16
 
 
17
import common.chat
 
18
 
 
19
class ExpiryTimer(object):
 
20
    def __init__(self, idle):
 
21
        self.idle = idle
 
22
        signal.signal(signal.SIGALRM, partial(self.timeout,self))
 
23
 
 
24
    def ping(self):
 
25
        signal.alarm(self.idle)
 
26
 
 
27
    def start(self, time):
 
28
        signal.alarm(time)
 
29
 
 
30
    def stop(self):
 
31
        self.ping()
 
32
 
 
33
    def timeout(self, signum, frame):
 
34
        sys.exit(1)
 
35
        
 
36
class StdinFromWeb(object):
 
37
    def __init__(self, cmdQ, lineQ):
 
38
        self.cmdQ = cmdQ
 
39
        self.lineQ = lineQ
 
40
 
 
41
    def readline(self):
 
42
        # stop the clock!
 
43
        self.cmdQ.put({"input":None})
 
44
        expiry.ping()
 
45
        ln = self.lineQ.get()
 
46
        if 'chat' in ln:
 
47
            return ln['chat']
 
48
 
 
49
class PythonRunner(Thread):
 
50
    def __init__(self, cmdQ, lineQ):
 
51
        self.cmdQ = cmdQ
 
52
        self.lineQ = lineQ
 
53
        self.out = cStringIO.StringIO()
 
54
        Thread.__init__(self)
 
55
 
 
56
    def execCmd(self, cmd):
 
57
        try:
 
58
            sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
 
59
            sys.stdout = self.out
 
60
            sys.stderr = self.out
 
61
            res = eval(cmd, self.globs, self.locls)
 
62
            self.cmdQ.put({"okay":(self.out.getvalue(),res)})
 
63
            self.curr_cmd = ''
 
64
            self.out = cStringIO.StringIO()
 
65
        except Exception, exc:
 
66
            self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
 
67
            self.curr_cmd = ''
 
68
            self.out = cStringIO.StringIO()
 
69
 
 
70
    def run(self):
 
71
        self.init_state()
 
72
        compiler = codeop.CommandCompiler()
 
73
 
 
74
        while True:
 
75
            ln = self.lineQ.get()
 
76
            if 'chat' in ln:
 
77
                if self.curr_cmd == '':
 
78
                    self.curr_cmd = ln['chat']
 
79
                else:
 
80
                    self.curr_cmd = self.curr_cmd + '\n' + ln['chat']
 
81
                try:
 
82
                    cmd = compiler(self.curr_cmd)
 
83
                    if cmd is None:
 
84
                        # The command was incomplete,
 
85
                        # so send back a None, so the
 
86
                        # client can print a '...'
 
87
                        self.cmdQ.put({"more":None})
 
88
                    else:
 
89
                        self.execCmd(cmd)
 
90
                except Exception, exc:
 
91
                    self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
 
92
                    self.curr_cmd = ''
 
93
                    self.out = cStringIO.StringIO()
 
94
            if 'block' in ln:
 
95
                # throw away a partial command.
 
96
                try:
 
97
                    cmd = compile(ln['block'], "<web session>", 'exec');
 
98
                    self.execCmd(cmd)
 
99
                except Exception, exc:
 
100
                    self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
 
101
                    self.curr_cmd = ''
 
102
                    self.out = cStringIO.StringIO()
 
103
 
 
104
    def init_state(self):
 
105
        self.globs = {}
 
106
        self.globs['__builtins__'] = globals()['__builtins__']
 
107
        self.locls = {}
 
108
        self.curr_cmd = ''
 
109
 
 
110
def daemonize():
 
111
    if os.fork():   # launch child and...
 
112
        os._exit(0) # kill off parent
 
113
    os.setsid()
 
114
    if os.fork():   # launch child and...
 
115
        os._exit(0) # kill off parent again.
 
116
    os.umask(077)
 
117
 
 
118
# The global 'magic' is the secret that the client and server share
 
119
# which is used to create and md5 digest to authenticate requests.
 
120
# It is assigned a real value at startup.
 
121
magic = ''
 
122
 
 
123
cmdQ = Queue.Queue()
 
124
lineQ = Queue.Queue()
 
125
interpThread = PythonRunner(cmdQ, lineQ)
 
126
 
 
127
# Default expiry time of 15 minutes
 
128
expiry = ExpiryTimer(15 * 60)
 
129
 
 
130
def initializer():
 
131
    interpThread.setDaemon(True)
 
132
    interpThread.start()
 
133
    expiry.ping()
 
134
 
 
135
def dispatch_msg(msg):
 
136
    expiry.ping()
 
137
    lineQ.put({msg['cmd']:msg['text']})
 
138
    return cmdQ.get()
 
139
 
 
140
if __name__ == "__main__":
 
141
    port = int(sys.argv[1])
 
142
    magic = sys.argv[2]
 
143
 
 
144
    common.chat.start_server(port, magic, True, dispatch_msg, initializer)