45 |
45 |
46 class CodeStyleCheckerPlugin(QObject): |
46 class CodeStyleCheckerPlugin(QObject): |
47 """ |
47 """ |
48 Class implementing the code style checker plug-in. |
48 Class implementing the code style checker plug-in. |
49 |
49 |
50 @signal styleChecked(str, dict, int, list) emited when the style check was |
50 @signal styleChecked(str, dict, int, list) emitted when the style check was |
51 done. |
51 done for a file. |
|
52 @signal batchFinished() emitted when a style check batch is done |
52 """ |
53 """ |
53 styleChecked = pyqtSignal(str, dict, int, list) |
54 styleChecked = pyqtSignal(str, dict, int, list) |
|
55 batchFinished = pyqtSignal() |
54 |
56 |
55 def __init__(self, ui): |
57 def __init__(self, ui): |
56 """ |
58 """ |
57 Constructor |
59 Constructor |
58 |
60 |
67 path = os.path.join( |
69 path = os.path.join( |
68 os.path.dirname(__file__), 'CheckerPlugins', 'CodeStyleChecker') |
70 os.path.dirname(__file__), 'CheckerPlugins', 'CodeStyleChecker') |
69 self.backgroundService.serviceConnect( |
71 self.backgroundService.serviceConnect( |
70 'style', 'Python2', path, 'CodeStyleChecker', |
72 'style', 'Python2', path, 'CodeStyleChecker', |
71 self.__translateStyleCheck, |
73 self.__translateStyleCheck, |
72 onErrorCallback=self.serviceErrorPy2) |
74 onErrorCallback=self.serviceErrorPy2, |
|
75 onBatchDone=self.batchJobDone) |
73 self.backgroundService.serviceConnect( |
76 self.backgroundService.serviceConnect( |
74 'style', 'Python3', path, 'CodeStyleChecker', |
77 'style', 'Python3', path, 'CodeStyleChecker', |
75 self.__translateStyleCheck, |
78 self.__translateStyleCheck, |
76 onErrorCallback=self.serviceErrorPy3) |
79 onErrorCallback=self.serviceErrorPy3, |
|
80 onBatchDone=self.batchJobDone) |
|
81 |
|
82 self.queuedBatches = [] |
|
83 self.batchesFinished = True |
77 |
84 |
78 def __serviceError(self, fn, msg): |
85 def __serviceError(self, fn, msg): |
79 """ |
86 """ |
80 Private slot handling service errors. |
87 Private slot handling service errors. |
81 |
88 |
85 self.styleChecked.emit( |
92 self.styleChecked.emit( |
86 fn, {}, 0, [[1, 1, '---- ' + msg, False, False, False]]) |
93 fn, {}, 0, [[1, 1, '---- ' + msg, False, False, False]]) |
87 |
94 |
88 def serviceErrorPy2(self, fx, lang, fn, msg): |
95 def serviceErrorPy2(self, fx, lang, fn, msg): |
89 """ |
96 """ |
90 Public method handling service errors for Python 2. |
97 Public slot handling service errors for Python 2. |
91 |
98 |
92 @param fx service name (string) |
99 @param fx service name (string) |
93 @param lang language (string) |
100 @param lang language (string) |
94 @param fn file name (string) |
101 @param fn file name (string) |
95 @param msg message text (string) |
102 @param msg message text (string) |
97 if fx == 'style' and lang == 'Python2': |
104 if fx == 'style' and lang == 'Python2': |
98 self.__serviceError(fn, msg) |
105 self.__serviceError(fn, msg) |
99 |
106 |
100 def serviceErrorPy3(self, fx, lang, fn, msg): |
107 def serviceErrorPy3(self, fx, lang, fn, msg): |
101 """ |
108 """ |
102 Public method handling service errors for Python 2. |
109 Public slot handling service errors for Python 2. |
103 |
110 |
104 @param fx service name (string) |
111 @param fx service name (string) |
105 @param lang language (string) |
112 @param lang language (string) |
106 @param fn file name (string) |
113 @param fn file name (string) |
107 @param msg message text (string) |
114 @param msg message text (string) |
108 """ |
115 """ |
109 if fx == 'style' and lang == 'Python3': |
116 if fx == 'style' and lang == 'Python3': |
110 self.__serviceError(fn, msg) |
117 self.__serviceError(fn, msg) |
111 |
118 |
|
119 def batchJobDone(self, fx, lang): |
|
120 """ |
|
121 Public slot handling the completion of a batch job. |
|
122 |
|
123 @param fx service name (string) |
|
124 @param lang language (string) |
|
125 """ |
|
126 if fx == 'style': |
|
127 if lang in self.queuedBatches: |
|
128 self.queuedBatches.remove(lang) |
|
129 # prevent sending the signal multiple times |
|
130 if len(self.queuedBatches) == 0 and not self.batchesFinished: |
|
131 self.batchFinished.emit() |
|
132 self.batchesFinished = True |
|
133 |
112 def __initialize(self): |
134 def __initialize(self): |
113 """ |
135 """ |
114 Private slot to (re)initialize the plugin. |
136 Private slot to (re)initialize the plugin. |
115 """ |
137 """ |
116 self.__projectAct = None |
138 self.__projectAct = None |
144 return |
166 return |
145 |
167 |
146 data = [source, args] |
168 data = [source, args] |
147 self.backgroundService.enqueueRequest('style', lang, filename, data) |
169 self.backgroundService.enqueueRequest('style', lang, filename, data) |
148 |
170 |
|
171 def styleBatchCheck(self, argumentsList): |
|
172 """ |
|
173 Public method to prepare a style check on multiple Python source files. |
|
174 |
|
175 @param argumentsList list of arguments tuples with each tuple |
|
176 containing filename, source and args as given in styleCheck() |
|
177 method |
|
178 """ |
|
179 data = { |
|
180 "Python2": [], |
|
181 "Python3": [], |
|
182 } |
|
183 for filename, source, args in argumentsList: |
|
184 lang = 'Python{0}'.format(determinePythonVersion(filename, source)) |
|
185 if lang not in ['Python2', 'Python3']: |
|
186 continue |
|
187 else: |
|
188 data[lang].append((filename, source, args)) |
|
189 |
|
190 self.queuedBatches = [] |
|
191 for lang in ['Python2', 'Python3']: |
|
192 if data[lang]: |
|
193 self.queuedBatches.append(lang) |
|
194 self.backgroundService.enqueueRequest('batch_style', lang, "", |
|
195 data[lang]) |
|
196 self.batchesFinished = False |
|
197 |
149 def __translateStyleCheck(self, fn, codeStyleCheckerStats, results): |
198 def __translateStyleCheck(self, fn, codeStyleCheckerStats, results): |
150 """ |
199 """ |
151 Private slot called after perfoming a style check on one file. |
200 Private slot called after perfoming a style check on one file. |
152 |
201 |
153 @param fn filename of the just checked file (str) |
202 @param fn filename of the just checked file (str) |
156 lineno (int), position (int), text (str), fixed (bool), |
205 lineno (int), position (int), text (str), fixed (bool), |
157 autofixing (bool), fixedMsg (str)) |
206 autofixing (bool), fixedMsg (str)) |
158 """ |
207 """ |
159 from CheckerPlugins.CodeStyleChecker.translations import \ |
208 from CheckerPlugins.CodeStyleChecker.translations import \ |
160 getTranslatedMessage |
209 getTranslatedMessage |
161 |
210 |
162 fixes = 0 |
211 fixes = 0 |
163 for result in results: |
212 for result in results: |
164 msg = getTranslatedMessage(result[2]) |
213 msg = getTranslatedMessage(result[2]) |
165 |
214 |
166 fixedMsg = result.pop() |
215 fixedMsg = result.pop() |