10 import os |
10 import os |
11 import fnmatch |
11 import fnmatch |
12 |
12 |
13 from PyQt4.QtCore import pyqtSlot, Qt |
13 from PyQt4.QtCore import pyqtSlot, Qt |
14 from PyQt4.QtGui import QDialog, QTreeWidgetItem, QAbstractButton, \ |
14 from PyQt4.QtGui import QDialog, QTreeWidgetItem, QAbstractButton, \ |
15 QDialogButtonBox, QApplication, QHeaderView |
15 QDialogButtonBox, QApplication, QHeaderView, QIcon |
16 |
16 |
17 from . import pep8 |
17 from . import pep8 |
18 |
18 |
19 from E5Gui.E5Application import e5App |
19 from E5Gui.E5Application import e5App |
20 |
20 |
113 """ |
113 """ |
114 self.resultList.sortItems(self.resultList.sortColumn(), |
114 self.resultList.sortItems(self.resultList.sortColumn(), |
115 self.resultList.header().sortIndicatorOrder() |
115 self.resultList.header().sortIndicatorOrder() |
116 ) |
116 ) |
117 |
117 |
118 def __createResultItem(self, file, line, pos, message, fixed): |
118 def __createResultItem(self, file, line, pos, message, fixed, autofixing): |
119 """ |
119 """ |
120 Private method to create an entry in the result list. |
120 Private method to create an entry in the result list. |
121 |
121 |
122 @param file file name of the file (string) |
122 @param file file name of the file (string) |
123 @param line line number of issue (integer or string) |
123 @param line line number of issue (integer or string) |
124 @param pos character position of issue (integer or string) |
124 @param pos character position of issue (integer or string) |
125 @param message message text (string) |
125 @param message message text (string) |
126 @param fixed flag indicating a fixed issue (boolean) |
126 @param fixed flag indicating a fixed issue (boolean) |
|
127 @param autofixing flag indicating, that we are fixing issues |
|
128 automatically (boolean) |
127 """ |
129 """ |
128 from .Pep8Fixer import Pep8FixableIssues |
130 from .Pep8Fixer import Pep8FixableIssues |
129 |
131 |
130 if self.__lastFileItem is None: |
132 if self.__lastFileItem is None: |
131 # It's a new file |
133 # It's a new file |
142 itm.setIcon(1, UI.PixmapCache.getIcon("warning.png")) |
144 itm.setIcon(1, UI.PixmapCache.getIcon("warning.png")) |
143 else: |
145 else: |
144 itm.setIcon(1, UI.PixmapCache.getIcon("syntaxError.png")) |
146 itm.setIcon(1, UI.PixmapCache.getIcon("syntaxError.png")) |
145 if fixed: |
147 if fixed: |
146 itm.setIcon(0, UI.PixmapCache.getIcon("issueFixed.png")) |
148 itm.setIcon(0, UI.PixmapCache.getIcon("issueFixed.png")) |
147 elif code in Pep8FixableIssues: |
149 elif code in Pep8FixableIssues and not autofixing: |
148 itm.setIcon(0, UI.PixmapCache.getIcon("issueFixable.png")) |
150 itm.setIcon(0, UI.PixmapCache.getIcon("issueFixable.png")) |
149 fixable = True |
151 fixable = True |
150 |
152 |
151 itm.setTextAlignment(0, Qt.AlignRight) |
153 itm.setTextAlignment(0, Qt.AlignRight) |
152 itm.setTextAlignment(1, Qt.AlignHCenter) |
154 itm.setTextAlignment(1, Qt.AlignHCenter) |
160 itm.setData(0, self.positionRole, int(pos)) |
162 itm.setData(0, self.positionRole, int(pos)) |
161 itm.setData(0, self.messageRole, message) |
163 itm.setData(0, self.messageRole, message) |
162 itm.setData(0, self.fixableRole, fixable) |
164 itm.setData(0, self.fixableRole, fixable) |
163 itm.setData(0, self.codeRole, code) |
165 itm.setData(0, self.codeRole, code) |
164 |
166 |
165 def __modifyFixedResultItem(self, itm, text): |
167 def __modifyFixedResultItem(self, itm, text, fixed): |
166 """ |
168 """ |
167 Private method to modify a result list entry to show its |
169 Private method to modify a result list entry to show its |
168 positive fixed state. |
170 positive fixed state. |
169 |
171 |
170 @param itm reference to the item to modify (QTreeWidgetItem) |
172 @param itm reference to the item to modify (QTreeWidgetItem) |
171 @param text text to be appended (string) |
173 @param text text to be appended (string) |
172 """ |
174 @param fixed flag indicating a fixed issue (boolean) |
173 message = itm.data(0, self.messageRole) + text |
175 """ |
174 itm.setText(2, message) |
176 if fixed: |
175 itm.setIcon(0, UI.PixmapCache.getIcon("issueFixed.png")) |
177 message = itm.data(0, self.messageRole) + text |
176 |
178 itm.setText(2, message) |
177 itm.setData(0, self.messageRole, message) |
179 itm.setIcon(0, UI.PixmapCache.getIcon("issueFixed.png")) |
|
180 |
|
181 itm.setData(0, self.messageRole, message) |
|
182 else: |
|
183 itm.setIcon(0, QIcon()) |
178 itm.setData(0, self.fixableRole, False) |
184 itm.setData(0, self.fixableRole, False) |
179 |
185 |
180 def __updateStatistics(self, statistics, fixer): |
186 def __updateStatistics(self, statistics, fixer): |
181 """ |
187 """ |
182 Private method to update the collected statistics. |
188 Private method to update the collected statistics. |
353 source = source.splitlines(True) |
359 source = source.splitlines(True) |
354 except (UnicodeError, IOError) as msg: |
360 except (UnicodeError, IOError) as msg: |
355 self.noResults = False |
361 self.noResults = False |
356 self.__createResultItem(file, "1", "1", |
362 self.__createResultItem(file, "1", "1", |
357 self.trUtf8("Error: {0}").format(str(msg))\ |
363 self.trUtf8("Error: {0}").format(str(msg))\ |
358 .rstrip()[1:-1], False) |
364 .rstrip()[1:-1], False, False) |
359 progress += 1 |
365 progress += 1 |
360 continue |
366 continue |
361 |
367 |
362 flags = Utilities.extractFlags(source) |
368 flags = Utilities.extractFlags(source) |
363 ext = os.path.splitext(file)[1] |
369 ext = os.path.splitext(file)[1] |
419 fixed, msg = fixer.fixIssue(lineno, position, text) |
425 fixed, msg = fixer.fixIssue(lineno, position, text) |
420 if fixed: |
426 if fixed: |
421 text += "\n" + \ |
427 text += "\n" + \ |
422 self.trUtf8("Fix: {0}").format(msg) |
428 self.trUtf8("Fix: {0}").format(msg) |
423 self.__createResultItem( |
429 self.__createResultItem( |
424 fname, lineno, position, text, fixed) |
430 fname, lineno, position, text, fixed, fixIssues) |
425 fixer and fixer.saveFile(encoding) |
431 fixer and fixer.saveFile(encoding) |
426 self.__updateStatistics(report.counters, fixer) |
432 self.__updateStatistics(report.counters, fixer) |
427 progress += 1 |
433 progress += 1 |
428 finally: |
434 finally: |
429 # reenable updates of the list |
435 # reenable updates of the list |
706 @pyqtSlot() |
712 @pyqtSlot() |
707 def on_fixButton_clicked(self): |
713 def on_fixButton_clicked(self): |
708 """ |
714 """ |
709 Private slot to fix selected issues. |
715 Private slot to fix selected issues. |
710 """ |
716 """ |
711 # TODO: test this |
|
712 from .Pep8Fixer import Pep8Fixer |
717 from .Pep8Fixer import Pep8Fixer |
713 |
718 |
714 # build a dictionary of issues to fix |
719 # build a dictionary of issues to fix |
715 fixableItems = self.__getSelectedFixableItems() |
720 fixableItems = self.__getSelectedFixableItems() |
716 fixesDict = {} # dictionary of lists of tuples containing |
721 fixesDict = {} # dictionary of lists of tuples containing |
758 if lineno > len(source): |
763 if lineno > len(source): |
759 lineno = len(source) |
764 lineno = len(source) |
760 fixed, msg = fixer.fixIssue(lineno, position, text) |
765 fixed, msg = fixer.fixIssue(lineno, position, text) |
761 if fixed: |
766 if fixed: |
762 text = "\n" + self.trUtf8("Fix: {0}").format(msg) |
767 text = "\n" + self.trUtf8("Fix: {0}").format(msg) |
763 self.__modifyFixedResultItem(itm, text) |
768 else: |
|
769 text = "" |
|
770 self.__modifyFixedResultItem(itm, text, fixed) |
764 fixer.saveFile(encoding) |
771 fixer.saveFile(encoding) |
765 |
772 |
766 self.__updateFixerStatistics(fixer) |
773 self.__updateFixerStatistics(fixer) |
767 progress += 1 |
774 progress += 1 |
768 |
775 |