eric7/Plugins/CheckerPlugins/CodeStyleChecker/Imports/ImportsChecker.py

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

eric ide

mercurial