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

« back to all changes in this revision

Viewing changes to services/python-console

  • Committer: William Grant
  • Date: 2010-07-28 04:12:08 UTC
  • mfrom: (1790.1.8 codemirror-srsly)
  • Revision ID: grantw@unimelb.edu.au-20100728041208-mciagtog1785oje4
Move from CodePress to CodeMirror. It's now an external dependency, too, so you'll need to install it yourself.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
import cPickle
9
9
import cStringIO
10
10
import md5
11
 
import os
12
11
import Queue
13
12
import signal
14
13
import socket
171
170
 
172
171
    def run(self):
173
172
        # Set up global space and partial command buffer
174
 
        self.globs = {}
 
173
        self.globs = {'__name__': '__main__'}
175
174
        self.curr_cmd = ''
176
175
 
177
176
        # Set up I/O to use web interface
181
180
 
182
181
        # Handlers for each action
183
182
        actions = {
 
183
            'splash': self.handle_splash,
184
184
            'chat': self.handle_chat,
185
185
            'block': self.handle_block,
186
186
            'globals': self.handle_globals,
198
198
                response = {'error': repr(e)}
199
199
            finally:
200
200
                self.cmdQ.put(response)
201
 
                   
 
201
 
 
202
    def handle_splash(self, params):
 
203
        # Initial console splash screen
 
204
        python_version = '.'.join(str(v) for v in sys.version_info[:3])
 
205
        splash_text = ("""IVLE %s Python Console (Python %s)
 
206
Type "help", "copyright", "credits" or "license" for more information.
 
207
""" % (ivle.__version__, python_version))
 
208
        return {'output': splash_text}
 
209
 
202
210
    def handle_chat(self, params):
203
211
        # Set up the partial cmd buffer
204
212
        if self.curr_cmd == '':
208
216
 
209
217
        # Try to execute the buffer
210
218
        try:
211
 
            cmd = self.cc(self.curr_cmd, '<web session>')
 
219
            # A single trailing newline simply indicates that the line is
 
220
            # finished. Two trailing newlines indicate the end of a block.
 
221
            # Unfortunately, codeop.CommandCompiler causes even one to
 
222
            # terminate a block.
 
223
            # Thus we need to remove a trailing newline from the command,
 
224
            # unless there are *two* trailing newlines, or multi-line indented
 
225
            # blocks are impossible. See Google Code issue 105.
 
226
            cmd_text = self.curr_cmd
 
227
            if cmd_text.endswith('\n') and not cmd_text.endswith('\n\n'):
 
228
                cmd_text = cmd_text[:-1]
 
229
            cmd = self.cc(cmd_text, '<web session>')
212
230
            if cmd is None:
213
231
                # The command was incomplete, so send back a None, so the              
214
232
                # client can print a '...'
244
262
    def handle_globals(self, params):
245
263
        # Unpickle the new space (if provided)
246
264
        if isinstance(params, dict):
247
 
            self.globs = {}
 
265
            self.globs = {'__name__': '__main__'}
248
266
            for g in params:
249
267
                try:
250
268
                    self.globs[g] = cPickle.loads(params[g])
359
377
    """Handles response from signals"""
360
378
    global terminate
361
379
    if signum == signal.SIGXCPU:
362
 
        terminate = "CPU Time Limit Exceeded"
 
380
        terminate = "CPU time limit exceeded"
363
381
 
364
382
def dispatch_msg(msg):
365
383
    global terminate
366
384
    if msg['cmd'] == 'terminate':
367
 
        terminate = "User requested console be terminated"
 
385
        terminate = "User requested restart"
368
386
    if terminate:
369
387
        raise ivle.chat.Terminate({"terminate":terminate})
370
388
    expiry.ping()
389
407
    for o in object:
390
408
        try:
391
409
            flat[o] = cPickle.dumps(object[o], PICKLEVERSION)
392
 
        except TypeError:
 
410
        except (TypeError, cPickle.PicklingError):
393
411
            try:
394
412
                o_type = type(object[o]).__name__
395
413
                o_name = object[o].__name__
402
420
if __name__ == "__main__":
403
421
    port = int(sys.argv[1])
404
422
    magic = sys.argv[2]
405
 
    
406
 
    # Sanitise the Enviroment
407
 
    os.environ = {}
408
 
    os.environ['PATH'] = '/usr/local/bin:/usr/bin:/bin'
409
 
 
410
 
    if len(sys.argv) >= 4:
411
 
        # working_dir
412
 
        os.chdir(sys.argv[3])
413
 
        os.environ['HOME'] = sys.argv[3]
414
423
 
415
424
    # Make python's search path follow the cwd
416
425
    sys.path[0] = ''