Plugins/CheckerPlugins/CodeStyleChecker/NamingStyleChecker.py

Fri, 01 Nov 2013 15:48:48 +0100

author
T.Rzepka <Tobias.Rzepka@gmail.com>
date
Fri, 01 Nov 2013 15:48:48 +0100
branch
Py2 comp.
changeset 3058
0a02c433f52d
parent 3057
10516539f238
parent 3022
57179e4cdadd
child 3145
a9de05d4a22f
permissions
-rw-r--r--

Merge with default branch after fixed indentation issues.

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
3056
9986ec0e559a Merge with default branch before style changes.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2910
diff changeset
10 from __future__ import unicode_literals # __IGNORE_WARNING__
9986ec0e559a Merge with default branch before style changes.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2910
diff changeset
11
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import collections
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 import ast
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 import re
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 import os
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 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
18
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19
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
20 class NamingStyleChecker(object):
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 """
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
22 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
23 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 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
25 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
26 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
27 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
28
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 Codes = [
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 "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
31 "N811", "N812", "N813", "N814", "N821", "N831"
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 ]
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 Messages = {
3022
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
34 "N801": QT_TRANSLATE_NOOP(
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
35 "NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 "class names should use CapWords convention"),
3022
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
37 "N802": QT_TRANSLATE_NOOP(
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
38 "NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 "function name should be lowercase"),
3022
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
40 "N803": QT_TRANSLATE_NOOP(
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
41 "NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 "argument name should be lowercase"),
3022
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
43 "N804": QT_TRANSLATE_NOOP(
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
44 "NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 "first argument of a class method should be named 'cls'"),
3022
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
46 "N805": QT_TRANSLATE_NOOP(
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
47 "NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 "first argument of a method should be named 'self'"),
3022
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
49 "N806": QT_TRANSLATE_NOOP(
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
50 "NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 "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
52 " 'self' or 'cls"),
3022
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
53 "N807": QT_TRANSLATE_NOOP(
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
54 "NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 "module names should be lowercase"),
3022
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
56 "N808": QT_TRANSLATE_NOOP(
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
57 "NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 "package names should be lowercase"),
3022
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
59 "N811": QT_TRANSLATE_NOOP(
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
60 "NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 "constant imported as non constant"),
3022
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
62 "N812": QT_TRANSLATE_NOOP(
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
63 "NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 "lowercase imported as non lowercase"),
3022
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
65 "N813": QT_TRANSLATE_NOOP(
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
66 "NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 "camelcase imported as lowercase"),
3022
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
68 "N814": QT_TRANSLATE_NOOP(
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
69 "NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 "camelcase imported as constant"),
3022
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
71 "N821": QT_TRANSLATE_NOOP(
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
72 "NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 "variable in function should be lowercase"),
3022
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
74 "N831": QT_TRANSLATE_NOOP(
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3004
diff changeset
75 "NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 "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
77 }
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
79 def __init__(self, tree, filename, options):
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 """
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
81 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
82
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 @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
84 @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
85 @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
86 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 self.__parents = collections.deque()
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 self.__tree = tree
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 self.__filename = filename
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
91 self.__checkersWithCodes = {
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
92 "classdef": [
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
93 (self.__checkClassName, ("N801",)),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
94 (self.__checkNameToBeAvoided, ("N831",)),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
95 ],
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
96 "functiondef": [
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
97 (self.__checkFuntionName, ("N802",)),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
98 (self.__checkFunctionArgumentNames,
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
99 ("N803", "N804", "N805", "N806")),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
100 (self.__checkNameToBeAvoided, ("N831",)),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
101 ],
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
102 "assign": [
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
103 (self.__checkVariablesInFunction, ("N821",)),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
104 (self.__checkNameToBeAvoided, ("N831",)),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
105 ],
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
106 "importfrom": [
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
107 (self.__checkImportAs, ("N811", "N812", "N813", "N814")),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
108 ],
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
109 "module": [
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
110 (self.__checkModule, ("N807", "N808")),
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
111 ],
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 }
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
113
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
114 self.__checkers = {}
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
115 for key, checkers in self.__checkersWithCodes.items():
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
116 for checker, codes in checkers:
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
117 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
118 for code in codes):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
119 if key not in self.__checkers:
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
120 self.__checkers[key] = []
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
121 self.__checkers[key].append(checker)
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 def run(self):
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 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
126
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 @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
128 checker function
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 """
2975
fb336783a649 A little optimization to the naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2962
diff changeset
130 if self.__tree and self.__checkers:
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 return self.__visitTree(self.__tree)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 else:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 return ()
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 @classmethod
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 def getMessage(cls, code, *args):
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 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
139 given code.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 @param code message code (string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 @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
143 @return translated and formatted message (string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 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
146 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
147 "NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 cls.Messages[code]).format(*args)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 else:
3004
c4bf32c791d0 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2984
diff changeset
150 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
151 "NamingStyleChecker",
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 "no message for this code defined")
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 __visitTree(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 scan the given AST tree.
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 scan
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 for error in self.__visitNode(node):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 yield error
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 self.__parents.append(node)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 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
166 for error in self.__visitTree(child):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 yield error
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 self.__parents.pop()
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 def __visitNode(self, node):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 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
173
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 @param node AST tree node to inspect
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 @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
176 checker function
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 if isinstance(node, ast.ClassDef):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 self.__tagClassFunctions(node)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 elif isinstance(node, ast.FunctionDef):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 self.__findGlobalDefs(node)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 checkerName = node.__class__.__name__.lower()
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 if checkerName in self.__checkers:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 for checker in self.__checkers[checkerName]:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 for error in checker(node, self.__parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 yield error + (self.__checkers[checkerName],)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 def __tagClassFunctions(self, classNode):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 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
192 static methods.
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 @param classNode AST tree node to tag
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 # 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
197 # m = staticmethod(m)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 lateDecoration = {}
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 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
200 if not (isinstance(node, ast.Assign) and
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 isinstance(node.value, ast.Call) and
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 isinstance(node.value.func, ast.Name)):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 continue
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 funcName = node.value.func.id
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 if funcName in ("classmethod", "staticmethod"):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 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
207 if isinstance(meth, ast.Name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 lateDecoration[meth.id] = funcName
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 # iterate over all functions and tag them
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 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
212 if not isinstance(node, ast.FunctionDef):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 continue
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 node.function_type = 'method'
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 if node.name == "__new__":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 node.function_type = "classmethod"
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 if node.name in lateDecoration:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 node.function_type = lateDecoration[node.name]
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 elif node.decorator_list:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 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
223 if isinstance(d, ast.Name) and
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 d.id in ("classmethod", "staticmethod")]
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 if names:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 node.function_type = names[0]
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 def __findGlobalDefs(self, functionNode):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 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
231
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 @param functionNode AST tree node to amend
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 globalNames = set()
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 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
236 while nodesToCheck:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 node = nodesToCheck.pop()
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 if isinstance(node, ast.Global):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 globalNames.update(node.names)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 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
242 nodesToCheck.extend(ast.iter_child_nodes(node))
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 functionNode.global_names = globalNames
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 def __getArgNames(self, node):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 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
248
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 @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
250 @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
251 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 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
253 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
254 return posArgs + kwOnly
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 def __error(self, node, code):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 """
2962
d6c9d1ca2da4 Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2910
diff changeset
258 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
259
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 @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
261 @param code error code to report (string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 @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
263 (integer, integer, string)
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 if isinstance(node, ast.Module):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266 lineno = 0
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 offset = 0
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 else:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 lineno = node.lineno
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 offset = node.col_offset
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 if isinstance(node, ast.ClassDef):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 lineno += len(node.decorator_list)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 offset += 6
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 elif isinstance(node, ast.FunctionDef):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 lineno += len(node.decorator_list)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 offset += 4
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 return (lineno, offset, code)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 def __isNameToBeAvoided(self, name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 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
282
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283 @param name name to be checked (string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 @return flag indicating to avoid it (boolen)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286 return name in ("l", "O", "I")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
288 def __checkNameToBeAvoided(self, node, parents):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
289 """
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
290 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
291
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
292 @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
293 @param parents list of parent nodes
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
294 @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
295 (integer, integer, string)
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
296 """
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
297 if isinstance(node, (ast.ClassDef, ast.FunctionDef)):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
298 name = node.name
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
299 if self.__isNameToBeAvoided(name):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
300 yield self.__error(node, "N831")
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
301 return
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
302
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
303 if isinstance(node, ast.FunctionDef):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
304 argNames = self.__getArgNames(node)
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
305 for arg in argNames:
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
306 if self.__isNameToBeAvoided(arg):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
307 yield self.__error(node, "N831")
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
308 return
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
309
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
310 if isinstance(node, ast.Assign):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
311 for target in node.targets:
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
312 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
313 if not name:
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
314 return
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
315
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
316 if self.__isNameToBeAvoided(name):
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
317 yield self.__error(node, "N831")
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
318 return
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
319
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
320 def __checkClassName(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 class name
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
323 conventions (N801).
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 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
326 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
327
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328 @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
329 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
330 @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
331 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
332 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
333 if not self.CamelcaseRegexp.match(node.name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
334 yield self.__error(node, "N801")
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 def __checkFuntionName(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
337 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
338 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
339 conventions (N802).
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 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
342 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
343 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
344 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
345 (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
346
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347 @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
348 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 @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
350 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 functionType = getattr(node, "function_type", "function")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
353 name = node.name
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354 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
355 not self.LowercaseRegex.match(name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356 yield self.__error(node, "N802")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 def __checkFunctionArgumentNames(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 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
361 (N803, N804, N805, N806).
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363 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
364 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
365 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
366
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
367 @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
368 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
369 @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
370 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
371 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372 if node.args.kwarg is not None:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373 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
374 yield self.__error(node, "N803")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377 if node.args.vararg is not None:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378 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
379 yield self.__error(node, "N803")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
381
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
382 argNames = self.__getArgNames(node)
2895
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
383 functionType = getattr(node, "function_type", "function")
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
384
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385 if not argNames:
2895
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
386 if functionType == "method":
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
387 yield self.__error(node, "N805")
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
388 elif functionType == "classmethod":
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
389 yield self.__error(node, "N804")
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
390 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
391
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
392 if functionType == "method":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393 if argNames[0] != "self":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
394 yield self.__error(node, "N805")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
395 elif functionType == "classmethod":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
396 if argNames[0] != "cls":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
397 yield self.__error(node, "N804")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
398 elif functionType == "staticmethod":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
399 if argNames[0] in ("cls", "self"):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
400 yield self.__error(node, "N806")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
401 for arg in argNames:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
402 if not self.LowercaseRegex.match(arg):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
403 yield self.__error(node, "N803")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
404 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
405
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
406 def __checkVariablesInFunction(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
407 """
2910
cdc56e9d9f12 Optimized the PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2905
diff changeset
408 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
409
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
410 Local variables in functions should be lowercase.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
411
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
412 @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
413 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
414 @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
415 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
416 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417 for parentFunc in reversed(parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418 if isinstance(parentFunc, ast.ClassDef):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
419 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
420 if isinstance(parentFunc, ast.FunctionDef):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
421 break
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
422 else:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
423 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
424 for target in node.targets:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425 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
426 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
427 return
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
428
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429 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
430 yield self.__error(target, "N821")
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 def __checkModule(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
434 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
435
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
436 Module and package names should be lowercase.
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
437
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
438 @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
439 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
440 @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
441 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
442 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
443 if self.__filename:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
444 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
445 if moduleName.lower() != moduleName:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
446 yield self.__error(node, "N807")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
447
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
448 if moduleName == "__init__":
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
449 # we got a package
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
450 packageName = \
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
451 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
452 if packageName.lower != packageName:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
453 yield self.__error(node, "N808")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
454
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
455 def __checkImportAs(self, node, parents):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
456 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
457 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
458 naming convention (N811, N812, N813, N814).
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
459
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
460 @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
461 @param parents list of parent nodes
2894
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
462 @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
463 (integer, integer, string)
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
464 """
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
465 for name in node.names:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
466 if not name.asname:
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
467 continue
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
468
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
469 if self.UppercaseRegexp.match(name.name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
470 if not self.UppercaseRegexp.match(name.asname):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
471 yield self.__error(node, "N811")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
472 elif self.LowercaseRegex.match(name.name):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
473 if not self.LowercaseRegex.match(name.asname):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
474 yield self.__error(node, "N812")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
475 elif self.LowercaseRegex.match(name.asname):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
476 yield self.__error(node, "N813")
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
477 elif self.UppercaseRegexp.match(name.asname):
8e4264045fc9 Started implementing a PEP-8 naming checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
478 yield self.__error(node, "N814")

eric ide

mercurial