|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2003 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a simple Python syntax checker. |
|
8 """ |
|
9 |
|
10 import sys |
|
11 import os |
|
12 import types |
|
13 |
|
14 from PyQt4.QtCore import * |
|
15 from PyQt4.QtGui import * |
|
16 |
|
17 from E4Gui.E4Application import e4App |
|
18 |
|
19 from Ui_SyntaxCheckerDialog import Ui_SyntaxCheckerDialog |
|
20 |
|
21 import Utilities |
|
22 |
|
23 class SyntaxCheckerDialog(QDialog, Ui_SyntaxCheckerDialog): |
|
24 """ |
|
25 Class implementing a dialog to display the results of a syntax check run. |
|
26 """ |
|
27 def __init__(self, parent = None): |
|
28 """ |
|
29 Constructor |
|
30 |
|
31 @param parent The parent widget. (QWidget) |
|
32 """ |
|
33 QDialog.__init__(self, parent) |
|
34 self.setupUi(self) |
|
35 |
|
36 self.showButton = self.buttonBox.addButton(\ |
|
37 self.trUtf8("Show"), QDialogButtonBox.ActionRole) |
|
38 self.showButton.setToolTip(\ |
|
39 self.trUtf8("Press to show all files containing a syntax error")) |
|
40 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False) |
|
41 self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True) |
|
42 |
|
43 self.resultList.headerItem().setText(self.resultList.columnCount(), "") |
|
44 self.resultList.header().setSortIndicator(0, Qt.AscendingOrder) |
|
45 |
|
46 self.noResults = True |
|
47 self.cancelled = False |
|
48 |
|
49 def __resort(self): |
|
50 """ |
|
51 Private method to resort the tree. |
|
52 """ |
|
53 self.resultList.sortItems(self.resultList.sortColumn(), |
|
54 self.resultList.header().sortIndicatorOrder()) |
|
55 |
|
56 def __createResultItem(self, file, line, error, sourcecode): |
|
57 """ |
|
58 Private method to create an entry in the result list. |
|
59 |
|
60 @param file filename of file (string) |
|
61 @param line linenumber of faulty source (integer or string) |
|
62 @param error error text (string) |
|
63 @param sourcecode faulty line of code (string) |
|
64 """ |
|
65 itm = QTreeWidgetItem(self.resultList, [file, str(line), error, sourcecode]) |
|
66 itm.setTextAlignment(1, Qt.AlignRight) |
|
67 |
|
68 def start(self, fn, codestring = ""): |
|
69 """ |
|
70 Public slot to start the syntax check. |
|
71 |
|
72 @param fn file or list of files or directory to be checked |
|
73 (string or list of strings) |
|
74 @param codestring string containing the code to be checked (string). |
|
75 If this is given, file must be a single file name. |
|
76 """ |
|
77 if type(fn) is types.ListType: |
|
78 files = fn |
|
79 elif os.path.isdir(fn): |
|
80 files = Utilities.direntries(fn, 1, '*.py', 0) |
|
81 files += Utilities.direntries(fn, 1, '*.pyw', 0) |
|
82 files += Utilities.direntries(fn, 1, '*.ptl', 0) |
|
83 else: |
|
84 files = [fn] |
|
85 files = [f for f in files \ |
|
86 if f.endswith(".py") or f.endswith(".pyw") or f.endswith(".ptl")] |
|
87 |
|
88 if (codestring and len(files) == 1) or \ |
|
89 (not codestring and len(files) > 0): |
|
90 self.checkProgress.setMaximum(len(files)) |
|
91 QApplication.processEvents() |
|
92 |
|
93 # now go through all the files |
|
94 progress = 0 |
|
95 for file in files: |
|
96 if self.cancelled: |
|
97 return |
|
98 |
|
99 nok, fname, line, code, error = Utilities.compile(file, codestring) |
|
100 if nok: |
|
101 self.noResults = False |
|
102 self.__createResultItem(fname, line, error, code) |
|
103 progress += 1 |
|
104 self.checkProgress.setValue(progress) |
|
105 QApplication.processEvents() |
|
106 self.__resort() |
|
107 else: |
|
108 self.checkProgress.setMaximum(1) |
|
109 self.checkProgress.setValue(1) |
|
110 self.__finish() |
|
111 |
|
112 def __finish(self): |
|
113 """ |
|
114 Private slot called when the syntax check finished or the user pressed the button. |
|
115 """ |
|
116 self.cancelled = True |
|
117 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(True) |
|
118 self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False) |
|
119 self.buttonBox.button(QDialogButtonBox.Close).setDefault(True) |
|
120 |
|
121 if self.noResults: |
|
122 self.__createResultItem(self.trUtf8('No syntax errors found.'), "", "", "") |
|
123 QApplication.processEvents() |
|
124 self.showButton.setEnabled(False) |
|
125 self.__clearErrors() |
|
126 else: |
|
127 self.showButton.setEnabled(True) |
|
128 self.resultList.header().resizeSections(QHeaderView.ResizeToContents) |
|
129 self.resultList.header().setStretchLastSection(True) |
|
130 |
|
131 def on_buttonBox_clicked(self, button): |
|
132 """ |
|
133 Private slot called by a button of the button box clicked. |
|
134 |
|
135 @param button button that was clicked (QAbstractButton) |
|
136 """ |
|
137 if button == self.buttonBox.button(QDialogButtonBox.Close): |
|
138 self.close() |
|
139 elif button == self.buttonBox.button(QDialogButtonBox.Cancel): |
|
140 self.__finish() |
|
141 elif button == self.showButton: |
|
142 self.on_showButton_clicked() |
|
143 |
|
144 def on_resultList_itemActivated(self, itm, col): |
|
145 """ |
|
146 Private slot to handle the activation of an item. |
|
147 |
|
148 @param itm reference to the activated item (QTreeWidgetItem) |
|
149 @param col column the item was activated in (integer) |
|
150 """ |
|
151 if self.noResults: |
|
152 return |
|
153 |
|
154 fn = Utilities.normabspath(itm.text(0)) |
|
155 lineno = int(itm.text(1)) |
|
156 error = itm.text(2) |
|
157 |
|
158 vm = e4App().getObject("ViewManager") |
|
159 vm.openSourceFile(fn, lineno) |
|
160 editor = vm.getOpenEditor(fn) |
|
161 editor.toggleSyntaxError(lineno, True, error) |
|
162 |
|
163 @pyqtSlot() |
|
164 def on_showButton_clicked(self): |
|
165 """ |
|
166 Private slot to handle the "Show" button press. |
|
167 """ |
|
168 for index in range(self.resultList.topLevelItemCount()): |
|
169 itm = self.resultList.topLevelItem(index) |
|
170 self.on_resultList_itemActivated(itm, 0) |
|
171 |
|
172 # go through the list again to clear syntax error markers |
|
173 # for files, that are ok |
|
174 vm = e4App().getObject("ViewManager") |
|
175 openFiles = vm.getOpenFilenames() |
|
176 errorFiles = [] |
|
177 for index in range(self.resultList.topLevelItemCount()): |
|
178 itm = self.resultList.topLevelItem(index) |
|
179 errorFiles.append(Utilities.normabspath(itm.text(0))) |
|
180 for file in openFiles: |
|
181 if not file in errorFiles: |
|
182 editor = vm.getOpenEditor(file) |
|
183 editor.clearSyntaxError() |
|
184 |
|
185 def __clearErrors(self): |
|
186 """ |
|
187 Private method to clear all error markers of open editors. |
|
188 """ |
|
189 vm = e4App().getObject("ViewManager") |
|
190 openFiles = vm.getOpenFilenames() |
|
191 for file in openFiles: |
|
192 editor = vm.getOpenEditor(file) |
|
193 editor.clearSyntaxError() |