Plugins/PluginPep8Checker.py

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

eric ide

mercurial