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

« back to all changes in this revision

Viewing changes to scripts/python-console

  • Committer: dcoles
  • Date: 2008-08-13 07:26:44 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1017
Console: Refactored a lot of the console stuff into a class to make calls to 
the console from Python a lot nicer. Eventually it should even be able to be 
used by consoleservice but needs a little more work (at the moment it's only 
used for starting the console).
Also added in a few Tutorial specific functions to console such as 'inspect' 
which gives a summary of the the evaluated code block and flush to clear and 
optionally set globals.

Listmake will need to be rerun after this revision due to the new 
lib/common/console.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import common.chat
20
20
 
21
21
# This version must be supported by both the local and remote code
22
 
PICKLEVERSION = 2
 
22
PICKLEVERSION = 0
23
23
 
24
24
class Interrupt(Exception):
25
25
    def __init__(self):
194
194
                    self.cmdQ.put({"exc": ''.join(tb).decode('utf-8', 'replace')})
195
195
                    self.webio.flush()
196
196
                    self.curr_cmd = ''
197
 
            if 'block' in ln:
 
197
            elif 'block' in ln:
198
198
                # throw away a partial command.
199
199
                try:
200
200
                    cmd = compile(ln['block'], "<web session>", 'exec');
204
204
                    self.webio.flush()
205
205
                    self.cmdQ.put({"exc": ''.join(tb).decode('utf-8', 'replace')})
206
206
                    self.curr_cmd = ''
207
 
            if 'inspect' in ln:
 
207
            elif 'flush' in ln:
 
208
                # reset the globals
 
209
                self.globs = {}
 
210
                self.globs['__builtins__'] = globals()['__builtins__']
 
211
                self.cmdQ.put({'response': 'okay'})
 
212
                # Unpickle the new space (if provided)
 
213
                if isinstance(ln['flush'],dict):
 
214
                    for g in ln['flush']:
 
215
                        try:
 
216
                            self.globs[g] = cPickle.loads(ln['flush'][g])
 
217
                        except:
 
218
                            pass
 
219
            elif 'call' in ln:
 
220
                if isinstance(ln['call'], dict):
 
221
                    params = ln['call']
 
222
                    if 'args' in params:
 
223
                        args = params['args']
 
224
                    else:
 
225
                        args = []
 
226
                    if 'kwargs' in params:
 
227
                        kwargs = params['kwargs']
 
228
                    else:
 
229
                        kwargs = {}
 
230
 
 
231
                    try:
 
232
                        function = cPickle.loads(params['function'])
 
233
                        result = function(*args, **kwargs)
 
234
                        self.cmdQ.put({'output': result})
 
235
                    except Exception,e:
 
236
                        self.cmdQ.put({'response': 'failure: %s'%repr(e)})
 
237
                else:
 
238
                    self.cmdQ.put({'response': 'failure'})
 
239
            elif 'inspect' in ln:
208
240
                # Like block but return a serialization of the state
209
 
                # throw away a partial command and all state.
 
241
                # throw away partial command
210
242
                inspection = {}
211
 
                globs = {}
212
 
                globs['__builtins__'] = globals()['__builtins__']
213
 
                output_buffer = cStringIO.StringIO()
 
243
                stdout = cStringIO.StringIO()
 
244
                stderr = cStringIO.StringIO()
214
245
                try:
215
246
                    cmd = compile(ln['inspect'], "<web session>", 'exec');
216
 
                    #self.execCmd(cmd)
217
 
                    # Begin changes
218
247
                    sys.stdin = None
219
 
                    sys.stdout = output_buffer
220
 
                    sys.stderr = output_buffer
 
248
                    sys.stdout = stdout
 
249
                    sys.stderr = stderr
221
250
                    # We don't expect a return value - 'single' symbol prints 
222
251
                    # it.
223
 
                    eval(cmd, globs)
 
252
                    eval(cmd, self.globs)
224
253
                except Exception, e:
225
254
                    exception = {}
226
255
                    tb = format_exc_start(start=1)
230
259
                    inspection['exception'] = exception                
231
260
                
232
261
                # Write out the inspection object
233
 
                inspection['output'] = output_buffer.getvalue()
234
 
                inspection['globals'] = flatten(globs)
235
 
                self.cmdQ.put({"inspection": inspection})
236
 
                output_buffer.close()
 
262
                inspection['stdout'] = stdout.getvalue()
 
263
                inspection['stderr'] = stderr.getvalue()
 
264
                inspection['globals'] = flatten(self.globs)
 
265
                self.cmdQ.put(inspection)
 
266
                stdout.close()
 
267
                stderr.close()
237
268
                self.curr_cmd = ''
238
 
 
 
269
            else:
 
270
                raise Exception, "Invalid Command"
239
271
 
240
272
def daemonize():
241
273
    if os.fork():   # launch child and...