UtilitiesPython2/Pep257CheckerPy2.py

changeset 2949
e8f41288a688
parent 2948
ea04689ee599
child 2952
94fc661a54a2
equal deleted inserted replaced
2948:ea04689ee599 2949:e8f41288a688
111 111
112 "D203", "D205", 112 "D203", "D205",
113 "D221", 113 "D221",
114 "D231", "D234", "D235", "D236", "D237", "D238", "D239", 114 "D231", "D234", "D235", "D236", "D237", "D238", "D239",
115 "D242", "D243", "D244", "D245", "D246", "D247", 115 "D242", "D243", "D244", "D245", "D246", "D247",
116 "D250", "D251",
116 ] 117 ]
117 118
118 def __init__(self, source, filename, select, ignore, expected, repeat, 119 def __init__(self, source, filename, select, ignore, expected, repeat,
119 maxLineLength=79, docType="pep257"): 120 maxLineLength=79, docType="pep257"):
120 """ 121 """
213 (self.__checkEricReturn, ("D234", "D235")), 214 (self.__checkEricReturn, ("D234", "D235")),
214 (self.__checkEricFunctionArguments, 215 (self.__checkEricFunctionArguments,
215 ("D236", "D237", "D238", "D239")), 216 ("D236", "D237", "D238", "D239")),
216 (self.__checkEricNoBlankBeforeAndAfterClassOrFunction, 217 (self.__checkEricNoBlankBeforeAndAfterClassOrFunction,
217 ("D244", "D245")), 218 ("D244", "D245")),
219 (self.__checkEricException, ("D250", "D251")),
218 ], 220 ],
219 "docstring": [ 221 "docstring": [
220 (self.__checkTripleDoubleQuotes, ("D111",)), 222 (self.__checkTripleDoubleQuotes, ("D111",)),
221 (self.__checkBackslashes, ("D112",)), 223 (self.__checkBackslashes, ("D112",)),
222 (self.__checkUnicode, ("D113",)), 224 (self.__checkUnicode, ("D113",)),
338 """ 340 """
339 summaries = [] 341 summaries = []
340 lines = docstringContext.source() 342 lines = docstringContext.source()
341 343
342 line0 = (lines[0] 344 line0 = (lines[0]
343 .replace('r"""', "", 1) 345 .replace('r"""', "", 1)
344 .replace('u"""', "", 1) 346 .replace('u"""', "", 1)
345 .replace('"""', "") 347 .replace('"""', "")
346 .replace("r'''", "", 1) 348 .replace("r'''", "", 1)
347 .replace("u'''", "", 1) 349 .replace("u'''", "", 1)
348 .replace("'''", "") 350 .replace("'''", "")
349 .strip()) 351 .strip())
350 if len(lines) > 1: 352 if len(lines) > 1:
351 line1 = lines[1].strip().replace('"""', "").replace("'''", "") 353 line1 = lines[1].strip().replace('"""', "").replace("'''", "")
352 else: 354 else:
353 line1 = "" 355 line1 = ""
354 if len(lines) > 2: 356 if len(lines) > 2:
446 def __parseDocstring(self, context, what=''): 448 def __parseDocstring(self, context, what=''):
447 """ 449 """
448 Private method to extract a docstring given `def` or `class` source. 450 Private method to extract a docstring given `def` or `class` source.
449 451
450 @param context context data to get the docstring from (Pep257Context) 452 @param context context data to get the docstring from (Pep257Context)
453 @param what string denoting what is being parsed (string)
451 @return context of extracted docstring (Pep257Context) 454 @return context of extracted docstring (Pep257Context)
452 """ 455 """
453 moduleDocstring = self.__parseModuleDocstring(context.source()) 456 moduleDocstring = self.__parseModuleDocstring(context.source())
454 if what.startswith('module') or context.contextType() == "module": 457 if what.startswith('module') or context.contextType() == "module":
455 return moduleDocstring 458 return moduleDocstring
983 docstringContext.start() + lineNumber + len(summaryLines) - 1, 986 docstringContext.start() + lineNumber + len(summaryLines) - 1,
984 0, "D231") 987 0, "D231")
985 988
986 def __checkEricReturn(self, docstringContext, context): 989 def __checkEricReturn(self, docstringContext, context):
987 """ 990 """
988 Private method to check, that docstrings contain an @return line 991 Private method to check, that docstrings contain an @return line
989 if they return anything and don't otherwise. 992 if they return anything and don't otherwise.
990 993
991 @param docstringContext docstring context (Pep257Context) 994 @param docstringContext docstring context (Pep257Context)
992 @param context context of the docstring (Pep257Context) 995 @param context context of the docstring (Pep257Context)
993 """ 996 """
1009 set([])): 1012 set([])):
1010 self.__error(docstringContext.end(), 0, "D235") 1013 self.__error(docstringContext.end(), 0, "D235")
1011 1014
1012 def __checkEricFunctionArguments(self, docstringContext, context): 1015 def __checkEricFunctionArguments(self, docstringContext, context):
1013 """ 1016 """
1014 Private method to check, that docstrings contain an @param line 1017 Private method to check, that docstrings contain an @param and/or
1015 for each argument. 1018 @keyparam line for each argument.
1016 1019
1017 @param docstringContext docstring context (Pep257Context) 1020 @param docstringContext docstring context (Pep257Context)
1018 @param context context of the docstring (Pep257Context) 1021 @param context context of the docstring (Pep257Context)
1019 """ 1022 """
1020 if docstringContext is None or self.__isScript: 1023 if docstringContext is None or self.__isScript:
1032 argNames.remove("self") 1035 argNames.remove("self")
1033 if "cls" in argNames: 1036 if "cls" in argNames:
1034 argNames.remove("cls") 1037 argNames.remove("cls")
1035 1038
1036 docstring = docstringContext.ssource() 1039 docstring = docstringContext.ssource()
1037 if (docstring.count("@param") + docstring.count("@keyparam") < 1040 if (docstring.count("@param") + docstring.count("@keyparam") <
1038 len(argNames + kwNames)): 1041 len(argNames + kwNames)):
1039 self.__error(docstringContext.end(), 0, "D236") 1042 self.__error(docstringContext.end(), 0, "D236")
1040 elif (docstring.count("@param") + docstring.count("@keyparam") > 1043 elif (docstring.count("@param") + docstring.count("@keyparam") >
1041 len(argNames + kwNames)): 1044 len(argNames + kwNames)):
1042 self.__error(docstringContext.end(), 0, "D237") 1045 self.__error(docstringContext.end(), 0, "D237")
1043 else: 1046 else:
1044 # extract @param and @keyparam from docstring 1047 # extract @param and @keyparam from docstring
1045 args = [] 1048 args = []
1057 self.__error(docstringContext.end(), 0, "D238") 1060 self.__error(docstringContext.end(), 0, "D238")
1058 return 1061 return
1059 if argNames + kwNames != args: 1062 if argNames + kwNames != args:
1060 self.__error(docstringContext.end(), 0, "D239") 1063 self.__error(docstringContext.end(), 0, "D239")
1061 1064
1065 def __checkEricException(self, docstringContext, context):
1066 """
1067 Private method to check, that docstrings contain an &#64;exception line
1068 if they raise an exception and don't otherwise.
1069
1070 @param docstringContext docstring context (Pep257Context)
1071 @param context context of the docstring (Pep257Context)
1072 """
1073 if docstringContext is None or self.__isScript:
1074 return
1075
1076 tokens = list(
1077 tokenize.generate_tokens(StringIO(context.ssource()).readline))
1078 exception = [tokens[i + 1][0] for i, token in enumerate(tokens)
1079 if token[1] == "raise"]
1080 if "@exception" not in docstringContext.ssource() and \
1081 "@throws" not in docstringContext.ssource() and \
1082 "@raise" not in docstringContext.ssource():
1083 if (set(exception) -
1084 set([tokenize.COMMENT, tokenize.NL, tokenize.NEWLINE]) !=
1085 set([])):
1086 self.__error(docstringContext.end(), 0, "D250")
1087 else:
1088 if (set(exception) -
1089 set([tokenize.COMMENT, tokenize.NL, tokenize.NEWLINE]) ==
1090 set([])):
1091 self.__error(docstringContext.end(), 0, "D251")
1092
1062 def __checkEricBlankAfterSummary(self, docstringContext, context): 1093 def __checkEricBlankAfterSummary(self, docstringContext, context):
1063 """ 1094 """
1064 Private method to check, that docstring summaries are followed 1095 Private method to check, that docstring summaries are followed
1065 by a blank line. 1096 by a blank line.
1066 1097

eric ide

mercurial