Package vqt :: Module qpython
[hide private]
[frames] | no frames]

Source Code for Module vqt.qpython

 1  '''
 
 2  Home of some helpers for python interactive stuff.
 
 3  ''' 
 4  import types 
 5  import traceback 
 6  
 
 7  from threading import Thread 
 8  from PyQt4 import QtCore, QtGui 
 9  
 
10  from vqt.main import idlethread 
11  from vqt.basics import * 
12 13 @idlethread 14 -def scripterr(msg, info):
15 msgbox = QtGui.QMessageBox() 16 msgbox.setText('Script Error: %s' % msg) 17 msgbox.setInformativeText(info) 18 msgbox.exec_()
19
20 -class ScriptThread(Thread):
21
22 - def __init__(self, cobj, locals):
23 Thread.__init__(self) 24 self.setDaemon(True) 25 self.cobj = cobj 26 self.locals = locals
27
28 - def run(self):
29 try: 30 exec(self.cobj, self.locals) 31 except Exception, e: 32 scripterr(str(e), traceback.format_exc())
33
34 -class VQPythonView(QtGui.QWidget):
35
36 - def __init__(self, locals=None, parent=None):
37 if locals == None: 38 locals = {} 39 40 self._locals = locals 41 42 QtGui.QWidget.__init__(self, parent=parent) 43 44 self._textWidget = QtGui.QTextEdit(parent=self) 45 self._botWidget = QtGui.QWidget(parent=self) 46 self._help_button = QtGui.QPushButton('?', parent=self._botWidget) 47 self._run_button = QtGui.QPushButton('Run', parent=self._botWidget) 48 self._run_button.clicked.connect(self._okClicked) 49 self._help_button.clicked.connect( self._helpClicked ) 50 51 self._help_text = None 52 53 hbox = HBox( None, self._help_button, self._run_button ) 54 self._botWidget.setLayout( hbox ) 55 56 vbox = VBox( self._textWidget, self._botWidget ) 57 self.setLayout( vbox ) 58 59 self.setWindowTitle('Python Interactive')
60
61 - def _okClicked(self):
62 pycode = str(self._textWidget.document().toPlainText()) 63 cobj = compile(pycode, "vqpython_exec.py", "exec") 64 sthr = ScriptThread(cobj, self._locals) 65 sthr.start()
66
67 - def _helpClicked(self):
68 withhelp = [] 69 for lname,lval in self._locals.items(): 70 if type(lval) in (types.ModuleType, ): 71 continue 72 doc = getattr(lval, '__doc__', '\nNo Documentation\n') 73 if doc == None: 74 doc = '\nNo Documentation\n' 75 withhelp.append( (lname, doc) ) 76 77 withhelp.sort() 78 79 txt = 'Objects/Functions in the namespace:\n' 80 for name,doc in withhelp: 81 txt += ( '====== %s\n' % name ) 82 txt += ( '%s\n' % doc ) 83 84 self._help_text = QtGui.QTextEdit() 85 self._help_text.setReadOnly( True ) 86 self._help_text.setWindowTitle('Python Interactive Help') 87 self._help_text.setText( txt ) 88 self._help_text.show()
89