Sat, 26 Apr 2025 12:34:32 +0200
MicroPython
- Added a configuration option to disable the support for the no longer produced Pimoroni Pico Wireless Pack.
8166 | 1 | # -*- coding: utf-8 -*- |
2 | ||
11090
f5f5f5803935
Updated copyright for 2025.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
3 | # Copyright (c) 2021 - 2025 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 | |
8166 | 17 | import ast |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
18 | import contextlib |
11150
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
19 | |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
20 | from CodeStyleTopicChecker import CodeStyleTopicChecker |
8166 | 21 | |
22 | ||
11150
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
23 | class PathlibChecker(CodeStyleTopicChecker): |
8166 | 24 | """ |
25 | Class implementing a checker for functions that can be replaced by use of | |
26 | the pathlib module. | |
27 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
28 | |
8166 | 29 | Codes = [ |
30 | ## Replacements for the os module functions | |
11147
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
31 | "P-101", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
32 | "P-102", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
33 | "P-103", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
34 | "P-104", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
35 | "P-105", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
36 | "P-106", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
37 | "P-107", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
38 | "P-108", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
39 | "P-109", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
40 | "P-110", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
41 | "P-111", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
42 | "P-112", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
43 | "P-113", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
44 | "P-114", |
8166 | 45 | ## Replacements for the os.path module functions |
11147
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
46 | "P-201", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
47 | "P-202", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
48 | "P-203", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
49 | "P-204", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
50 | "P-205", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
51 | "P-206", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
52 | "P-207", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
53 | "P-208", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
54 | "P-209", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
55 | "P-210", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
56 | "P-211", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
57 | "P-212", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
58 | "P-213", |
9595
2bd590c40309
Updated some checker methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
59 | ## Replacements for some Python standard library functions |
11147
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
60 | "P-301", |
8166 | 61 | ## Replacements for py.path.local |
11147
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
62 | "P-401", |
8166 | 63 | ] |
11150
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
64 | Category = "P" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
65 | |
8166 | 66 | # map functions to be replaced to error codes |
67 | Function2Code = { | |
11147
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
68 | "os.chmod": "P-101", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
69 | "os.mkdir": "P-102", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
70 | "os.makedirs": "P-103", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
71 | "os.rename": "P-104", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
72 | "os.replace": "P-105", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
73 | "os.rmdir": "P-106", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
74 | "os.remove": "P-107", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
75 | "os.unlink": "P-108", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
76 | "os.getcwd": "P-109", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
77 | "os.readlink": "P-110", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
78 | "os.stat": "P-111", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
79 | "os.listdir": "P-112", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
80 | "os.link": "P-113", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
81 | "os.symlink": "P-114", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
82 | "os.path.abspath": "P-201", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
83 | "os.path.exists": "P-202", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
84 | "os.path.expanduser": "P-203", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
85 | "os.path.isdir": "P-204", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
86 | "os.path.isfile": "P-205", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
87 | "os.path.islink": "P-206", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
88 | "os.path.isabs": "P-207", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
89 | "os.path.join": "P-208", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
90 | "os.path.basename": "P-209", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
91 | "os.path.dirname": "P-210", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
92 | "os.path.samefile": "P-211", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
93 | "os.path.splitext": "P-212", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
94 | "os.path.relpath": "P-213", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
95 | "open": "P-301", |
dee6e106b4d3
Modified the code style checker such, that the issue category and issue number are separated by a '-' to make up the issue code (e.g E-901).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11145
diff
changeset
|
96 | "py.path.local": "P-401", |
8166 | 97 | } |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
98 | |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
99 | def __init__(self, source, filename, tree, selected, ignored, expected, repeat): |
8166 | 100 | """ |
101 | Constructor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
102 | |
8166 | 103 | @param source source code to be checked |
104 | @type list of str | |
105 | @param filename name of the source file | |
106 | @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
|
107 | @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
|
108 | @type ast.Module |
8166 | 109 | @param selected list of selected codes |
110 | @type list of str | |
111 | @param ignored list of codes to be ignored | |
112 | @type list of str | |
113 | @param expected list of expected codes | |
114 | @type list of str | |
115 | @param repeat flag indicating to report each occurrence of a code | |
116 | @type bool | |
117 | """ | |
11150
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
118 | super().__init__( |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
119 | PathlibChecker.Category, |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
120 | source, |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
121 | filename, |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
122 | tree, |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
123 | selected, |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
124 | ignored, |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
125 | expected, |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
126 | repeat, |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
127 | [], |
11142
2f0fb22c1d63
Fixed a few issues in the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11090
diff
changeset
|
128 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
129 | |
11150
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
130 | checkersWithCodes = [ |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
131 | ( |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
132 | self.__checkPathlibReplacement, |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
133 | ( |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
134 | "P-101", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
135 | "P-102", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
136 | "P-103", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
137 | "P-104", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
138 | "P-105", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
139 | "P-106", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
140 | "P-107", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
141 | "P-108", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
142 | "P-109", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
143 | "P-110", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
144 | "P-111", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
145 | "P-112", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
146 | "P-113", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
147 | "P-114", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
148 | "P-201", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
149 | "P-202", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
150 | "P-203", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
151 | "P-204", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
152 | "P-205", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
153 | "P-206", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
154 | "P-207", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
155 | "P-208", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
156 | "P-209", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
157 | "P-210", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
158 | "P-211", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
159 | "P-212", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
160 | "P-213", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
161 | "P-301", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
162 | "P-401", |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
163 | ), |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
164 | ), |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
165 | ] |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
166 | self._initializeCheckers(checkersWithCodes) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
167 | |
11150
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
168 | def __checkPathlibReplacement(self): |
8166 | 169 | """ |
11150
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
170 | Private method to check for pathlib replacements. |
8166 | 171 | """ |
172 | visitor = PathlibVisitor(self.__checkForReplacement) | |
11150
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
173 | visitor.visit(self.tree) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
174 | |
8166 | 175 | def __checkForReplacement(self, node, name): |
176 | """ | |
177 | Private method to check the given node for the need for a | |
178 | replacement. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
179 | |
8166 | 180 | @param node reference to the AST node to check |
181 | @type ast.AST | |
182 | @param name resolved name of the node | |
183 | @type str | |
184 | """ | |
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
|
185 | with contextlib.suppress(KeyError): |
8166 | 186 | errorCode = self.Function2Code[name] |
11150
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
187 | self.addErrorFromNode(node, errorCode) |
8166 | 188 | |
189 | ||
190 | class PathlibVisitor(ast.NodeVisitor): | |
191 | """ | |
192 | Class to traverse the AST node tree and check for potential issues. | |
193 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
194 | |
8166 | 195 | def __init__(self, checkCallback): |
196 | """ | |
197 | Constructor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
198 | |
8166 | 199 | @param checkCallback callback function taking a reference to the |
200 | AST node and the resolved name | |
201 | @type func | |
202 | """ | |
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
|
203 | super().__init__() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
204 | |
8166 | 205 | self.__checkCallback = checkCallback |
206 | self.__importAlias = {} | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
207 | |
8166 | 208 | def visit_ImportFrom(self, node): |
209 | """ | |
210 | 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
|
211 | |
8166 | 212 | @param node reference to the ImportFrom AST node |
213 | @type ast.ImportFrom | |
214 | """ | |
215 | for imp in node.names: | |
216 | if imp.asname: | |
217 | self.__importAlias[imp.asname] = f"{node.module}.{imp.name}" | |
218 | else: | |
219 | self.__importAlias[imp.name] = f"{node.module}.{imp.name}" | |
220 | ||
221 | def visit_Import(self, node): | |
222 | """ | |
223 | 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
|
224 | |
8166 | 225 | @param node reference to the Import AST node |
226 | @type ast.Import | |
227 | """ | |
228 | for imp in node.names: | |
229 | if imp.asname: | |
230 | self.__importAlias[imp.asname] = imp.name | |
231 | ||
232 | def visit_Call(self, node): | |
233 | """ | |
234 | 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
|
235 | |
8166 | 236 | @param node reference to the Call AST node |
237 | @type ast.Call | |
238 | """ | |
239 | nameResolver = NameResolver(self.__importAlias) | |
240 | nameResolver.visit(node.func) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
241 | |
8166 | 242 | self.__checkCallback(node, nameResolver.name()) |
243 | ||
11150
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
244 | self.generic_visit(node) |
73d80859079c
Code Style Checkers
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11147
diff
changeset
|
245 | |
8166 | 246 | |
247 | class NameResolver(ast.NodeVisitor): | |
248 | """ | |
249 | Class to resolve a Name or Attribute node. | |
250 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
251 | |
8166 | 252 | def __init__(self, importAlias): |
253 | """ | |
254 | Constructor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
255 | |
8166 | 256 | @param importAlias reference to the import aliases dictionary |
257 | @type dict | |
258 | """ | |
259 | self.__importAlias = importAlias | |
260 | self.__names = [] | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
261 | |
8166 | 262 | def name(self): |
263 | """ | |
264 | 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
|
265 | |
8166 | 266 | @return resolved name |
267 | @rtype str | |
268 | """ | |
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
|
269 | with contextlib.suppress(KeyError, IndexError): |
8166 | 270 | attr = self.__importAlias[self.__names[-1]] |
271 | self.__names[-1] = attr | |
272 | # 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
|
273 | |
8166 | 274 | 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
|
275 | |
8166 | 276 | def visit_Name(self, node): |
277 | """ | |
278 | 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
|
279 | |
8166 | 280 | @param node reference to the Name AST node |
281 | @type ast.Name | |
282 | """ | |
283 | 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
|
284 | |
8166 | 285 | def visit_Attribute(self, node): |
286 | """ | |
287 | 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
|
288 | |
8166 | 289 | @param node reference to the Attribute AST node |
290 | @type ast.Attribute | |
291 | """ | |
292 | try: | |
293 | self.__names.append(node.attr) | |
294 | self.__names.append(node.value.id) | |
295 | except AttributeError: | |
296 | self.generic_visit(node) |