Plugins/CheckerPlugins/CodeStyleChecker/NamingStyleChecker.py

branch
Py2 comp.
changeset 3456
96232974dcdb
parent 3178
f25fc1364c88
parent 3413
5e63f809732a
child 3646
cfbb47b6d885
equal deleted inserted replaced
3178:f25fc1364c88 3456:96232974dcdb
4 # 4 #
5 5
6 """ 6 """
7 Module implementing a checker for naming conventions. 7 Module implementing a checker for naming conventions.
8 """ 8 """
9
10 from __future__ import unicode_literals
11 9
12 import collections 10 import collections
13 import ast 11 import ast
14 import re 12 import re
15 import os 13 import os
16 14 import sys
17 from PyQt4.QtCore import QT_TRANSLATE_NOOP, QCoreApplication
18 15
19 16
20 class NamingStyleChecker(object): 17 class NamingStyleChecker(object):
21 """ 18 """
22 Class implementing a checker for naming conventions. 19 Class implementing a checker for naming conventions.
28 25
29 Codes = [ 26 Codes = [
30 "N801", "N802", "N803", "N804", "N805", "N806", "N807", "N808", 27 "N801", "N802", "N803", "N804", "N805", "N806", "N807", "N808",
31 "N811", "N812", "N813", "N814", "N821", "N831" 28 "N811", "N812", "N813", "N814", "N821", "N831"
32 ] 29 ]
33 Messages = {
34 "N801": QT_TRANSLATE_NOOP(
35 "NamingStyleChecker",
36 "class names should use CapWords convention"),
37 "N802": QT_TRANSLATE_NOOP(
38 "NamingStyleChecker",
39 "function name should be lowercase"),
40 "N803": QT_TRANSLATE_NOOP(
41 "NamingStyleChecker",
42 "argument name should be lowercase"),
43 "N804": QT_TRANSLATE_NOOP(
44 "NamingStyleChecker",
45 "first argument of a class method should be named 'cls'"),
46 "N805": QT_TRANSLATE_NOOP(
47 "NamingStyleChecker",
48 "first argument of a method should be named 'self'"),
49 "N806": QT_TRANSLATE_NOOP(
50 "NamingStyleChecker",
51 "first argument of a static method should not be named"
52 " 'self' or 'cls"),
53 "N807": QT_TRANSLATE_NOOP(
54 "NamingStyleChecker",
55 "module names should be lowercase"),
56 "N808": QT_TRANSLATE_NOOP(
57 "NamingStyleChecker",
58 "package names should be lowercase"),
59 "N811": QT_TRANSLATE_NOOP(
60 "NamingStyleChecker",
61 "constant imported as non constant"),
62 "N812": QT_TRANSLATE_NOOP(
63 "NamingStyleChecker",
64 "lowercase imported as non lowercase"),
65 "N813": QT_TRANSLATE_NOOP(
66 "NamingStyleChecker",
67 "camelcase imported as lowercase"),
68 "N814": QT_TRANSLATE_NOOP(
69 "NamingStyleChecker",
70 "camelcase imported as constant"),
71 "N821": QT_TRANSLATE_NOOP(
72 "NamingStyleChecker",
73 "variable in function should be lowercase"),
74 "N831": QT_TRANSLATE_NOOP(
75 "NamingStyleChecker",
76 "names 'l', 'O' and 'I' should be avoided"),
77 }
78 30
79 def __init__(self, tree, filename, options): 31 def __init__(self, tree, filename, options):
80 """ 32 """
81 Constructor (according to 'extended' pep8.py API) 33 Constructor (according to 'extended' pep8.py API)
82 34
130 if self.__tree and self.__checkers: 82 if self.__tree and self.__checkers:
131 return self.__visitTree(self.__tree) 83 return self.__visitTree(self.__tree)
132 else: 84 else:
133 return () 85 return ()
134 86
135 @classmethod
136 def getMessage(cls, code, *args):
137 """
138 Class method to get a translated and formatted message for a
139 given code.
140
141 @param code message code (string)
142 @param args arguments for a formatted message (list)
143 @return translated and formatted message (string)
144 """
145 if code in cls.Messages:
146 return code + " " + QCoreApplication.translate(
147 "NamingStyleChecker",
148 cls.Messages[code]).format(*args)
149 else:
150 return code + " " + QCoreApplication.translate(
151 "NamingStyleChecker",
152 "no message for this code defined")
153
154 def __visitTree(self, node): 87 def __visitTree(self, node):
155 """ 88 """
156 Private method to scan the given AST tree. 89 Private method to scan the given AST tree.
157 90
158 @param node AST tree node to scan 91 @param node AST tree node to scan
247 Private method to get the argument names of a function node. 180 Private method to get the argument names of a function node.
248 181
249 @param node AST node to extract arguments names from 182 @param node AST node to extract arguments names from
250 @return list of argument names (list of string) 183 @return list of argument names (list of string)
251 """ 184 """
252 posArgs = [arg.arg for arg in node.args.args] 185 if sys.version_info[0] == 3:
253 kwOnly = [arg.arg for arg in node.args.kwonlyargs] 186 posArgs = [arg.arg for arg in node.args.args]
254 return posArgs + kwOnly 187 kwOnly = [arg.arg for arg in node.args.kwonlyargs]
188 return posArgs + kwOnly
189 else:
190 def unpackArgs(args):
191 """
192 Local helper function to unpack function argument names.
193
194 @param args list of AST node arguments
195 @return list of argument names (list of string)
196 """
197 ret = []
198 for arg in args:
199 if isinstance(arg, ast.Tuple):
200 ret.extend(unpackArgs(arg.elts))
201 else:
202 ret.append(arg.id)
203 return ret
204
205 return unpackArgs(node.args.args)
255 206
256 def __error(self, node, code): 207 def __error(self, node, code):
257 """ 208 """
258 Private method to build the error information. 209 Private method to build the error information.
259 210

eric ide

mercurial