10 # |
10 # |
11 # The routines of the checker class are modeled after the ones found in |
11 # The routines of the checker class are modeled after the ones found in |
12 # pep257.py (version 0.2.4). |
12 # pep257.py (version 0.2.4). |
13 # |
13 # |
14 |
14 |
15 try: |
|
16 # Python 2 |
|
17 from StringIO import StringIO # __IGNORE_EXCEPTION__ |
|
18 except ImportError: |
|
19 # Python 3 |
|
20 from io import StringIO # __IGNORE_WARNING__ |
|
21 import tokenize |
15 import tokenize |
22 import ast |
16 import ast |
23 import sys |
17 import sys |
|
18 from io import StringIO |
24 |
19 |
25 try: |
20 try: |
26 ast.AsyncFunctionDef # __IGNORE_EXCEPTION__ |
21 ast.AsyncFunctionDef # __IGNORE_EXCEPTION__ |
27 except AttributeError: |
22 except AttributeError: |
28 ast.AsyncFunctionDef = ast.FunctionDef |
23 ast.AsyncFunctionDef = ast.FunctionDef |
126 """ |
121 """ |
127 Class implementing a checker for documentation string conventions. |
122 Class implementing a checker for documentation string conventions. |
128 """ |
123 """ |
129 Codes = [ |
124 Codes = [ |
130 "D101", "D102", "D103", "D104", "D105", |
125 "D101", "D102", "D103", "D104", "D105", |
131 "D111", "D112", "D113", |
126 "D111", "D112", |
132 "D121", "D122", |
127 "D121", "D122", |
133 "D130", "D131", "D132", "D133", "D134", |
128 "D130", "D131", "D132", "D133", "D134", |
134 "D141", "D142", "D143", "D144", "D145", |
129 "D141", "D142", "D143", "D144", "D145", |
135 |
130 |
136 "D203", "D205", "D206", |
131 "D203", "D205", "D206", |
207 (self.__checkNoBlankLineBefore, ("D141",)), |
202 (self.__checkNoBlankLineBefore, ("D141",)), |
208 ], |
203 ], |
209 "docstring": [ |
204 "docstring": [ |
210 (self.__checkTripleDoubleQuotes, ("D111",)), |
205 (self.__checkTripleDoubleQuotes, ("D111",)), |
211 (self.__checkBackslashes, ("D112",)), |
206 (self.__checkBackslashes, ("D112",)), |
212 (self.__checkUnicode, ("D113",)), |
|
213 (self.__checkOneLiner, ("D121",)), |
207 (self.__checkOneLiner, ("D121",)), |
214 (self.__checkIndent, ("D122",)), |
208 (self.__checkIndent, ("D122",)), |
215 (self.__checkSummary, ("D130",)), |
209 (self.__checkSummary, ("D130",)), |
216 (self.__checkEndsWithPeriod, ("D131",)), |
210 (self.__checkEndsWithPeriod, ("D131",)), |
217 (self.__checkBlankAfterSummary, ("D144",)), |
211 (self.__checkBlankAfterSummary, ("D144",)), |
247 ("D250", "D251", "D252", "D253")), |
241 ("D250", "D251", "D252", "D253")), |
248 ], |
242 ], |
249 "docstring": [ |
243 "docstring": [ |
250 (self.__checkTripleDoubleQuotes, ("D111",)), |
244 (self.__checkTripleDoubleQuotes, ("D111",)), |
251 (self.__checkBackslashes, ("D112",)), |
245 (self.__checkBackslashes, ("D112",)), |
252 (self.__checkUnicode, ("D113",)), |
|
253 (self.__checkIndent, ("D122",)), |
246 (self.__checkIndent, ("D122",)), |
254 (self.__checkSummary, ("D130",)), |
247 (self.__checkSummary, ("D130",)), |
255 (self.__checkEricEndsWithPeriod, ("D231",)), |
248 (self.__checkEricEndsWithPeriod, ("D231",)), |
256 (self.__checkEricBlankAfterSummary, ("D246",)), |
249 (self.__checkEricBlankAfterSummary, ("D246",)), |
257 (self.__checkEricNBlankAfterLastParagraph, ("D247",)), |
250 (self.__checkEricNBlankAfterLastParagraph, ("D247",)), |
775 return |
768 return |
776 |
769 |
777 docstring = docstringContext.ssource().strip() |
770 docstring = docstringContext.ssource().strip() |
778 if "\\" in docstring and not docstring.startswith('r"""'): |
771 if "\\" in docstring and not docstring.startswith('r"""'): |
779 self.__error(docstringContext.start(), 0, "D112") |
772 self.__error(docstringContext.start(), 0, "D112") |
780 |
|
781 def __checkUnicode(self, docstringContext, context): |
|
782 """ |
|
783 Private method to check, that all docstrings containing unicode |
|
784 characters are surrounded by unicode triple double quotes. |
|
785 |
|
786 @param docstringContext docstring context (DocStyleContext) |
|
787 @param context context of the docstring (DocStyleContext) |
|
788 """ |
|
789 if docstringContext is None: |
|
790 return |
|
791 |
|
792 docstring = docstringContext.ssource().strip() |
|
793 if ( |
|
794 not docstring.startswith('u"""') and |
|
795 any(ord(char) > 127 for char in docstring) |
|
796 ): |
|
797 self.__error(docstringContext.start(), 0, "D113") |
|
798 |
773 |
799 def __checkOneLiner(self, docstringContext, context): |
774 def __checkOneLiner(self, docstringContext, context): |
800 """ |
775 """ |
801 Private method to check, that one-liner docstrings fit on |
776 Private method to check, that one-liner docstrings fit on |
802 one line with quotes. |
777 one line with quotes. |