24
25
Base class for all scripting related exceptions.
31
self.errorMessage = 'Unspecified script error'
35
Private method returning a representation of the exception.
37
@return string representing the error message
39
return unicode(self.errorMessage)
43
Private method returning a string representation of the exception.
45
@return string representing the error message
47
return str(self.errorMessage)
28
49
class NoSuchScriptError(ScriptError):
30
51
Class implementing an exception, which is raised, if a script couldn't be found.
36
ERR = qApp.translate("ScriptError", "Script %1 is not installed")
57
ERR = QApplication.translate("ScriptError", "Script %1 is not installed")
37
58
self.errorMessage = ERR.arg(script)
41
Private method returning a representation of the exception.
43
@return string representing the error message
45
return unicode(self.errorMessage)
49
Private method returning a string representation of the exception.
51
@return string representing the error message
53
return str(self.errorMessage)
55
60
class CompilationError(ScriptError):
57
62
Class implementing an exception, which is raised, if a script couldn't be compiled.
63
ERR = qApp.translate("ScriptError", "Script could not be compiled. Reason: %1")
68
ERR = QApplication.translate("ScriptError",
69
"Script could not be compiled. Reason: %1")
64
70
self.errorMessage = ERR.arg(unicode(error))
68
Private method returning a representation of the exception.
70
@return string representing the error message
72
return unicode(self.errorMessage)
76
Private method returning a string representation of the exception.
78
@return string representing the error message
80
return str(self.errorMessage)
82
72
class ExecutionError(ScriptError):
84
74
Class implementing an exception, which is raised, if a script couldn't be executed.
90
ERR = qApp.translate("ScriptError", "Script could not be executed. Reason: %1")
80
ERR = QApplication.translate("ScriptError",
81
"Script could not be executed. Reason: %1")
91
82
self.errorMessage = ERR.arg(unicode(error))
95
Private method returning a representation of the exception.
97
@return string representing the error message
99
return unicode(self.errorMessage)
103
Private method returning a string representation of the exception.
105
@return string representing the error message
107
return str(self.errorMessage)
109
84
class ScriptAction(QAction):
111
86
Class implementing the script action.
113
88
This class is subclassed from QAction to have the possibility to
114
89
attach it to menus and toolbars.
116
@signal activated signal emitted when this script action is activated
91
@signal executeScript signal emitted when this script action is triggered
117
92
@exception CompilationError raised, if script compilation fails
118
93
@exception ExecutionError raised, if script execution failed
128
103
self.code = unicode(code)
129
104
self.bytecode = self.__compile(self.code)
130
105
self.locations = []
131
self.connect(self, SIGNAL("activated()"), self.activated)
106
self.connect(self, SIGNAL("triggered()"), self.__triggered)
135
Private method connected to the QAction activated signal.
137
self.emit(PYSIGNAL("activated"), (self,))
108
def __triggered(self):
110
Private method connected to the QAction triggered signal.
112
self.emit(SIGNAL("executeScript"), self)
139
114
def addTo(self, widget):
141
116
Public method to add this action to a widget.
143
Overloaded from QAction in order to keep a list of widgets
118
This keeps a list of widgets we are added to.
146
120
@param widget widget to add this action to (QWidget)
148
QAction.addTo(self, widget)
122
widget.addAction(self)
149
123
self.locations.append(widget)
151
125
def removeFrom(self, widget):
153
127
Public method to remove this action from a widget.
155
Overloaded from QAction in order to keep a list of widgets
129
This keeps a list of widgets we are added to.
158
131
@param widget widget to remove this action from (QWidget)
160
QAction.removeFrom(self, widget)
133
widget.removeAction(self)
161
134
self.locations.remove(widget)
163
136
def remove(self):
173
146
Private method to compile the code string.
175
@param ode string containing the code (string)
148
@param code string containing the code (string)
176
149
@exception CompilationError raised, if compilation fails
177
150
@return the compiled bytecode as a string
189
162
except Exception, e:
190
163
raise CompilationError(e)
192
165
def execute(self, out, err, globals, locals):
194
167
Public method to execute this script.
220
191
@signal firstScriptAdded emitted after the first script was added
221
192
@signal lastScriptDeleted emitted after the last script was deleted
223
def __init__(self, parent = None, g = None, l = None, *args):
194
def __init__(self, parent = None, global_ = None, local_ = None):
227
198
@param parent parent of this scriptmanager (QObject)
228
@param g dictionary for global scope
229
@param l dictionary for local scope
199
@param global_ dictionary for global scope
200
@param local_ dictionary for local scope
230
201
@param *args arguments passed on to QObject
232
QObject.__init__(*(self, parent) + args)
203
QObject.__init__(self, parent)
234
205
self.scriptObjects = {}
237
208
self.globals = globals()
210
self.globals = global_
242
213
self.locals = locals()
246
217
def deleteScript(self, scriptName):
248
219
Public method to delete a script.
255
226
self.scriptObjects[unicode(scriptName)].remove()
256
227
del self.scriptObjects[unicode(scriptName)]
257
228
if not self.scriptObjects:
258
self.emit(PYSIGNAL("lastScriptDeleted"), ())
229
self.emit(SIGNAL("lastScriptDeleted"))
260
231
def addScript(self, scriptName, scriptString):
270
241
# remove a previously loaded script with same name
271
242
self.deleteScript(scriptName)
272
243
if not self.scriptObjects:
273
self.emit(PYSIGNAL("firstScriptAdded"), ())
244
self.emit(SIGNAL("firstScriptAdded"))
274
245
self.scriptObjects[unicode(scriptName)] = action
275
self.connect(action, PYSIGNAL("activated"), self.executeAction)
246
self.connect(action, SIGNAL("executeScript"), self.executeAction)
278
249
def executeAction(self, action):
295
266
if not self.scriptObjects.has_key(unicode(scriptName)):
296
267
raise NoSuchScriptError(scriptName)
298
self.scriptObjects[unicode(scriptName)].execute(out, err, self.globals, self.locals)
269
self.scriptObjects[unicode(scriptName)].execute(\
270
out, err, self.globals, self.locals)
300
272
def getScriptNames(self):
302
274
Public method to retrieve the names of all scripts.
308
280
def getScriptName(self):
310
Private method to select a script name from the list of scripts.
282
Public method to select a script name from the list of scripts.
312
284
@return Tuple of script name and a flag, indicating, if the user pressed ok or
313
285
canceled the operation. (QString, boolean)