23 class InternalServices(QObject): |
23 class InternalServices(QObject): |
24 """ |
24 """ |
25 Implement the standard services (syntax with flakes and the style check). |
25 Implement the standard services (syntax with flakes and the style check). |
26 """ |
26 """ |
27 syntaxChecked = pyqtSignal(str, bool, str, int, int, str, str, list) |
27 syntaxChecked = pyqtSignal(str, bool, str, int, int, str, str, list) |
28 #styleChecked = pyqtSignal(TBD) |
28 styleChecked = pyqtSignal(str, dict, int, list) |
29 #indentChecked = pyqtSignal(TBD) |
29 #indentChecked = pyqtSignal(TBD) |
30 |
30 |
31 def __init__(self, backgroundService): |
31 def __init__(self, backgroundService): |
32 """ |
32 """ |
33 Contructor of InternalServices. |
33 Contructor of InternalServices. |
35 @param backgroundService to connect to |
35 @param backgroundService to connect to |
36 """ |
36 """ |
37 super(InternalServices, self).__init__() |
37 super(InternalServices, self).__init__() |
38 self.backgroundService = backgroundService |
38 self.backgroundService = backgroundService |
39 |
39 |
40 path = os.path.join( |
40 ericPath = getConfig('ericDir') |
41 getConfig('ericDir'), 'Plugins', 'CheckerPlugins', 'SyntaxChecker') |
41 # Syntax check |
|
42 path = os.path.join(ericPath, 'Plugins', 'CheckerPlugins', |
|
43 'SyntaxChecker') |
42 self.backgroundService.serviceConnect( |
44 self.backgroundService.serviceConnect( |
43 'syntax', path, 'SyntaxCheck', |
45 'syntax', path, 'SyntaxCheck', |
44 self.__translateSyntaxCheck, |
46 self.__translateSyntaxCheck, |
45 lambda fx, fn, ver, msg: self.syntaxChecked.emit( |
47 lambda fx, fn, ver, msg: self.syntaxChecked.emit( |
46 fn, True, fn, 0, 0, '', msg, [])) |
48 fn, True, fn, 0, 0, '', msg, [])) |
|
49 |
|
50 # Style check |
|
51 path = os.path.join(ericPath, 'Plugins', 'CheckerPlugins', |
|
52 'CodeStyleChecker') |
|
53 self.backgroundService.serviceConnect( |
|
54 'style', path, 'CodeStyleChecker', |
|
55 self.__translateStyleCheck, |
|
56 lambda fx, fn, ver, msg: self.styleChecked.emit( |
|
57 fn, {}, 0, [[0, 0, '---- ' + msg, False, False]])) |
|
58 |
|
59 # # Indent check |
|
60 # path = os.path.join(ericPath, 'Plugins', 'CheckerPlugins', |
|
61 # 'Tabnanny') |
|
62 # self.backgroundService.serviceConnect( |
|
63 # 'indent', path, 'Tabnanny', |
|
64 # self.__translateIndentCheck) |
47 |
65 |
48 def syntaxCheck(self, filename, source="", checkFlakes=True, |
66 def syntaxCheck(self, filename, source="", checkFlakes=True, |
49 ignoreStarImportWarnings=False, pyVer=None, editor=None): |
67 ignoreStarImportWarnings=False, pyVer=None, editor=None): |
50 """ |
68 """ |
51 Function to compile one Python source file to Python bytecode |
69 Method to prepare to compile one Python source file to Python bytecode |
52 and to perform a pyflakes check. |
70 and to perform a pyflakes check in another task. |
53 |
71 |
54 @param filename source filename (string) |
72 @param filename source filename (string) |
55 @keyparam source string containing the code to check (string) |
73 @keyparam source string containing the code to check (string) |
56 @keyparam checkFlakes flag indicating to do a pyflakes check (boolean) |
74 @keyparam checkFlakes flag indicating to do a pyflakes check (boolean) |
57 @keyparam ignoreStarImportWarnings flag indicating to |
75 @keyparam ignoreStarImportWarnings flag indicating to |
95 translated = translated[1:] |
113 translated = translated[1:] |
96 warning[4] = translated.replace(" u'", " '") |
114 warning[4] = translated.replace(" u'", " '") |
97 |
115 |
98 self.syntaxChecked.emit( |
116 self.syntaxChecked.emit( |
99 fn, nok, fname, line, index, code, error, warnings) |
117 fn, nok, fname, line, index, code, error, warnings) |
|
118 |
|
119 def styleCheck(self, filename, source, args, pyVer=None, editor=None): |
|
120 """ |
|
121 Method to prepare a style check on one Python source file in another |
|
122 task. |
|
123 |
|
124 @param filename source filename (string) |
|
125 @param source string containing the code to check (string) |
|
126 @param args arguments used by the codeStyleCheck function (list of |
|
127 excludeMessages (str), includeMessages (str), repeatMessages |
|
128 (bool), fixCodes (str), noFixCodes (str), fixIssues (bool), |
|
129 maxLineLength (int), hangClosing (bool), docType (str), errors |
|
130 (list of str), eol (str), encoding (str)) |
|
131 @keyparam pyVer version of the interpreter to use or None for |
|
132 autodetect corresponding interpreter (int or None) |
|
133 @keyparam editor if the file is opened already (Editor object) |
|
134 """ |
|
135 if pyVer is None: |
|
136 pyVer = determinePythonVersion(filename, source, editor) |
|
137 |
|
138 data = [source, args] |
|
139 self.backgroundService.enqueueRequest('style', filename, pyVer, data) |
|
140 |
|
141 def __translateStyleCheck(self, fn, codeStyleCheckerStats, results): |
|
142 """ |
|
143 Privat slot called after perfoming a style check on one file. |
|
144 |
|
145 @param fn filename of the just checked file (str) |
|
146 @param codeStyleCheckerStats stats of style and name check (dict) |
|
147 @param results tuple for each found violation of style (tuple of |
|
148 lineno (int), position (int), text (str), fixed (bool), |
|
149 autofixing (bool), fixedMsg (str)) |
|
150 """ |
|
151 fixes = 0 |
|
152 for result in results: |
|
153 msg = result[2].split('@@') |
|
154 if msg[0].startswith(('W', 'E')): |
|
155 msgType = 'pep8' |
|
156 elif msg[0].startswith('N'): |
|
157 msgType = 'NamingStyleChecker' |
|
158 else: |
|
159 msgType = 'DocStyleChecker' |
|
160 translMsg = msg[0][:5] + QApplication.translate( |
|
161 msgType, msg[0][5:]).format(*msg[1:]) |
|
162 |
|
163 fixedMsg = result.pop() |
|
164 if fixedMsg: |
|
165 fixes += 1 |
|
166 if '@@' in fixedMsg: |
|
167 msg, param = fixedMsg.split('@@') |
|
168 fixedMsg = QApplication.translate( |
|
169 'CodeStyleFixer', msg).format(param) |
|
170 else: |
|
171 fixedMsg = QApplication.translate( |
|
172 'CodeStyleFixer', fixedMsg) |
|
173 |
|
174 translMsg += "\n" + QApplication.translate( |
|
175 'CodeStyleCheckerDialog', "Fix: {0}").format(fixedMsg) |
|
176 result[2] = translMsg |
|
177 self.styleChecked.emit(fn, codeStyleCheckerStats, fixes, results) |