Plugins/CheckerPlugins/CodeStyleChecker/DocStyleChecker.py

branch
BgService
changeset 3413
5e63f809732a
parent 3209
c5432abceb25
child 3456
96232974dcdb
equal deleted inserted replaced
3412:9364dab2d472 3413:5e63f809732a
19 # Python 3 19 # Python 3
20 from io import StringIO # __IGNORE_WARNING__ 20 from io import StringIO # __IGNORE_WARNING__
21 import tokenize 21 import tokenize
22 import ast 22 import ast
23 import sys 23 import sys
24
25 # Tell 'lupdate' which strings to keep for translation.
26 QT_TRANSLATE_NOOP = lambda mod, txt: txt
27 24
28 25
29 class DocStyleContext(object): 26 class DocStyleContext(object):
30 """ 27 """
31 Class implementing the source context. 28 Class implementing the source context.
118 "D242", "D243", "D244", "D245", "D246", "D247", 115 "D242", "D243", "D244", "D245", "D246", "D247",
119 "D250", "D251", 116 "D250", "D251",
120 117
121 "D901", 118 "D901",
122 ] 119 ]
123
124 Messages = {
125 "D101": QT_TRANSLATE_NOOP(
126 "DocStyleChecker", "module is missing a docstring"),
127 "D102": QT_TRANSLATE_NOOP(
128 "DocStyleChecker",
129 "public function/method is missing a docstring"),
130 "D103": QT_TRANSLATE_NOOP(
131 "DocStyleChecker",
132 "private function/method may be missing a docstring"),
133 "D104": QT_TRANSLATE_NOOP(
134 "DocStyleChecker", "public class is missing a docstring"),
135 "D105": QT_TRANSLATE_NOOP(
136 "DocStyleChecker", "private class may be missing a docstring"),
137 "D111": QT_TRANSLATE_NOOP(
138 "DocStyleChecker", 'docstring not surrounded by """'),
139 "D112": QT_TRANSLATE_NOOP(
140 "DocStyleChecker",
141 'docstring containing \\ not surrounded by r"""'),
142 "D113": QT_TRANSLATE_NOOP(
143 "DocStyleChecker",
144 'docstring containing unicode character not surrounded by u"""'),
145 "D121": QT_TRANSLATE_NOOP(
146 "DocStyleChecker", "one-liner docstring on multiple lines"),
147 "D122": QT_TRANSLATE_NOOP(
148 "DocStyleChecker", "docstring has wrong indentation"),
149 "D131": QT_TRANSLATE_NOOP(
150 "DocStyleChecker", "docstring summary does not end with a period"),
151 "D132": QT_TRANSLATE_NOOP(
152 "DocStyleChecker",
153 "docstring summary is not in imperative mood"
154 " (Does instead of Do)"),
155 "D133": QT_TRANSLATE_NOOP(
156 "DocStyleChecker",
157 "docstring summary looks like a function's/method's signature"),
158 "D134": QT_TRANSLATE_NOOP(
159 "DocStyleChecker",
160 "docstring does not mention the return value type"),
161 "D141": QT_TRANSLATE_NOOP(
162 "DocStyleChecker",
163 "function/method docstring is separated by a blank line"),
164 "D142": QT_TRANSLATE_NOOP(
165 "DocStyleChecker",
166 "class docstring is not preceded by a blank line"),
167 "D143": QT_TRANSLATE_NOOP(
168 "DocStyleChecker",
169 "class docstring is not followed by a blank line"),
170 "D144": QT_TRANSLATE_NOOP(
171 "DocStyleChecker",
172 "docstring summary is not followed by a blank line"),
173 "D145": QT_TRANSLATE_NOOP(
174 "DocStyleChecker",
175 "last paragraph of docstring is not followed by a blank line"),
176
177 "D203": QT_TRANSLATE_NOOP(
178 "DocStyleChecker",
179 "private function/method is missing a docstring"),
180 "D205": QT_TRANSLATE_NOOP(
181 "DocStyleChecker", "private class is missing a docstring"),
182 "D221": QT_TRANSLATE_NOOP(
183 "DocStyleChecker",
184 "leading quotes of docstring not on separate line"),
185 "D222": QT_TRANSLATE_NOOP(
186 "DocStyleChecker",
187 "trailing quotes of docstring not on separate line"),
188 "D231": QT_TRANSLATE_NOOP(
189 "DocStyleChecker", "docstring summary does not end with a period"),
190 "D234": QT_TRANSLATE_NOOP(
191 "DocStyleChecker",
192 "docstring does not contain a @return line but function/method"
193 " returns something"),
194 "D235": QT_TRANSLATE_NOOP(
195 "DocStyleChecker",
196 "docstring contains a @return line but function/method doesn't"
197 " return anything"),
198 "D236": QT_TRANSLATE_NOOP(
199 "DocStyleChecker",
200 "docstring does not contain enough @param/@keyparam lines"),
201 "D237": QT_TRANSLATE_NOOP(
202 "DocStyleChecker",
203 "docstring contains too many @param/@keyparam lines"),
204 "D238": QT_TRANSLATE_NOOP(
205 "DocStyleChecker",
206 "keyword only arguments must be documented with @keyparam lines"),
207 "D239": QT_TRANSLATE_NOOP(
208 "DocStyleChecker", "order of @param/@keyparam lines does"
209 " not match the function/method signature"),
210 "D242": QT_TRANSLATE_NOOP(
211 "DocStyleChecker", "class docstring is preceded by a blank line"),
212 "D243": QT_TRANSLATE_NOOP(
213 "DocStyleChecker", "class docstring is followed by a blank line"),
214 "D244": QT_TRANSLATE_NOOP(
215 "DocStyleChecker",
216 "function/method docstring is preceded by a blank line"),
217 "D245": QT_TRANSLATE_NOOP(
218 "DocStyleChecker",
219 "function/method docstring is followed by a blank line"),
220 "D246": QT_TRANSLATE_NOOP(
221 "DocStyleChecker",
222 "docstring summary is not followed by a blank line"),
223 "D247": QT_TRANSLATE_NOOP(
224 "DocStyleChecker",
225 "last paragraph of docstring is followed by a blank line"),
226 "D250": QT_TRANSLATE_NOOP(
227 "DocStyleChecker",
228 "docstring does not contain a @exception line but function/method"
229 " raises an exception"),
230 "D251": QT_TRANSLATE_NOOP(
231 "DocStyleChecker",
232 "docstring contains a @exception line but function/method doesn't"
233 " raise an exception"),
234
235 "D901": QT_TRANSLATE_NOOP(
236 "DocStyleChecker", "{0}: {1}"),
237 }
238
239 MessagesSampleArgs = {
240 "D901": ["SyntaxError", "Invalid Syntax"],
241 }
242 120
243 def __init__(self, source, filename, select, ignore, expected, repeat, 121 def __init__(self, source, filename, select, ignore, expected, repeat,
244 maxLineLength=79, docType="pep257"): 122 maxLineLength=79, docType="pep257"):
245 """ 123 """
246 Constructor 124 Constructor
392 # Don't care about expected codes 270 # Don't care about expected codes
393 if code in self.__expected: 271 if code in self.__expected:
394 return 272 return
395 273
396 if code and (self.counters[code] == 1 or self.__repeat): 274 if code and (self.counters[code] == 1 or self.__repeat):
397 text = self.getMessage(code, *args)
398 # record the issue with one based line number 275 # record the issue with one based line number
399 self.errors.append((self.__filename, lineNumber + 1, offset, text)) 276 self.errors.append(
277 (self.__filename, lineNumber + 1, offset, (code, args)))
400 278
401 def __reportInvalidSyntax(self): 279 def __reportInvalidSyntax(self):
402 """ 280 """
403 Private method to report a syntax error. 281 Private method to report a syntax error.
404 """ 282 """
409 offset = offset[1:3] 287 offset = offset[1:3]
410 else: 288 else:
411 offset = (1, 0) 289 offset = (1, 0)
412 self.__error(offset[0] - 1, offset[1] or 0, 290 self.__error(offset[0] - 1, offset[1] or 0,
413 'D901', exc_type.__name__, exc.args[0]) 291 'D901', exc_type.__name__, exc.args[0])
414
415 @classmethod
416 def getMessage(cls, code, *args):
417 """
418 Class method to get a translated and formatted message for a
419 given code.
420
421 @param code message code (string)
422 @param args arguments for a formatted message (list)
423 @return translated and formatted message (string)
424 """
425 if code in cls.Messages:
426 return '@@'.join(
427 [code + ' ' + cls.Messages[code]] + list(args))
428 else:
429 return code + ' ' + QT_TRANSLATE_NOOP(
430 "DocStyleChecker", "no message for this code defined")
431 292
432 def __resetReadline(self): 293 def __resetReadline(self):
433 """ 294 """
434 Private method to reset the internal readline function. 295 Private method to reset the internal readline function.
435 """ 296 """

eric ide

mercurial