Plugins/CheckerPlugins/CodeStyleChecker/NamingStyleChecker.py

Thu, 10 Oct 2013 19:03:45 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 10 Oct 2013 19:03:45 +0200
changeset 3004
c4bf32c791d0
parent 2984
031cceaa8b01
child 3022
57179e4cdadd
child 3057
10516539f238
permissions
-rw-r--r--

Continued to shorten the code lines to max. 79 characters.

2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2013 Detlev Offenbach <detlev@die-offenbachs.de>
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
2984
031cceaa8b01 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2982
diff changeset
7 Module implementing a checker for naming conventions.
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import collections
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import ast
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import re
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 import os
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 from PyQt4.QtCore import QT_TRANSLATE_NOOP, QCoreApplication
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17
2979
e2eee1b09664 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2975
diff changeset
18 class NamingStyleChecker(object):
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 """
2984
031cceaa8b01 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2982
diff changeset
20 Class implementing a checker for naming conventions.
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 LowercaseRegex = re.compile(r"[_a-z][_a-z0-9]*$")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 UppercaseRegexp = re.compile(r"[_A-Z][_A-Z0-9]*$")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 CamelcaseRegexp = re.compile(r"_?[A-Z][a-zA-Z0-9]*$")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 MixedcaseRegexp = re.compile(r"_?[a-z][a-zA-Z0-9]*$")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 Codes = [
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 "N801", "N802", "N803", "N804", "N805", "N806", "N807", "N808",
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 "N811", "N812", "N813", "N814", "N821", "N831"
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 ]
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 Messages = {
2979
e2eee1b09664 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2975
diff changeset
32 "N801": QT_TRANSLATE_NOOP("NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 "class names should use CapWords convention"),
2979
e2eee1b09664 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2975
diff changeset
34 "N802": QT_TRANSLATE_NOOP("NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 "function name should be lowercase"),
2979
e2eee1b09664 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2975
diff changeset
36 "N803": QT_TRANSLATE_NOOP("NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 "argument name should be lowercase"),
2979
e2eee1b09664 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2975
diff changeset
38 "N804": QT_TRANSLATE_NOOP("NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 "first argument of a class method should be named 'cls'"),
2979
e2eee1b09664 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2975
diff changeset
40 "N805": QT_TRANSLATE_NOOP("NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 "first argument of a method should be named 'self'"),
2979
e2eee1b09664 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2975
diff changeset
42 "N806": QT_TRANSLATE_NOOP("NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 "first argument of a static method should not be named"
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 " 'self' or 'cls"),
2979
e2eee1b09664 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2975
diff changeset
45 "N807": QT_TRANSLATE_NOOP("NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 "module names should be lowercase"),
2979
e2eee1b09664 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2975
diff changeset
47 "N808": QT_TRANSLATE_NOOP("NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 "package names should be lowercase"),
2979
e2eee1b09664 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2975
diff changeset
49 "N811": QT_TRANSLATE_NOOP("NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 "constant imported as non constant"),
2979
e2eee1b09664 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2975
diff changeset
51 "N812": QT_TRANSLATE_NOOP("NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 "lowercase imported as non lowercase"),
2979
e2eee1b09664 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2975
diff changeset
53 "N813": QT_TRANSLATE_NOOP("NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 "camelcase imported as lowercase"),
2979
e2eee1b09664 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2975
diff changeset
55 "N814": QT_TRANSLATE_NOOP("NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 "camelcase imported as constant"),
2979
e2eee1b09664 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2975
diff changeset
57 "N821": QT_TRANSLATE_NOOP("NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 "variable in function should be lowercase"),
2979
e2eee1b09664 Continued changing the names of the various code style checkers to make them more appropriate to the broadened scope.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2975
diff changeset
59 "N831": QT_TRANSLATE_NOOP("NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 "names 'l', 'O' and 'I' should be avoided"),
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 }
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
63 def __init__(self, tree, filename, options):
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 """
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
65 Constructor (according to 'extended' pep8.py API)
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 @param tree AST tree of the source file
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 @param filename name of the source file (string)
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
69 @param options options as parsed by pep8.StyleGuide
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 self.__parents = collections.deque()
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 self.__tree = tree
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 self.__filename = filename
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
75 self.__checkersWithCodes = {
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
76 "classdef": [
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
77 (self.__checkClassName, ("N801",)),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
78 (self.__checkNameToBeAvoided, ("N831",)),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
79 ],
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
80 "functiondef": [
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
81 (self.__checkFuntionName, ("N802",)),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
82 (self.__checkFunctionArgumentNames,
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
83 ("N803", "N804", "N805", "N806")),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
84 (self.__checkNameToBeAvoided, ("N831",)),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
85 ],
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
86 "assign": [
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
87 (self.__checkVariablesInFunction, ("N821",)),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
88 (self.__checkNameToBeAvoided, ("N831",)),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
89 ],
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
90 "importfrom": [
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
91 (self.__checkImportAs, ("N811", "N812", "N813", "N814")),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
92 ],
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
93 "module": [
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
94 (self.__checkModule, ("N807", "N808")),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
95 ],
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 }
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
97
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
98 self.__checkers = {}
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
99 for key, checkers in self.__checkersWithCodes.items():
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
100 for checker, codes in checkers:
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
101 if any(not (code and options.ignore_code(code))
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
102 for code in codes):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
103 if key not in self.__checkers:
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
104 self.__checkers[key] = []
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
105 self.__checkers[key].append(checker)
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 def run(self):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 Public method run by the pep8.py checker.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 @return tuple giving line number, offset within line, code and
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 checker function
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 """
2975
fb336783a649 A little optimization to the naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2962
diff changeset
114 if self.__tree and self.__checkers:
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 return self.__visitTree(self.__tree)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 else:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 return ()
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 @classmethod
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 def getMessage(cls, code, *args):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 Class method to get a translated and formatted message for a
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 given code.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 @param code message code (string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 @param args arguments for a formatted message (list)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 @return translated and formatted message (string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 if code in cls.Messages:
3004
c4bf32c791d0 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2984
diff changeset
130 return code + " " + QCoreApplication.translate(
c4bf32c791d0 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2984
diff changeset
131 "NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 cls.Messages[code]).format(*args)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 else:
3004
c4bf32c791d0 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2984
diff changeset
134 return code + " " + QCoreApplication.translate(
c4bf32c791d0 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2984
diff changeset
135 "NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 "no message for this code defined")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 def __visitTree(self, node):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 Private method to scan the given AST tree.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 @param node AST tree node to scan
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 @return tuple giving line number, offset within line, code and
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 checker function
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 for error in self.__visitNode(node):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 yield error
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 self.__parents.append(node)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 for child in ast.iter_child_nodes(node):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 for error in self.__visitTree(child):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 yield error
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 self.__parents.pop()
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 def __visitNode(self, node):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 Private method to inspect the given AST node.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 @param node AST tree node to inspect
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 @return tuple giving line number, offset within line, code and
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 checker function
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 if isinstance(node, ast.ClassDef):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 self.__tagClassFunctions(node)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 elif isinstance(node, ast.FunctionDef):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 self.__findGlobalDefs(node)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 checkerName = node.__class__.__name__.lower()
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 if checkerName in self.__checkers:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 for checker in self.__checkers[checkerName]:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 for error in checker(node, self.__parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 yield error + (self.__checkers[checkerName],)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 def __tagClassFunctions(self, classNode):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 Private method to tag functions if they are methods, class methods or
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 static methods.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 @param classNode AST tree node to tag
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 # try to find all 'old style decorators' like
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 # m = staticmethod(m)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 lateDecoration = {}
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 for node in ast.iter_child_nodes(classNode):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 if not (isinstance(node, ast.Assign) and
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 isinstance(node.value, ast.Call) and
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 isinstance(node.value.func, ast.Name)):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 continue
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 funcName = node.value.func.id
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 if funcName in ("classmethod", "staticmethod"):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 meth = (len(node.value.args) == 1 and node.value.args[0])
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 if isinstance(meth, ast.Name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 lateDecoration[meth.id] = funcName
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 # iterate over all functions and tag them
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 for node in ast.iter_child_nodes(classNode):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 if not isinstance(node, ast.FunctionDef):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 continue
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 node.function_type = 'method'
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 if node.name == "__new__":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 node.function_type = "classmethod"
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 if node.name in lateDecoration:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 node.function_type = lateDecoration[node.name]
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 elif node.decorator_list:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 names = [d.id for d in node.decorator_list
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 if isinstance(d, ast.Name) and
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 d.id in ("classmethod", "staticmethod")]
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 if names:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 node.function_type = names[0]
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 def __findGlobalDefs(self, functionNode):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 Private method amend a node with global definitions information.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 @param functionNode AST tree node to amend
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 globalNames = set()
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 nodesToCheck = collections.deque(ast.iter_child_nodes(functionNode))
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 while nodesToCheck:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 node = nodesToCheck.pop()
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 if isinstance(node, ast.Global):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 globalNames.update(node.names)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 if not isinstance(node, (ast.FunctionDef, ast.ClassDef)):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 nodesToCheck.extend(ast.iter_child_nodes(node))
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 functionNode.global_names = globalNames
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 def __getArgNames(self, node):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 Private method to get the argument names of a function node.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 @param node AST node to extract arguments names from
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 @return list of argument names (list of string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 posArgs = [arg.arg for arg in node.args.args]
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 kwOnly = [arg.arg for arg in node.args.kwonlyargs]
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 return posArgs + kwOnly
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 def __error(self, node, code):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 """
2962
d6c9d1ca2da4 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2910
diff changeset
242 Private method to build the error information.
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 @param node AST node to report an error for
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 @param code error code to report (string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 @return tuple giving line number, offset within line and error code
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 if isinstance(node, ast.Module):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 lineno = 0
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 offset = 0
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 else:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 lineno = node.lineno
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 offset = node.col_offset
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 if isinstance(node, ast.ClassDef):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 lineno += len(node.decorator_list)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 offset += 6
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 elif isinstance(node, ast.FunctionDef):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 lineno += len(node.decorator_list)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 offset += 4
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261 return (lineno, offset, code)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 def __isNameToBeAvoided(self, name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 Private method to check, if the given name should be avoided.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 @param name name to be checked (string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 @return flag indicating to avoid it (boolen)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 return name in ("l", "O", "I")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
272 def __checkNameToBeAvoided(self, node, parents):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
273 """
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
274 Private class to check the given node for a name to be avoided (N831).
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
275
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
276 @param node AST note to check
2962
d6c9d1ca2da4 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2910
diff changeset
277 @param parents list of parent nodes
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
278 @return tuple giving line number, offset within line and error code
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
279 (integer, integer, string)
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
280 """
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
281 if isinstance(node, (ast.ClassDef, ast.FunctionDef)):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
282 name = node.name
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
283 if self.__isNameToBeAvoided(name):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
284 yield self.__error(node, "N831")
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
285 return
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
286
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
287 if isinstance(node, ast.FunctionDef):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
288 argNames = self.__getArgNames(node)
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
289 for arg in argNames:
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
290 if self.__isNameToBeAvoided(arg):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
291 yield self.__error(node, "N831")
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
292 return
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
293
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
294 if isinstance(node, ast.Assign):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
295 for target in node.targets:
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
296 name = isinstance(target, ast.Name) and target.id
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
297 if not name:
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
298 return
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
299
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
300 if self.__isNameToBeAvoided(name):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
301 yield self.__error(node, "N831")
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
302 return
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
303
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304 def __checkClassName(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
306 Private class to check the given node for class name
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
307 conventions (N801).
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
309 Almost without exception, class names use the CapWords convention.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310 Classes for internal use have a leading underscore in addition.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
311
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
312 @param node AST note to check
2962
d6c9d1ca2da4 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2910
diff changeset
313 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
314 @return tuple giving line number, offset within line and error code
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
315 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
316 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
317 if not self.CamelcaseRegexp.match(node.name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
318 yield self.__error(node, "N801")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
319
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
320 def __checkFuntionName(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
321 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
322 Private class to check the given node for function name
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
323 conventions (N802).
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
324
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
325 Function names should be lowercase, with words separated by underscores
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
326 as necessary to improve readability. Functions <b>not</b> being
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
327 methods '__' in front and back are not allowed. Mixed case is allowed
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328 only in contexts where that's already the prevailing style
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329 (e.g. threading.py), to retain backwards compatibility.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
330
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
331 @param node AST note to check
2962
d6c9d1ca2da4 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2910
diff changeset
332 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
333 @return tuple giving line number, offset within line and error code
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
334 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
336 functionType = getattr(node, "function_type", "function")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
337 name = node.name
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
338 if (functionType == "function" and "__" in (name[:2], name[-2:])) or \
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339 not self.LowercaseRegex.match(name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340 yield self.__error(node, "N802")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 def __checkFunctionArgumentNames(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
343 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344 Private class to check the argument names of functions
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
345 (N803, N804, N805, N806).
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347 The argument names of a function should be lowercase, with words
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348 separated by underscores. A class method should have 'cls' as the
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 first argument. A method should have 'self' as the first argument.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351 @param node AST note to check
2962
d6c9d1ca2da4 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2910
diff changeset
352 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
353 @return tuple giving line number, offset within line and error code
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
355 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356 if node.args.kwarg is not None:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357 if not self.LowercaseRegex.match(node.args.kwarg):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 yield self.__error(node, "N803")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
361 if node.args.vararg is not None:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362 if not self.LowercaseRegex.match(node.args.vararg):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363 yield self.__error(node, "N803")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
364 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
366 argNames = self.__getArgNames(node)
2895
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
367 functionType = getattr(node, "function_type", "function")
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
368
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
369 if not argNames:
2895
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
370 if functionType == "method":
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
371 yield self.__error(node, "N805")
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
372 elif functionType == "classmethod":
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
373 yield self.__error(node, "N804")
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
374 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376 if functionType == "method":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377 if argNames[0] != "self":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378 yield self.__error(node, "N805")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
379 elif functionType == "classmethod":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380 if argNames[0] != "cls":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
381 yield self.__error(node, "N804")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
382 elif functionType == "staticmethod":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
383 if argNames[0] in ("cls", "self"):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
384 yield self.__error(node, "N806")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385 for arg in argNames:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
386 if not self.LowercaseRegex.match(arg):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
387 yield self.__error(node, "N803")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
388 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
389
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
390 def __checkVariablesInFunction(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
391 """
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
392 Private method to check local variables in functions (N821).
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
394 Local variables in functions should be lowercase.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
395
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
396 @param node AST note to check
2962
d6c9d1ca2da4 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2910
diff changeset
397 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
398 @return tuple giving line number, offset within line and error code
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
399 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
400 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
401 for parentFunc in reversed(parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
402 if isinstance(parentFunc, ast.ClassDef):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
403 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
404 if isinstance(parentFunc, ast.FunctionDef):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
405 break
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
406 else:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
407 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
408 for target in node.targets:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
409 name = isinstance(target, ast.Name) and target.id
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
410 if not name or name in parentFunc.global_names:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
411 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
412
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
413 if not self.LowercaseRegex.match(name) and name[:1] != '_':
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
414 yield self.__error(target, "N821")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
415
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
416 def __checkModule(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418 Private method to check module naming conventions (N807, N808).
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
419
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
420 Module and package names should be lowercase.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
421
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
422 @param node AST note to check
2962
d6c9d1ca2da4 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2910
diff changeset
423 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
424 @return tuple giving line number, offset within line and error code
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427 if self.__filename:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
428 moduleName = os.path.splitext(os.path.basename(self.__filename))[0]
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429 if moduleName.lower() != moduleName:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
430 yield self.__error(node, "N807")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
432 if moduleName == "__init__":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433 # we got a package
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
434 packageName = \
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
435 os.path.split(os.path.dirname(self.__filename))[1]
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
436 if packageName.lower != packageName:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
437 yield self.__error(node, "N808")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
438
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
439 def __checkImportAs(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
440 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
441 Private method to check that imports don't change the
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
442 naming convention (N811, N812, N813, N814).
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
443
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
444 @param node AST note to check
2962
d6c9d1ca2da4 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2910
diff changeset
445 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
446 @return tuple giving line number, offset within line and error code
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
447 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
448 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
449 for name in node.names:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
450 if not name.asname:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
451 continue
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
452
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
453 if self.UppercaseRegexp.match(name.name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
454 if not self.UppercaseRegexp.match(name.asname):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
455 yield self.__error(node, "N811")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
456 elif self.LowercaseRegex.match(name.name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
457 if not self.LowercaseRegex.match(name.asname):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
458 yield self.__error(node, "N812")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
459 elif self.LowercaseRegex.match(name.asname):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
460 yield self.__error(node, "N813")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
461 elif self.UppercaseRegexp.match(name.asname):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
462 yield self.__error(node, "N814")

eric ide

mercurial