Fixed some code style and formatting issues as well as an implementation issue.
10362
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
1
|
# -*- coding: utf-8 -*- |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
2
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
3
|
# Copyright (c) 2023 Detlev Offenbach <detlev@die-offenbachs.de> |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
4
|
# |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
5
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
6
|
""" |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
7
|
Module implementing a checker for logging related issues. |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
8
|
""" |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
9
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
10
|
import copy |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
11
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
12
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
13
|
class LoggingChecker: |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
14
|
""" |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
15
|
Class implementing a checker for logging related issues. |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
16
|
""" |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
17
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
18
|
Codes = [ |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
19
|
## Logging format |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
20
|
"L101", |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
21
|
"L102", |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
22
|
"L103", |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
23
|
"L104", |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
24
|
"L110", |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
25
|
] |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
26
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
27
|
def __init__(self, source, filename, tree, select, ignore, expected, repeat, args): |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
28
|
""" |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
29
|
Constructor |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
30
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
31
|
@param source source code to be checked |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
32
|
@type list of str |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
33
|
@param filename name of the source file |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
34
|
@type str |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
35
|
@param tree AST tree of the source code |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
36
|
@type ast.Module |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
37
|
@param select list of selected codes |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
38
|
@type list of str |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
39
|
@param ignore list of codes to be ignored |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
40
|
@type list of str |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
41
|
@param expected list of expected codes |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
42
|
@type list of str |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
43
|
@param repeat flag indicating to report each occurrence of a code |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
44
|
@type bool |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
45
|
@param args dictionary of arguments for the various checks |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
46
|
@type dict |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
47
|
""" |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
48
|
self.__select = tuple(select) |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
49
|
self.__ignore = ("",) if select else tuple(ignore) |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
50
|
self.__expected = expected[:] |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
51
|
self.__repeat = repeat |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
52
|
self.__filename = filename |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
53
|
self.__source = source[:] |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
54
|
self.__tree = copy.deepcopy(tree) |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
55
|
self.__args = args |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
56
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
57
|
# statistics counters |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
58
|
self.counters = {} |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
59
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
60
|
# collection of detected errors |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
61
|
self.errors = [] |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
62
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
63
|
checkersWithCodes = [ |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
64
|
(self.__checkLoggingFormat, ("L101", "L102", "L103", "L104", "L110")), |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
65
|
] |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
66
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
67
|
self.__checkers = [] |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
68
|
for checker, codes in checkersWithCodes: |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
69
|
if any(not (code and self.__ignoreCode(code)) for code in codes): |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
70
|
self.__checkers.append(checker) |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
71
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
72
|
def __ignoreCode(self, code): |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
73
|
""" |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
74
|
Private method to check if the message code should be ignored. |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
75
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
76
|
@param code message code to check for |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
77
|
@type str |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
78
|
@return flag indicating to ignore the given code |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
79
|
@rtype bool |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
80
|
""" |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
81
|
return code.startswith(self.__ignore) and not code.startswith(self.__select) |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
82
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
83
|
def __error(self, lineNumber, offset, code, *args): |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
84
|
""" |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
85
|
Private method to record an issue. |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
86
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
87
|
@param lineNumber line number of the issue |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
88
|
@type int |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
89
|
@param offset position within line of the issue |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
90
|
@type int |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
91
|
@param code message code |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
92
|
@type str |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
93
|
@param args arguments for the message |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
94
|
@type list |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
95
|
""" |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
96
|
if self.__ignoreCode(code): |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
97
|
return |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
98
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
99
|
if code in self.counters: |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
100
|
self.counters[code] += 1 |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
101
|
else: |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
102
|
self.counters[code] = 1 |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
103
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
104
|
# Don't care about expected codes |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
105
|
if code in self.__expected: |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
106
|
return |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
107
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
108
|
if code and (self.counters[code] == 1 or self.__repeat): |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
109
|
# record the issue with one based line number |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
110
|
self.errors.append( |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
111
|
{ |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
112
|
"file": self.__filename, |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
113
|
"line": lineNumber + 1, |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
114
|
"offset": offset, |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
115
|
"code": code, |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
116
|
"args": args, |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
117
|
} |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
118
|
) |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
119
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
120
|
def run(self): |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
121
|
""" |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
122
|
Public method to check the given source against miscellaneous |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
123
|
conditions. |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
124
|
""" |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
125
|
if not self.__filename: |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
126
|
# don't do anything, if essential data is missing |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
127
|
return |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
128
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
129
|
if not self.__checkers: |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
130
|
# don't do anything, if no codes were selected |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
131
|
return |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
132
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
133
|
for check in self.__checkers: |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
134
|
check() |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
135
|
|
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
136
|
def __checkLoggingFormat(self): |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
137
|
""" |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
138
|
Private method to check logging statements. |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
139
|
""" |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
140
|
from .LoggingFormatVisitor import LoggingFormatVisitor |
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
141
|
|
10363
6244c89dbc3f
Fixed some code style and formatting issues as well as an implementation issue.
Detlev Offenbach <detlev@die-offenbachs.de>
diff
changeset
|
142
|
visitor = LoggingFormatVisitor(errorCallback=self.__error) |
10362
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
143
|
visitor.visit(self.__tree) |