Plugins/PluginSyntaxChecker.py

changeset 0
de9c2efb9d02
child 6
52e8c820d0dd
equal deleted inserted replaced
-1:000000000000 0:de9c2efb9d02
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2007 - 2009 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the Tabnanny plugin.
8 """
9
10 import os
11
12 from PyQt4.QtCore import QObject, SIGNAL
13
14 from E4Gui.E4Application import e4App
15
16 from E4Gui.E4Action import E4Action
17
18 from CheckerPlugins.SyntaxChecker.SyntaxCheckerDialog import SyntaxCheckerDialog
19
20 # Start-Of-Header
21 name = "Syntax Checker Plugin"
22 author = "Detlev Offenbach <detlev@die-offenbachs.de>"
23 autoactivate = True
24 deactivateable = True
25 version = "5.0.0"
26 className = "SyntaxCheckerPlugin"
27 packageName = "__core__"
28 shortDescription = "Show the Syntax Checker dialog."
29 longDescription = """This plugin implements the Syntax Checker dialog.""" \
30 """ Syntax Checker is used to check Python source files for correct syntax."""
31 pyqtApi = 2
32 # End-Of-Header
33
34 error = ""
35
36 class SyntaxCheckerPlugin(QObject):
37 """
38 Class implementing the Syntax Checker plugin.
39 """
40 def __init__(self, ui):
41 """
42 Constructor
43
44 @param ui reference to the user interface object (UI.UserInterface)
45 """
46 QObject.__init__(self, ui)
47 self.__ui = ui
48 self.__initialize()
49
50 def __initialize(self):
51 """
52 Private slot to (re)initialize the plugin.
53 """
54 self.__projectAct = None
55 self.__projectSyntaxCheckerDialog = None
56
57 self.__projectBrowserAct = None
58 self.__projectBrowserMenu = None
59 self.__projectBrowserSyntaxCheckerDialog = None
60
61 self.__editors = []
62 self.__editorAct = None
63 self.__editorSyntaxCheckerDialog = None
64
65 def activate(self):
66 """
67 Public method to activate this plugin.
68
69 @return tuple of None and activation status (boolean)
70 """
71 menu = e4App().getObject("Project").getMenu("Checks")
72 if menu:
73 self.__projectAct = E4Action(self.trUtf8('Check Syntax'),
74 self.trUtf8('&Syntax...'), 0, 0,
75 self, 'project_check_syntax')
76 self.__projectAct.setStatusTip(\
77 self.trUtf8('Check syntax.'))
78 self.__projectAct.setWhatsThis(self.trUtf8(
79 """<b>Check Syntax...</b>"""
80 """<p>This checks Python files for syntax errors.</p>"""
81 ))
82 self.connect(self.__projectAct, SIGNAL('triggered()'),
83 self.__projectSyntaxCheck)
84 e4App().getObject("Project").addE4Actions([self.__projectAct])
85 menu.addAction(self.__projectAct)
86
87 self.__editorAct = E4Action(self.trUtf8('Check Syntax'),
88 self.trUtf8('&Syntax...'), 0, 0,
89 self, "")
90 self.__editorAct.setWhatsThis(self.trUtf8(
91 """<b>Check Syntax...</b>"""
92 """<p>This checks Python files for syntax errors.</p>"""
93 ))
94 self.connect(self.__editorAct, SIGNAL('triggered()'), self.__editorSyntaxCheck)
95
96 self.connect(e4App().getObject("Project"), SIGNAL("showMenu"),
97 self.__projectShowMenu)
98 self.connect(e4App().getObject("ProjectBrowser").getProjectBrowser("sources"),
99 SIGNAL("showMenu"), self.__projectBrowserShowMenu)
100 self.connect(e4App().getObject("ViewManager"), SIGNAL("editorOpenedEd"),
101 self.__editorOpened)
102 self.connect(e4App().getObject("ViewManager"), SIGNAL("editorClosedEd"),
103 self.__editorClosed)
104
105 for editor in e4App().getObject("ViewManager").getOpenEditors():
106 self.__editorOpened(editor)
107
108 return None, True
109
110 def deactivate(self):
111 """
112 Public method to deactivate this plugin.
113 """
114 self.disconnect(e4App().getObject("Project"), SIGNAL("showMenu"),
115 self.__projectShowMenu)
116 self.disconnect(e4App().getObject("ProjectBrowser").getProjectBrowser("sources"),
117 SIGNAL("showMenu"), self.__projectBrowserShowMenu)
118 self.disconnect(e4App().getObject("ViewManager"), SIGNAL("editorOpenedEd"),
119 self.__editorOpened)
120 self.disconnect(e4App().getObject("ViewManager"), SIGNAL("editorClosedEd"),
121 self.__editorClosed)
122
123 menu = e4App().getObject("Project").getMenu("Checks")
124 if menu:
125 menu.removeAction(self.__projectAct)
126
127 if self.__projectBrowserMenu:
128 if self.__projectBrowserAct:
129 self.__projectBrowserMenu.removeAction(self.__projectBrowserAct)
130
131 for editor in self.__editors:
132 self.disconnect(editor, SIGNAL("showMenu"), self.__editorShowMenu)
133 menu = editor.getMenu("Checks")
134 if menu is not None:
135 menu.removeAction(self.__editorAct)
136
137 self.__initialize()
138
139 def __projectShowMenu(self, menuName, menu):
140 """
141 Private slot called, when the the project menu or a submenu is
142 about to be shown.
143
144 @param menuName name of the menu to be shown (string)
145 @param menu reference to the menu (QMenu)
146 """
147 if menuName == "Checks" and self.__projectAct is not None:
148 self.__projectAct.setEnabled(\
149 e4App().getObject("Project").getProjectLanguage() == "Python")
150
151 def __projectBrowserShowMenu(self, menuName, menu):
152 """
153 Private slot called, when the the project browser menu or a submenu is
154 about to be shown.
155
156 @param menuName name of the menu to be shown (string)
157 @param menu reference to the menu (QMenu)
158 """
159 if menuName == "Checks" and \
160 e4App().getObject("Project").getProjectLanguage() == "Python":
161 self.__projectBrowserMenu = menu
162 if self.__projectBrowserAct is None:
163 self.__projectBrowserAct = E4Action(self.trUtf8('Check Syntax'),
164 self.trUtf8('&Syntax...'), 0, 0,
165 self, "")
166 self.__projectBrowserAct.setWhatsThis(self.trUtf8(
167 """<b>Check Syntax...</b>"""
168 """<p>This checks Python files for syntax errors.</p>"""
169 ))
170 self.connect(self.__projectBrowserAct, SIGNAL('triggered()'),
171 self.__projectBrowserSyntaxCheck)
172 if not self.__projectBrowserAct in menu.actions():
173 menu.addAction(self.__projectBrowserAct)
174
175 def __projectSyntaxCheck(self):
176 """
177 Public slot used to check the project files for bad indentations.
178 """
179 project = e4App().getObject("Project")
180 project.saveAllScripts()
181 files = [os.path.join(project.ppath, file) \
182 for file in project.pdata["SOURCES"] \
183 if file.endswith(".py") or \
184 file.endswith(".pyw") or \
185 file.endswith(".ptl")]
186
187 self.__projectSyntaxCheckerDialog = SyntaxCheckerDialog()
188 self.__projectSyntaxCheckerDialog.show()
189 self.__projectSyntaxCheckerDialog.start(files)
190
191 def __projectBrowserSyntaxCheck(self):
192 """
193 Private method to handle the syntax check context menu action of the project
194 sources browser.
195 """
196 browser = e4App().getObject("ProjectBrowser").getProjectBrowser("sources")
197 itm = browser.model().item(browser.currentIndex())
198 try:
199 fn = itm.fileName()
200 except AttributeError:
201 fn = itm.dirName()
202
203 self.__projectBrowserSyntaxCheckerDialog = SyntaxCheckerDialog()
204 self.__projectBrowserSyntaxCheckerDialog.show()
205 self.__projectBrowserSyntaxCheckerDialog.start(fn)
206
207 def __editorOpened(self, editor):
208 """
209 Private slot called, when a new editor was opened.
210
211 @param editor reference to the new editor (QScintilla.Editor)
212 """
213 menu = editor.getMenu("Checks")
214 if menu is not None:
215 menu.addAction(self.__editorAct)
216 self.connect(editor, SIGNAL("showMenu"), self.__editorShowMenu)
217 self.__editors.append(editor)
218
219 def __editorClosed(self, editor):
220 """
221 Private slot called, when an editor was closed.
222
223 @param editor reference to the editor (QScintilla.Editor)
224 """
225 try:
226 self.__editors.remove(editor)
227 except ValueError:
228 pass
229
230 def __editorShowMenu(self, menuName, menu, editor):
231 """
232 Private slot called, when the the editor context menu or a submenu is
233 about to be shown.
234
235 @param menuName name of the menu to be shown (string)
236 @param menu reference to the menu (QMenu)
237 @param editor reference to the editor
238 """
239 if menuName == "Checks":
240 if not self.__editorAct in menu.actions():
241 menu.addAction(self.__editorAct)
242 self.__editorAct.setEnabled(editor.isPyFile())
243
244 def __editorSyntaxCheck(self):
245 """
246 Private slot to handle the syntax check context menu action of the editors.
247 """
248 editor = e4App().getObject("ViewManager").activeWindow()
249 if editor is not None:
250 self.__editorSyntaxCheckerDialog = SyntaxCheckerDialog()
251 self.__editorSyntaxCheckerDialog.show()
252 self.__editorSyntaxCheckerDialog.start(editor.getFileName(),
253 unicode(editor.text()))

eric ide

mercurial