forked from litepresence/Graphene-Metanode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_test_delint.py
181 lines (159 loc) · 4.77 KB
/
unit_test_delint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
r"""
black_all.py
WTFPL litepresence.com Jan 2021
A simple script that blacks, isorts, and pylints *.py files
"""
# STANDARD PYTHON MODULES
import os
from time import time
# these can be safely ignored in most circumstances
DISABLE = (
# too many?
"too-many-statements",
"too-many-locals",
"too-many-branches",
"too-many-function-args",
"too-many-arguments",
"too-many-nested-blocks",
"too-many-lines",
# improper exception handling
"bare-except",
"broad-except",
# snake_case, etc.
"invalid-name",
# sometimes it just can't find the modules referenced - on this machine
"import-error",
# whitespace authoritarianism
"bad-continuation",
"bad-whitespace",
# class minimums
"too-few-public-methods",
"no-self-use",
# suppression
"suppressed-message",
"locally-disabled",
"useless-suppression",
)
def auto_enumerate(name):
"""
swap enumerate() in place of range(len())
"""
with open(name, "r") as handle:
data = handle.read()
handle.close()
data = data.split("\n")
total = 0
final_data = []
for line in data:
if ", _ in enumerate(" in line and "):" in line:
line = line.replace(" in range(len(", ", _ in enumerate(").replace(
")):", "):"
)
total += 1
final_data.append(line)
final_data = "\n".join(final_data).strip("\n") + "\n"
with open(name, "w") as handle:
handle.write(final_data)
handle.close()
if total:
print(f"{total} range(len()) instances enumerated in {name}!")
def auto_broad_except(name):
with open(name, "r") as handle:
data = handle.read()
handle.close()
data = data.split("\n")
total = 0
final_data = []
for line in data:
if "except:" in line:
line = line.replace("except:", "except Exception:")
total += 1
final_data.append(line)
final_data = "\n".join(final_data).strip("\n") + "\n"
with open(name, "w") as handle:
handle.write(final_data)
handle.close()
if total:
print(f"{total} bare excepts replaced in {name}")
def auto_double_line_break(name):
with open(name, "r") as handle:
data = handle.read()
handle.close()
total = 0
for _ in range(3):
data_split = data.split("\n\n\n")
data = "\n\n".join(data_split)
total += len(data_split)-1
with open(name, "w") as handle:
handle.write(data)
handle.close()
if total:
print(f"{total} double line brakes replaced in {name}")
def main():
"""
\033c\nWelcome to lite Black Pylint Lite All! \n
"""
print(main.__doc__)
dispatch = {
1: "Black Pylint Lite All!",
2: "Black Pylint All!",
3: "Pylint Lite All Only",
4: "Pylint All Only",
5: "Black All Only",
}
print(" Menu\n")
for key, val in dispatch.items():
print(" ", key, " : ", val)
choice = input("\n\nInput Number or Press Enter for Choice 1\n\n ")
if choice == "":
choice = 1
choice = int(choice)
disabled = ""
if choice in [1, 3]:
disabled = "--enable=all --disable="
for item in DISABLE:
disabled += item + ","
disabled.rstrip(",")
# Get the start time
start = time()
# Clear the screen
print("\033c")
# Get all of the python files in the current folder
pythons = [f for f in os.listdir() if f.endswith(".py") and f != "black_all.py"]
# pythons = [f for f in os.listdir() if f in ONLY]
pythons = [f for f in pythons if "test" not in f]
for name in pythons:
auto_double_line_break(name)
# For every file in that list:
if choice in [1, 2, 5]:
for name in pythons:
# Print the script we are blacking.
print("Blacking script:", name)
# Black the script.
os.system(f"black -l 88 --experimental-string-processing {name}")
# Print a divider.
print("-" * 100)
print("Isorting all scripts")
os.system("isort *.py")
for name in pythons:
auto_enumerate(name)
for name in pythons:
auto_broad_except(name)
if choice in [1, 2, 3, 4]:
for name in pythons:
# Print the script we are blacking.
print("Pylinting script:", name)
# Black the script.
os.system(f"pylint {name} {disabled}")
# Print a divider.
print("-" * 100)
# Say we are done.
print("Done.")
# Get the end time:
end = time()
# Find the time it took to black the scripts.
took = end - start
# Print that time.
print(len(pythons), "scripts took %.1f" % took, "seconds.")
if __name__ == "__main__":
main()