156
158
def execCmd(self, cmd):
158
sys.stdin = self.webio
159
sys.stdout = self.webio
160
sys.stderr = self.webio
161
160
# We don't expect a return value - 'single' symbol prints it.
163
163
self.webio.flush()
164
self.cmdQ.put({"okay": None})
164
return({"okay": None})
167
tb = format_exc_start(start=1)
168
167
self.webio.flush()
169
self.cmdQ.put({"exc": ''.join(tb).decode('utf-8', 'replace')})
168
tb = format_exc_start(start=2)
169
return({"exc": ''.join(tb).decode('utf-8', 'replace')})
172
# Set up global space and partial command buffer
174
174
self.curr_cmd = ''
176
# Set up I/O to use web interface
177
sys.stdin = self.webio
178
sys.stdout = self.webio
179
sys.stderr = self.webio
181
# Handlers for each action
183
'chat': self.handle_chat,
184
'block': self.handle_block,
185
'globals': self.handle_globals,
186
'call': self.handle_call,
187
'execute': self.handle_execute,
188
'setvars': self.handle_setvars,
191
# Run the processing loop
177
ln = self.lineQ.get()
179
if self.curr_cmd == '':
180
self.curr_cmd = ln['chat']
182
self.curr_cmd = self.curr_cmd + '\n' + ln['chat']
184
cmd = codeop.compile_command(self.curr_cmd, '<web session>')
186
# The command was incomplete,
187
# so send back a None, so the
188
# client can print a '...'
189
self.cmdQ.put({"more":None})
193
tb = format_exc_start(start=3)
194
self.cmdQ.put({"exc": ''.join(tb).decode('utf-8', 'replace')})
198
# throw away a partial command.
200
cmd = compile(ln['block'], "<web session>", 'exec');
193
action, params = self.lineQ.get()
195
response = actions[action](params)
197
response = {'error': repr(e)}
199
self.cmdQ.put(response)
201
def handle_chat(self, params):
202
# Set up the partial cmd buffer
203
if self.curr_cmd == '':
204
self.curr_cmd = params
206
self.curr_cmd = self.curr_cmd + '\n' + params
208
# Try to execute the buffer
210
cmd = codeop.compile_command(self.curr_cmd, '<web session>')
212
# The command was incomplete, so send back a None, so the
213
# client can print a '...'
214
return({"more":None})
216
return(self.execCmd(cmd))
218
# Clear any partial command
220
# Flush the output buffers
223
# Return the exception
224
tb = format_exc_start(start=3)
225
return({"exc": ''.join(tb).decode('utf-8', 'replace')})
227
def handle_block(self, params):
228
# throw away any partial command.
231
# Try to execute a complete block of code
233
cmd = compile(params, "<web session>", 'exec');
234
return(self.execCmd(cmd))
236
# Flush the output buffers
239
# Return the exception
240
tb = format_exc_start(start=1)
241
return({"exc": ''.join(tb).decode('utf-8', 'replace')})
243
def handle_globals(self, params):
244
# Unpickle the new space (if provided)
245
if isinstance(params, dict):
249
self.globs[g] = cPickle.loads(params[g])
253
# Return the current globals
254
return({'globals': flatten(self.globs)})
256
def handle_call(self, params):
259
# throw away any partial command.
262
if isinstance(params, dict):
265
if isinstance(params['args'], list):
266
args = map(self.eval, params['args'])
269
if isinstance(params['kwargs'], dict):
271
for kwarg in params['kwargs']:
272
kwargs[kwarg] = self.eval(
273
params['kwargs'][kwarg])
278
function = self.eval(params['function'])
280
call['result'] = function(*args, **kwargs)
203
283
tb = format_exc_start(start=1)
205
self.cmdQ.put({"exc": ''.join(tb).decode('utf-8', 'replace')})
207
elif 'globals' in ln:
208
# Unpickle the new space (if provided)
209
if isinstance(ln['globals'],dict):
211
for g in ln['globals']:
213
self.globs[g] = cPickle.loads(ln['globals'][g])
217
# Return the current globals
218
self.cmdQ.put({'globals': flatten(self.globs)})
221
sys.stdin = self.webio
222
sys.stdout = self.webio
223
sys.stderr = self.webio
225
if isinstance(ln['call'], dict):
229
if isinstance(params['args'], list):
230
args = map(self.eval, params['args'])
233
if isinstance(params['kwargs'], dict):
235
for kwarg in params['kwargs']:
236
kwargs[kwarg] = self.eval(
237
params['kwargs'][kwarg])
242
function = self.eval(params['function'])
244
call['result'] = function(*args, **kwargs)
247
tb = format_exc_start(start=1)
248
exception['traceback'] = \
249
''.join(tb).decode('utf-8', 'replace')
250
exception['except'] = cPickle.dumps(e,
252
call['exception'] = exception
254
tb = format_exc_start(start=1)
256
{"exc": ''.join(tb).decode('utf-8', 'replace')})
258
# Write out the inspection object
261
self.cmdQ.put({'response': 'failure'})
263
elif 'execute' in ln:
264
# Like block but return a serialization of the state
265
# throw away partial command
266
response = {'okay': None}
267
sys.stdin = self.webio
268
sys.stdout = self.webio
269
sys.stderr = self.webio
271
cmd = compile(ln['execute'], "<web session>", 'exec');
272
# We don't expect a return value - 'single' symbol prints
276
response = {'exception': cPickle.dumps(e, PICKLEVERSION)}
284
exception['traceback'] = \
285
''.join(tb).decode('utf-8', 'replace')
286
exception['except'] = cPickle.dumps(e,
288
call['exception'] = exception
290
tb = format_exc_start(start=1)
291
call = {"exc": ''.join(tb).decode('utf-8', 'replace')}
293
# Flush the output buffers
297
# Write out the inspection object
300
return({'response': 'failure'})
302
def handle_execute(self, params):
303
# throw away any partial command.
306
# Like block but return a serialization of the state
307
# throw away partial command
308
response = {'okay': None}
310
cmd = compile(params, "<web session>", 'exec');
311
# We don't expect a return value - 'single' symbol prints it.
314
response = {'exception': cPickle.dumps(e, PICKLEVERSION)}
282
# Return the inspection object
283
self.cmdQ.put(response)
285
# Clear any previous command
287
elif 'set_vars' in ln:
288
# Adds some variables to the global dictionary
289
for var in ln['set_vars']:
291
self.globs[var] = self.eval(ln['set_vars'][var])
293
tb = format_exc_start(start=1)
295
{"exc": ''.join(tb).decode('utf-8', 'replace')})
297
self.cmdQ.put({'okay': None})
299
raise Exception, "Invalid Command"
320
# Return the inspection object
323
def handle_setvars(self, params):
324
# Adds some variables to the global dictionary
325
for var in params['set_vars']:
327
self.globs[var] = self.eval(params['set_vars'][var])
329
tb = format_exc_start(start=1)
330
return({"exc": ''.join(tb).decode('utf-8', 'replace')})
332
return({'okay': None})
301
334
def eval(self, source):
302
335
""" Evaluates a string in the private global space """