Mon, 22 May 2023 09:07:37 +0200
Corrected some code style issues detected by the extended checkers.
8166 | 1 | # -*- coding: utf-8 -*- |
2 | ||
9653
e67609152c5e
Updated copyright for 2023.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9595
diff
changeset
|
3 | # Copyright (c) 2021 - 2023 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 | ##################################################################################### |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
12 | ## This module was implemented along the 'flake8-use-pathlib' flake8 ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
13 | ## extension (v 0.3.0). ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
14 | ## ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
15 | ## Original: Copyright (c) 2021 Rodolphe Pelloux-Prayer ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
16 | ## ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
17 | ## License: ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
18 | ## 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
|
19 | ## 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
|
20 | ## 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
|
21 | ## 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
|
22 | ## 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
|
23 | ## furnished to do so, subject to the following conditions: ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
24 | ## ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
25 | ## 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
|
26 | ## copies or substantial portions of the Software. ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
27 | ## ## |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
28 | ## 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
|
29 | ## 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
|
30 | ## 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
|
31 | ## 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
|
32 | ## 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
|
33 | ## 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
|
34 | ##################################################################################### |
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
35 | |
8166 | 36 | import ast |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
37 | 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
|
38 | import copy |
8166 | 39 | |
40 | ||
8207
d359172d11be
Applied some more code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8198
diff
changeset
|
41 | class PathlibChecker: |
8166 | 42 | """ |
43 | Class implementing a checker for functions that can be replaced by use of | |
44 | the pathlib module. | |
45 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
46 | |
8166 | 47 | Codes = [ |
48 | ## 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
|
49 | "P101", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
50 | "P102", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
51 | "P103", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
52 | "P104", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
53 | "P105", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
54 | "P106", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
55 | "P107", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
56 | "P108", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
57 | "P109", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
58 | "P110", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
59 | "P111", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
60 | "P112", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
61 | "P113", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
62 | "P114", |
8166 | 63 | ## 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
|
64 | "P201", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
65 | "P202", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
66 | "P203", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
67 | "P204", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
68 | "P205", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
69 | "P206", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
70 | "P207", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
71 | "P208", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
72 | "P209", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
73 | "P210", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
74 | "P211", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
75 | "P212", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
76 | "P213", |
9595
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
77 | ## Replacements for some Python standard library functions |
8166 | 78 | "P301", |
79 | ## Replacements for py.path.local | |
80 | "P401", | |
81 | ] | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
82 | |
8166 | 83 | # map functions to be replaced to error codes |
84 | Function2Code = { | |
85 | "os.chmod": "P101", | |
86 | "os.mkdir": "P102", | |
87 | "os.makedirs": "P103", | |
88 | "os.rename": "P104", | |
89 | "os.replace": "P105", | |
90 | "os.rmdir": "P106", | |
91 | "os.remove": "P107", | |
92 | "os.unlink": "P108", | |
93 | "os.getcwd": "P109", | |
94 | "os.readlink": "P110", | |
95 | "os.stat": "P111", | |
8764
18c69de2292f
Upgraded the pathlib checker to support additional replacements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
96 | "os.listdir": "P112", |
18c69de2292f
Upgraded the pathlib checker to support additional replacements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
97 | "os.link": "P113", |
18c69de2292f
Upgraded the pathlib checker to support additional replacements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
98 | "os.symlink": "P114", |
8166 | 99 | "os.path.abspath": "P201", |
100 | "os.path.exists": "P202", | |
101 | "os.path.expanduser": "P203", | |
102 | "os.path.isdir": "P204", | |
103 | "os.path.isfile": "P205", | |
104 | "os.path.islink": "P206", | |
105 | "os.path.isabs": "P207", | |
106 | "os.path.join": "P208", | |
107 | "os.path.basename": "P209", | |
108 | "os.path.dirname": "P210", | |
109 | "os.path.samefile": "P211", | |
110 | "os.path.splitext": "P212", | |
8764
18c69de2292f
Upgraded the pathlib checker to support additional replacements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
111 | "os.path.relpath": "P213", |
8166 | 112 | "open": "P301", |
113 | "py.path.local": "P401", | |
114 | } | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
115 | |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
116 | def __init__(self, source, filename, tree, selected, ignored, expected, repeat): |
8166 | 117 | """ |
118 | Constructor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
119 | |
8166 | 120 | @param source source code to be checked |
121 | @type list of str | |
122 | @param filename name of the source file | |
123 | @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
|
124 | @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
|
125 | @type ast.Module |
8166 | 126 | @param selected list of selected codes |
127 | @type list of str | |
128 | @param ignored list of codes to be ignored | |
129 | @type list of str | |
130 | @param expected list of expected codes | |
131 | @type list of str | |
132 | @param repeat flag indicating to report each occurrence of a code | |
133 | @type bool | |
134 | """ | |
135 | self.__select = tuple(selected) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
136 | self.__ignore = ("",) if selected else tuple(ignored) |
8166 | 137 | self.__expected = expected[:] |
138 | self.__repeat = repeat | |
139 | self.__filename = filename | |
140 | 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
|
141 | 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
|
142 | |
8166 | 143 | # statistics counters |
144 | self.counters = {} | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
145 | |
8166 | 146 | # collection of detected errors |
147 | self.errors = [] | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
148 | |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
149 | 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
|
150 | |
8166 | 151 | def __ignoreCode(self, code): |
152 | """ | |
153 | Private method to check if the message code should be ignored. | |
154 | ||
155 | @param code message code to check for | |
156 | @type str | |
157 | @return flag indicating to ignore the given code | |
158 | @rtype bool | |
159 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
160 | 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
|
161 | |
8166 | 162 | def __error(self, lineNumber, offset, code, *args): |
163 | """ | |
164 | 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
|
165 | |
8166 | 166 | @param lineNumber line number of the issue |
167 | @type int | |
168 | @param offset position within line of the issue | |
169 | @type int | |
170 | @param code message code | |
171 | @type str | |
172 | @param args arguments for the message | |
173 | @type list | |
174 | """ | |
175 | if self.__ignoreCode(code): | |
176 | return | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
177 | |
8166 | 178 | if code in self.counters: |
179 | self.counters[code] += 1 | |
180 | else: | |
181 | self.counters[code] = 1 | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
182 | |
8166 | 183 | # Don't care about expected codes |
184 | if code in self.__expected: | |
185 | return | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
186 | |
8166 | 187 | if code and (self.counters[code] == 1 or self.__repeat): |
188 | # record the issue with one based line number | |
189 | self.errors.append( | |
190 | { | |
191 | "file": self.__filename, | |
192 | "line": lineNumber + 1, | |
193 | "offset": offset, | |
194 | "code": code, | |
195 | "args": args, | |
196 | } | |
197 | ) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
198 | |
8166 | 199 | def run(self): |
200 | """ | |
201 | Public method to check the given source against functions | |
202 | to be replaced by 'pathlib' equivalents. | |
203 | """ | |
204 | if not self.__filename: | |
205 | # don't do anything, if essential data is missing | |
206 | return | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
207 | |
8166 | 208 | if not self.__checkCodes: |
209 | # don't do anything, if no codes were selected | |
210 | return | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
211 | |
8166 | 212 | visitor = PathlibVisitor(self.__checkForReplacement) |
213 | visitor.visit(self.__tree) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
214 | |
8166 | 215 | def __checkForReplacement(self, node, name): |
216 | """ | |
217 | Private method to check the given node for the need for a | |
218 | replacement. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
219 | |
8166 | 220 | @param node reference to the AST node to check |
221 | @type ast.AST | |
222 | @param name resolved name of the node | |
223 | @type str | |
224 | """ | |
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
|
225 | with contextlib.suppress(KeyError): |
8166 | 226 | errorCode = self.Function2Code[name] |
227 | self.__error(node.lineno - 1, node.col_offset, errorCode) | |
228 | ||
229 | ||
230 | class PathlibVisitor(ast.NodeVisitor): | |
231 | """ | |
232 | Class to traverse the AST node tree and check for potential issues. | |
233 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
234 | |
8166 | 235 | def __init__(self, checkCallback): |
236 | """ | |
237 | Constructor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
238 | |
8166 | 239 | @param checkCallback callback function taking a reference to the |
240 | AST node and the resolved name | |
241 | @type func | |
242 | """ | |
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
|
243 | super().__init__() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
244 | |
8166 | 245 | self.__checkCallback = checkCallback |
246 | self.__importAlias = {} | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
247 | |
8166 | 248 | def visit_ImportFrom(self, node): |
249 | """ | |
250 | 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
|
251 | |
8166 | 252 | @param node reference to the ImportFrom AST node |
253 | @type ast.ImportFrom | |
254 | """ | |
255 | for imp in node.names: | |
256 | if imp.asname: | |
257 | self.__importAlias[imp.asname] = f"{node.module}.{imp.name}" | |
258 | else: | |
259 | self.__importAlias[imp.name] = f"{node.module}.{imp.name}" | |
260 | ||
261 | def visit_Import(self, node): | |
262 | """ | |
263 | 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
|
264 | |
8166 | 265 | @param node reference to the Import AST node |
266 | @type ast.Import | |
267 | """ | |
268 | for imp in node.names: | |
269 | if imp.asname: | |
270 | self.__importAlias[imp.asname] = imp.name | |
271 | ||
272 | def visit_Call(self, node): | |
273 | """ | |
274 | 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
|
275 | |
8166 | 276 | @param node reference to the Call AST node |
277 | @type ast.Call | |
278 | """ | |
279 | nameResolver = NameResolver(self.__importAlias) | |
280 | nameResolver.visit(node.func) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
281 | |
8166 | 282 | self.__checkCallback(node, nameResolver.name()) |
283 | ||
284 | ||
285 | class NameResolver(ast.NodeVisitor): | |
286 | """ | |
287 | Class to resolve a Name or Attribute node. | |
288 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
289 | |
8166 | 290 | def __init__(self, importAlias): |
291 | """ | |
292 | Constructor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
293 | |
8166 | 294 | @param importAlias reference to the import aliases dictionary |
295 | @type dict | |
296 | """ | |
297 | self.__importAlias = importAlias | |
298 | self.__names = [] | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
299 | |
8166 | 300 | def name(self): |
301 | """ | |
302 | 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
|
303 | |
8166 | 304 | @return resolved name |
305 | @rtype str | |
306 | """ | |
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
|
307 | with contextlib.suppress(KeyError, IndexError): |
8166 | 308 | attr = self.__importAlias[self.__names[-1]] |
309 | self.__names[-1] = attr | |
310 | # 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
|
311 | |
8166 | 312 | 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
|
313 | |
8166 | 314 | def visit_Name(self, node): |
315 | """ | |
316 | 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
|
317 | |
8166 | 318 | @param node reference to the Name AST node |
319 | @type ast.Name | |
320 | """ | |
321 | 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
|
322 | |
8166 | 323 | def visit_Attribute(self, node): |
324 | """ | |
325 | 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
|
326 | |
8166 | 327 | @param node reference to the Attribute AST node |
328 | @type ast.Attribute | |
329 | """ | |
330 | try: | |
331 | self.__names.append(node.attr) | |
332 | self.__names.append(node.value.id) | |
333 | except AttributeError: | |
334 | self.generic_visit(node) |