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

« back to all changes in this revision

Viewing changes to scripts/python-console

  • Committer: dcoles
  • Date: 2008-08-20 08:10:40 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1034
Console: Refactored the code to support supplying of stdin to the console.  
Renamed and unified a few call in the module - inspect is now execute since 
stdout is written to the console.stdout file-like object rather than being 
returned as part of the call. __chat becomes split into __chat which deals with 
the low level chat protocol issues and __handle_chat which deals with console's 
own protocol.

Updated tutorial service to use these new calls. Still have to fix the 
'add_stdin' function but should be very simple now. (Just write to 
console.stdin)

Show diffs side-by-side

added added

removed removed

Lines of Context:
171
171
 
172
172
    def run(self):
173
173
        self.globs = {}
174
 
        self.globs['__builtins__'] = globals()['__builtins__']
175
174
        self.curr_cmd = ''
176
175
 
177
176
        while True:
205
204
                    self.webio.flush()
206
205
                    self.cmdQ.put({"exc": ''.join(tb).decode('utf-8', 'replace')})
207
206
                    self.curr_cmd = ''
208
 
            elif 'flush' in ln:
209
 
                # reset the globals
210
 
                self.globs = {}
211
 
                self.globs['__builtins__'] = globals()['__builtins__']
212
 
                self.cmdQ.put({'response': 'okay'})
 
207
            elif 'globals' in ln:
213
208
                # Unpickle the new space (if provided)
214
 
                if isinstance(ln['flush'],dict):
215
 
                    for g in ln['flush']:
 
209
                if isinstance(ln['globals'],dict):
 
210
                    self.globs = {}
 
211
                    for g in ln['globals']:
216
212
                        try:
217
 
                            self.globs[g] = cPickle.loads(ln['flush'][g])
 
213
                            self.globs[g] = cPickle.loads(ln['globals'][g])
218
214
                        except:
219
215
                            pass
 
216
 
 
217
                # Return the current globals
 
218
                self.cmdQ.put({'globals': flatten(self.globs)})
220
219
            elif 'call' in ln:
221
220
                call = {}
222
 
                stdout = cStringIO.StringIO()
223
 
                stderr = cStringIO.StringIO()
224
 
                sys.stdout = stdout
225
 
                sys.stderr = stderr
 
221
                sys.stdin = self.webio
 
222
                sys.stdout = self.webio
 
223
                sys.stderr = self.webio
226
224
 
227
225
                if isinstance(ln['call'], dict):
228
226
                    params = ln['call']
258
256
                            {"exc": ''.join(tb).decode('utf-8', 'replace')})
259
257
                    
260
258
                    # Write out the inspection object
261
 
                    call['stdout'] = stdout.getvalue()
262
 
                    call['stderr'] = stderr.getvalue()
263
259
                    self.cmdQ.put(call)
264
260
                else:
265
261
                    self.cmdQ.put({'response': 'failure'})
266
 
                stdout.close()
267
 
                stderr.close()
268
262
                self.curr_cmd = ''
269
 
            elif 'inspect' in ln:
 
263
            elif 'execute' in ln:
270
264
                # Like block but return a serialization of the state
271
265
                # throw away partial command
272
 
                inspection = {}
273
 
                stdout = cStringIO.StringIO()
274
 
                stderr = cStringIO.StringIO()
 
266
                response = {'okay': None}
 
267
                sys.stdin = self.webio
 
268
                sys.stdout = self.webio
 
269
                sys.stderr = self.webio
275
270
                try:
276
 
                    cmd = compile(ln['inspect'], "<web session>", 'exec');
277
 
                    sys.stdin = None
278
 
                    sys.stdout = stdout
279
 
                    sys.stderr = stderr
 
271
                    cmd = compile(ln['execute'], "<web session>", 'exec');
280
272
                    # We don't expect a return value - 'single' symbol prints 
281
273
                    # it.
282
274
                    self.eval(cmd)
283
275
                except Exception, e:
284
 
                    exception = {}
285
 
                    tb = format_exc_start(start=1)
286
 
                    exception['traceback'] = \
287
 
                        ''.join(tb).decode('utf-8', 'replace')
288
 
                    exception['except'] = cPickle.dumps(e, PICKLEVERSION)
289
 
                    inspection['exception'] = exception                
 
276
                    response = {'exception': cPickle.dumps(e, PICKLEVERSION)}
 
277
               
 
278
                # Flush the files
 
279
                sys.stderr.flush()
 
280
                sys.stdout.flush()
290
281
                
291
 
                # Write out the inspection object
292
 
                inspection['stdout'] = stdout.getvalue()
293
 
                inspection['stderr'] = stderr.getvalue()
294
 
                inspection['globals'] = flatten(self.globs)
295
 
                self.cmdQ.put(inspection)
296
 
                stdout.close()
297
 
                stderr.close()
 
282
                # Return the inspection object
 
283
                self.cmdQ.put(response)
 
284
 
 
285
                # Clear any previous command
298
286
                self.curr_cmd = ''
299
287
            elif 'set_vars' in ln:
300
288
                # Adds some variables to the global dictionary
306
294
                        self.cmdQ.put(
307
295
                            {"exc": ''.join(tb).decode('utf-8', 'replace')})
308
296
 
309
 
                self.cmdQ.put({'response': 'okay'})
 
297
                self.cmdQ.put({'okay': None})
310
298
            else:
311
299
                raise Exception, "Invalid Command"
312
300
 
349
337
 
350
338
def dispatch_msg(msg):
351
339
    global terminate
352
 
    if msg['cmd'] == 'restart':
353
 
        terminate = "User requested console be reset"
 
340
    if msg['cmd'] == 'terminate':
 
341
        terminate = "User requested console be terminated"
354
342
    if terminate:
355
 
        raise common.chat.Terminate({"restart":terminate})
 
343
        raise common.chat.Terminate({"terminate":terminate})
356
344
    expiry.ping()
357
345
    lineQ.put({msg['cmd']:msg['text']})
358
346
    if terminate:
359
 
        raise common.chat.Terminate({"restart":terminate})
 
347
        raise common.chat.Terminate({"terminate":terminate})
360
348
    return cmdQ.get()
361
349
 
362
350
def format_exc_start(start=0):