~ubuntu-branches/ubuntu/karmic/eric/karmic

« back to all changes in this revision

Viewing changes to eric/Scripting/ScriptManager.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2008-01-28 18:02:25 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080128180225-6nrox6yrworh2c4v
Tags: 4.0.4-1ubuntu1
* Add python-qt3 to build-depends becuase that's where Ubuntu puts 
  pyqtconfig
* Change maintainer to MOTU

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
#
5
5
 
6
6
"""
7
 
Module implementing the scripting component of the eric3 IDE.
 
7
Module implementing the scripting component of the eric4 IDE.
8
8
"""
9
9
 
10
10
#
15
15
import sys
16
16
import os
17
17
 
18
 
from qt import *
 
18
from PyQt4.QtCore import *
 
19
from PyQt4.QtGui import *
19
20
 
20
21
from KdeQt import KQInputDialog
21
22
 
23
24
    """
24
25
    Base class for all scripting related exceptions.
25
26
    """
26
 
    pass
27
 
    
 
27
    def __init__(self):
 
28
        """
 
29
        Constructor
 
30
        """
 
31
        self.errorMessage = 'Unspecified script error'
 
32
        
 
33
    def __repr__(self):
 
34
        """
 
35
        Private method returning a representation of the exception.
 
36
        
 
37
        @return string representing the error message
 
38
        """
 
39
        return unicode(self.errorMessage)
 
40
        
 
41
    def __str__(self):
 
42
        """
 
43
        Private method returning a string representation of the exception.
 
44
        
 
45
        @return string representing the error message
 
46
        """
 
47
        return str(self.errorMessage)
 
48
 
28
49
class NoSuchScriptError(ScriptError):
29
50
    """
30
51
    Class implementing an exception, which is raised, if a script couldn't be found.
33
54
        """
34
55
        Constructor
35
56
        """
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)
38
 
        
39
 
    def __repr__(self):
40
 
        """
41
 
        Private method returning a representation of the exception.
42
 
        
43
 
        @return string representing the error message
44
 
        """
45
 
        return unicode(self.errorMessage)
46
 
        
47
 
    def __str__(self):
48
 
        """
49
 
        Private method returning a string representation of the exception.
50
 
        
51
 
        @return string representing the error message
52
 
        """
53
 
        return str(self.errorMessage)
54
 
        
 
59
 
55
60
class CompilationError(ScriptError):
56
61
    """
57
62
    Class implementing an exception, which is raised, if a script couldn't be compiled.
60
65
        """
61
66
        Constructor
62
67
        """
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))
65
 
        
66
 
    def __repr__(self):
67
 
        """
68
 
        Private method returning a representation of the exception.
69
 
        
70
 
        @return string representing the error message
71
 
        """
72
 
        return unicode(self.errorMessage)
73
 
        
74
 
    def __str__(self):
75
 
        """
76
 
        Private method returning a string representation of the exception.
77
 
        
78
 
        @return string representing the error message
79
 
        """
80
 
        return str(self.errorMessage)
81
 
        
 
71
 
82
72
class ExecutionError(ScriptError):
83
73
    """
84
74
    Class implementing an exception, which is raised, if a script couldn't be executed.
87
77
        """
88
78
        Constructor
89
79
        """
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))
92
 
        
93
 
    def __repr__(self):
94
 
        """
95
 
        Private method returning a representation of the exception.
96
 
        
97
 
        @return string representing the error message
98
 
        """
99
 
        return unicode(self.errorMessage)
100
 
        
101
 
    def __str__(self):
102
 
        """
103
 
        Private method returning a string representation of the exception.
104
 
        
105
 
        @return string representing the error message
106
 
        """
107
 
        return str(self.errorMessage)
108
 
        
 
83
 
109
84
class ScriptAction(QAction):
110
85
    """
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.
115
90
    
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
119
94
    """
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)
132
107
        
133
 
    def activated(self):
134
 
        """
135
 
        Private method connected to the QAction activated signal.
136
 
        """
137
 
        self.emit(PYSIGNAL("activated"), (self,))
 
108
    def __triggered(self):
 
109
        """
 
110
        Private method connected to the QAction triggered signal.
 
111
        """
 
112
        self.emit(SIGNAL("executeScript"), self)
138
113
        
139
114
    def addTo(self, widget):
140
115
        """
141
116
        Public method to add this action to a widget.
142
117
        
143
 
        Overloaded from QAction in order to keep a list of widgets
144
 
        we are added to.
 
118
        This keeps a list of widgets we are added to.
145
119
        
146
120
        @param widget widget to add this action to (QWidget)
147
121
        """
148
 
        QAction.addTo(self, widget)
 
122
        widget.addAction(self)
149
123
        self.locations.append(widget)
150
124
        
151
125
    def removeFrom(self, widget):
152
126
        """
153
127
        Public method to remove this action from a widget.
154
128
        
155
 
        Overloaded from QAction in order to keep a list of widgets
156
 
        we are added to.
 
129
        This keeps a list of widgets we are added to.
157
130
        
158
131
        @param widget widget to remove this action from (QWidget)
159
132
        """
160
 
        QAction.removeFrom(self, widget)
 
133
        widget.removeAction(self)
161
134
        self.locations.remove(widget)
162
135
        
163
136
    def remove(self):
172
145
        """
173
146
        Private method to compile the code string.
174
147
        
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
178
151
        """
188
161
            return bytecode
189
162
        except Exception, e:
190
163
            raise CompilationError(e)
191
 
            
 
164
        
192
165
    def execute(self, out, err, globals, locals):
193
166
        """
194
167
        Public method to execute this script.
207
180
            sys.stdout = __stdout
208
181
            sys.stderr = __stderr
209
182
        except Exception, e:
210
 
            print e
211
 
            print sys.exc_info
212
183
            sys.stdout = __stdout
213
184
            sys.stderr = __stderr
214
185
            raise ExecutionError(e)
215
 
            
 
186
 
216
187
class ScriptManager(QObject):
217
188
    """
218
189
    Class implementing the script manager.
220
191
    @signal firstScriptAdded emitted after the first script was added
221
192
    @signal lastScriptDeleted emitted after the last script was deleted
222
193
    """
223
 
    def __init__(self, parent = None, g = None, l = None, *args):
 
194
    def __init__(self, parent = None, global_ = None, local_ = None):
224
195
        """
225
196
        Constructor
226
197
        
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
231
202
        """
232
 
        QObject.__init__(*(self, parent) + args)
 
203
        QObject.__init__(self, parent)
233
204
        
234
205
        self.scriptObjects = {}
235
206
        
236
 
        if g is None:
 
207
        if global_ is None:
237
208
            self.globals = globals()
238
209
        else:
239
 
            self.globals = g
 
210
            self.globals = global_
240
211
            
241
 
        if l is None:
 
212
        if local_ is None:
242
213
            self.locals = locals()
243
214
        else:
244
 
            self.locals = l
245
 
            
 
215
            self.locals = local_
 
216
        
246
217
    def deleteScript(self, scriptName):
247
218
        """
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"))
259
230
        
260
231
    def addScript(self, scriptName, scriptString):
261
232
        """
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)
276
247
        return action
277
248
        
278
249
    def executeAction(self, action):
295
266
        if not self.scriptObjects.has_key(unicode(scriptName)):
296
267
            raise NoSuchScriptError(scriptName)
297
268
        else:
298
 
            self.scriptObjects[unicode(scriptName)].execute(out, err, self.globals, self.locals)
299
 
            
 
269
            self.scriptObjects[unicode(scriptName)].execute(\
 
270
                out, err, self.globals, self.locals)
 
271
        
300
272
    def getScriptNames(self):
301
273
        """
302
274
        Public method to retrieve the names of all scripts.
307
279
        
308
280
    def getScriptName(self):
309
281
        """
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.
311
283
        
312
284
        @return Tuple of script name and a flag, indicating, if the user pressed ok or
313
285
            canceled the operation. (QString, boolean)
317
289
            qs.append(s)
318
290
        qs.sort()
319
291
        return KQInputDialog.getItem(\
 
292
            self.parent(),
320
293
            self.trUtf8("Script Name"),
321
294
            self.trUtf8("Select a script name:"),
322
 
            qs, 0, 0, self.parent())
323
 
        
 
295
            qs,
 
296
            0, False)