Thu, 22 Feb 2024 16:26:46 +0100
Implemented first stage of remote project support.
8166 | 1 | # -*- coding: utf-8 -*- |
2 | ||
10439
21c28b0f9e41
Updated copyright for 2024.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10175
diff
changeset
|
3 | # Copyright (c) 2021 - 2024 Detlev Offenbach <detlev@die-offenbachs.de> |
8166 | 4 | # |
5 | ||
6 | """ | |
7 | Module implementing the checker for functions that can be replaced by use of | |
8 | the pathlib module. | |
9 | """ | |
10 | ||
9595
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
11 | ##################################################################################### |
10175
57ed3cb66e9a
Changed some code comments of adapted third party packages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9653
diff
changeset
|
12 | ## adapted from: flake8-use-pathlib v0.3.0 ## |
9595
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
13 | ## ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
14 | ## Original: Copyright (c) 2021 Rodolphe Pelloux-Prayer ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
15 | ## ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
16 | ## License: ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
17 | ## Permission is hereby granted, free of charge, to any person obtaining a copy ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
18 | ## of this software and associated documentation files (the "Software"), to deal ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
19 | ## in the Software without restriction, including without limitation the rights ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
20 | ## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
21 | ## copies of the Software, and to permit persons to whom the Software is ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
22 | ## furnished to do so, subject to the following conditions: ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
23 | ## ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
24 | ## The above copyright notice and this permission notice shall be included in all ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
25 | ## copies or substantial portions of the Software. ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
26 | ## ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
27 | ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
28 | ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
29 | ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
30 | ## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
31 | ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
32 | ## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
33 | ##################################################################################### |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
34 | |
8166 | 35 | import ast |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
36 | import contextlib |
8198
1c765dc90c21
Code Style Checker: changed code such, that the AST tree is built only once and passed to each checker module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8166
diff
changeset
|
37 | import copy |
8166 | 38 | |
39 | ||
8207
d359172d11be
Applied some more code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8198
diff
changeset
|
40 | class PathlibChecker: |
8166 | 41 | """ |
42 | Class implementing a checker for functions that can be replaced by use of | |
43 | the pathlib module. | |
44 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
45 | |
8166 | 46 | Codes = [ |
47 | ## Replacements for the os module functions | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
48 | "P101", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
49 | "P102", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
50 | "P103", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
51 | "P104", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
52 | "P105", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
53 | "P106", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
54 | "P107", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
55 | "P108", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
56 | "P109", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
57 | "P110", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
58 | "P111", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
59 | "P112", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
60 | "P113", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
61 | "P114", |
8166 | 62 | ## Replacements for the os.path module functions |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
63 | "P201", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
64 | "P202", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
65 | "P203", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
66 | "P204", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
67 | "P205", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
68 | "P206", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
69 | "P207", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
70 | "P208", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
71 | "P209", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
72 | "P210", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
73 | "P211", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
74 | "P212", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
75 | "P213", |
9595
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
76 | ## Replacements for some Python standard library functions |
8166 | 77 | "P301", |
78 | ## Replacements for py.path.local | |
79 | "P401", | |
80 | ] | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
81 | |
8166 | 82 | # map functions to be replaced to error codes |
83 | Function2Code = { | |
84 | "os.chmod": "P101", | |
85 | "os.mkdir": "P102", | |
86 | "os.makedirs": "P103", | |
87 | "os.rename": "P104", | |
88 | "os.replace": "P105", | |
89 | "os.rmdir": "P106", | |
90 | "os.remove": "P107", | |
91 | "os.unlink": "P108", | |
92 | "os.getcwd": "P109", | |
93 | "os.readlink": "P110", | |
94 | "os.stat": "P111", | |
8764
18c69de2292f
Upgraded the pathlib checker to support additional replacements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
95 | "os.listdir": "P112", |
18c69de2292f
Upgraded the pathlib checker to support additional replacements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
96 | "os.link": "P113", |
18c69de2292f
Upgraded the pathlib checker to support additional replacements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
97 | "os.symlink": "P114", |
8166 | 98 | "os.path.abspath": "P201", |
99 | "os.path.exists": "P202", | |
100 | "os.path.expanduser": "P203", | |
101 | "os.path.isdir": "P204", | |
102 | "os.path.isfile": "P205", | |
103 | "os.path.islink": "P206", | |
104 | "os.path.isabs": "P207", | |
105 | "os.path.join": "P208", | |
106 | "os.path.basename": "P209", | |
107 | "os.path.dirname": "P210", | |
108 | "os.path.samefile": "P211", | |
109 | "os.path.splitext": "P212", | |
8764
18c69de2292f
Upgraded the pathlib checker to support additional replacements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
110 | "os.path.relpath": "P213", |
8166 | 111 | "open": "P301", |
112 | "py.path.local": "P401", | |
113 | } | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
114 | |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
115 | def __init__(self, source, filename, tree, selected, ignored, expected, repeat): |
8166 | 116 | """ |
117 | Constructor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
118 | |
8166 | 119 | @param source source code to be checked |
120 | @type list of str | |
121 | @param filename name of the source file | |
122 | @type str | |
8198
1c765dc90c21
Code Style Checker: changed code such, that the AST tree is built only once and passed to each checker module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8166
diff
changeset
|
123 | @param tree AST tree of the source code |
1c765dc90c21
Code Style Checker: changed code such, that the AST tree is built only once and passed to each checker module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8166
diff
changeset
|
124 | @type ast.Module |
8166 | 125 | @param selected list of selected codes |
126 | @type list of str | |
127 | @param ignored list of codes to be ignored | |
128 | @type list of str | |
129 | @param expected list of expected codes | |
130 | @type list of str | |
131 | @param repeat flag indicating to report each occurrence of a code | |
132 | @type bool | |
133 | """ | |
134 | self.__select = tuple(selected) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
135 | self.__ignore = ("",) if selected else tuple(ignored) |
8166 | 136 | self.__expected = expected[:] |
137 | self.__repeat = repeat | |
138 | self.__filename = filename | |
139 | self.__source = source[:] | |
8198
1c765dc90c21
Code Style Checker: changed code such, that the AST tree is built only once and passed to each checker module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8166
diff
changeset
|
140 | self.__tree = copy.deepcopy(tree) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
141 | |
8166 | 142 | # statistics counters |
143 | self.counters = {} | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
144 | |
8166 | 145 | # collection of detected errors |
146 | self.errors = [] | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
147 | |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
148 | self.__checkCodes = (code for code in self.Codes if not self.__ignoreCode(code)) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
149 | |
8166 | 150 | def __ignoreCode(self, code): |
151 | """ | |
152 | Private method to check if the message code should be ignored. | |
153 | ||
154 | @param code message code to check for | |
155 | @type str | |
156 | @return flag indicating to ignore the given code | |
157 | @rtype bool | |
158 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
159 | return code.startswith(self.__ignore) and not code.startswith(self.__select) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
160 | |
8166 | 161 | def __error(self, lineNumber, offset, code, *args): |
162 | """ | |
163 | Private method to record an issue. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
164 | |
8166 | 165 | @param lineNumber line number of the issue |
166 | @type int | |
167 | @param offset position within line of the issue | |
168 | @type int | |
169 | @param code message code | |
170 | @type str | |
171 | @param args arguments for the message | |
172 | @type list | |
173 | """ | |
174 | if self.__ignoreCode(code): | |
175 | return | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
176 | |
8166 | 177 | if code in self.counters: |
178 | self.counters[code] += 1 | |
179 | else: | |
180 | self.counters[code] = 1 | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
181 | |
8166 | 182 | # Don't care about expected codes |
183 | if code in self.__expected: | |
184 | return | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
185 | |
8166 | 186 | if code and (self.counters[code] == 1 or self.__repeat): |
187 | # record the issue with one based line number | |
188 | self.errors.append( | |
189 | { | |
190 | "file": self.__filename, | |
191 | "line": lineNumber + 1, | |
192 | "offset": offset, | |
193 | "code": code, | |
194 | "args": args, | |
195 | } | |
196 | ) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
197 | |
8166 | 198 | def run(self): |
199 | """ | |
200 | Public method to check the given source against functions | |
201 | to be replaced by 'pathlib' equivalents. | |
202 | """ | |
203 | if not self.__filename: | |
204 | # don't do anything, if essential data is missing | |
205 | return | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
206 | |
8166 | 207 | if not self.__checkCodes: |
208 | # don't do anything, if no codes were selected | |
209 | return | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
210 | |
8166 | 211 | visitor = PathlibVisitor(self.__checkForReplacement) |
212 | visitor.visit(self.__tree) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
213 | |
8166 | 214 | def __checkForReplacement(self, node, name): |
215 | """ | |
216 | Private method to check the given node for the need for a | |
217 | replacement. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
218 | |
8166 | 219 | @param node reference to the AST node to check |
220 | @type ast.AST | |
221 | @param name resolved name of the node | |
222 | @type str | |
223 | """ | |
8243
cc717c2ae956
Applied some more code simplifications suggested by the new Simplify checker (Y105: use contextlib.suppress) (batch 2).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8218
diff
changeset
|
224 | with contextlib.suppress(KeyError): |
8166 | 225 | errorCode = self.Function2Code[name] |
226 | self.__error(node.lineno - 1, node.col_offset, errorCode) | |
227 | ||
228 | ||
229 | class PathlibVisitor(ast.NodeVisitor): | |
230 | """ | |
231 | Class to traverse the AST node tree and check for potential issues. | |
232 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
233 | |
8166 | 234 | def __init__(self, checkCallback): |
235 | """ | |
236 | Constructor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
237 | |
8166 | 238 | @param checkCallback callback function taking a reference to the |
239 | AST node and the resolved name | |
240 | @type func | |
241 | """ | |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8207
diff
changeset
|
242 | super().__init__() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
243 | |
8166 | 244 | self.__checkCallback = checkCallback |
245 | self.__importAlias = {} | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
246 | |
8166 | 247 | def visit_ImportFrom(self, node): |
248 | """ | |
249 | Public method handle the ImportFrom AST node. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
250 | |
8166 | 251 | @param node reference to the ImportFrom AST node |
252 | @type ast.ImportFrom | |
253 | """ | |
254 | for imp in node.names: | |
255 | if imp.asname: | |
256 | self.__importAlias[imp.asname] = f"{node.module}.{imp.name}" | |
257 | else: | |
258 | self.__importAlias[imp.name] = f"{node.module}.{imp.name}" | |
259 | ||
260 | def visit_Import(self, node): | |
261 | """ | |
262 | Public method to handle the Import AST node. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
263 | |
8166 | 264 | @param node reference to the Import AST node |
265 | @type ast.Import | |
266 | """ | |
267 | for imp in node.names: | |
268 | if imp.asname: | |
269 | self.__importAlias[imp.asname] = imp.name | |
270 | ||
271 | def visit_Call(self, node): | |
272 | """ | |
273 | Public method to handle the Call AST node. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
274 | |
8166 | 275 | @param node reference to the Call AST node |
276 | @type ast.Call | |
277 | """ | |
278 | nameResolver = NameResolver(self.__importAlias) | |
279 | nameResolver.visit(node.func) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
280 | |
8166 | 281 | self.__checkCallback(node, nameResolver.name()) |
282 | ||
283 | ||
284 | class NameResolver(ast.NodeVisitor): | |
285 | """ | |
286 | Class to resolve a Name or Attribute node. | |
287 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
288 | |
8166 | 289 | def __init__(self, importAlias): |
290 | """ | |
291 | Constructor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
292 | |
8166 | 293 | @param importAlias reference to the import aliases dictionary |
294 | @type dict | |
295 | """ | |
296 | self.__importAlias = importAlias | |
297 | self.__names = [] | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
298 | |
8166 | 299 | def name(self): |
300 | """ | |
301 | Public method to resolve the name. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
302 | |
8166 | 303 | @return resolved name |
304 | @rtype str | |
305 | """ | |
8243
cc717c2ae956
Applied some more code simplifications suggested by the new Simplify checker (Y105: use contextlib.suppress) (batch 2).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8218
diff
changeset
|
306 | with contextlib.suppress(KeyError, IndexError): |
8166 | 307 | attr = self.__importAlias[self.__names[-1]] |
308 | self.__names[-1] = attr | |
309 | # do nothing if there is no such name or the names list is empty | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
310 | |
8166 | 311 | return ".".join(reversed(self.__names)) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
312 | |
8166 | 313 | def visit_Name(self, node): |
314 | """ | |
315 | Public method to handle the Name AST node. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
316 | |
8166 | 317 | @param node reference to the Name AST node |
318 | @type ast.Name | |
319 | """ | |
320 | self.__names.append(node.id) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
321 | |
8166 | 322 | def visit_Attribute(self, node): |
323 | """ | |
324 | Public method to handle the Attribute AST node. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
325 | |
8166 | 326 | @param node reference to the Attribute AST node |
327 | @type ast.Attribute | |
328 | """ | |
329 | try: | |
330 | self.__names.append(node.attr) | |
331 | self.__names.append(node.value.id) | |
332 | except AttributeError: | |
333 | self.generic_visit(node) |