src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Logging/LoggingChecker.py

branch
eric7
changeset 11150
73d80859079c
parent 11147
dee6e106b4d3
equal deleted inserted replaced
11149:fc45672fae42 11150:73d80859079c
5 5
6 """ 6 """
7 Module implementing a checker for logging related issues. 7 Module implementing a checker for logging related issues.
8 """ 8 """
9 9
10 import copy 10 from CodeStyleTopicChecker import CodeStyleTopicChecker
11 11
12 12
13 class LoggingChecker: 13 class LoggingChecker(CodeStyleTopicChecker):
14 """ 14 """
15 Class implementing a checker for logging related issues. 15 Class implementing a checker for logging related issues.
16 """ 16 """
17 17
18 Codes = [ 18 Codes = [
31 "L-112", 31 "L-112",
32 "L-113", 32 "L-113",
33 "L-114", 33 "L-114",
34 "L-115", 34 "L-115",
35 ] 35 ]
36 Category = "L"
36 37
37 def __init__(self, source, filename, tree, select, ignore, expected, repeat, args): 38 def __init__(self, source, filename, tree, select, ignore, expected, repeat, args):
38 """ 39 """
39 Constructor 40 Constructor
40 41
53 @param repeat flag indicating to report each occurrence of a code 54 @param repeat flag indicating to report each occurrence of a code
54 @type bool 55 @type bool
55 @param args dictionary of arguments for the various checks 56 @param args dictionary of arguments for the various checks
56 @type dict 57 @type dict
57 """ 58 """
58 self.__select = tuple(select) 59 super().__init__(
59 self.__ignore = tuple(ignore) 60 LoggingChecker.Category,
60 self.__expected = expected[:] 61 source,
61 self.__repeat = repeat 62 filename,
62 self.__filename = filename 63 tree,
63 self.__source = source[:] 64 select,
64 self.__tree = copy.deepcopy(tree) 65 ignore,
65 self.__args = args 66 expected,
66 67 repeat,
67 # statistics counters 68 args,
68 self.counters = {} 69 )
69
70 # collection of detected errors
71 self.errors = []
72 70
73 checkersWithCodes = [ 71 checkersWithCodes = [
74 ( 72 (
75 self.__checkLogging, 73 self.__checkLogging,
76 ( 74 (
90 "L-114", 88 "L-114",
91 "L-115", 89 "L-115",
92 ), 90 ),
93 ), 91 ),
94 ] 92 ]
95 93 self._initializeCheckers(checkersWithCodes)
96 self.__checkers = []
97 for checker, codes in checkersWithCodes:
98 if any(not (code and self.__ignoreCode(code)) for code in codes):
99 self.__checkers.append(checker)
100
101 def __ignoreCode(self, code):
102 """
103 Private method to check if the message code should be ignored.
104
105 @param code message code to check for
106 @type str
107 @return flag indicating to ignore the given code
108 @rtype bool
109 """
110 return code in self.__ignore or (
111 code.startswith(self.__ignore) and not code.startswith(self.__select)
112 )
113
114 def __error(self, lineNumber, offset, code, *args):
115 """
116 Private method to record an issue.
117
118 @param lineNumber line number of the issue
119 @type int
120 @param offset position within line of the issue
121 @type int
122 @param code message code
123 @type str
124 @param args arguments for the message
125 @type list
126 """
127 if self.__ignoreCode(code):
128 return
129
130 if code in self.counters:
131 self.counters[code] += 1
132 else:
133 self.counters[code] = 1
134
135 # Don't care about expected codes
136 if code in self.__expected:
137 return
138
139 if code and (self.counters[code] == 1 or self.__repeat):
140 # record the issue with one based line number
141 self.errors.append(
142 {
143 "file": self.__filename,
144 "line": lineNumber + 1,
145 "offset": offset,
146 "code": code,
147 "args": args,
148 }
149 )
150
151 def run(self):
152 """
153 Public method to check the given source against miscellaneous
154 conditions.
155 """
156 if not self.__filename:
157 # don't do anything, if essential data is missing
158 return
159
160 if not self.__checkers:
161 # don't do anything, if no codes were selected
162 return
163
164 for check in self.__checkers:
165 check()
166 94
167 def __checkLogging(self): 95 def __checkLogging(self):
168 """ 96 """
169 Private method to check logging statements. 97 Private method to check logging statements.
170 """ 98 """
171 from .LoggingVisitor import LoggingVisitor 99 from .LoggingVisitor import LoggingVisitor
172 100
173 visitor = LoggingVisitor(errorCallback=self.__error) 101 visitor = LoggingVisitor(errorCallback=self.addErrorFromNode)
174 visitor.visit(self.__tree) 102 visitor.visit(self.tree)

eric ide

mercurial