Skip to content

Commit 97c6047

Browse files
committed
new script to blacken docstrings in bindings
1 parent da4f2db commit 97c6047

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

tools/black_bindings.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/python3
2+
import re
3+
import subprocess
4+
import sys
5+
6+
7+
def transform(s):
8+
lines = s.rstrip().split("\n")
9+
lines = [line.removeprefix("//| ").removeprefix("//|") for line in lines]
10+
s = "\n".join(lines) + "\n"
11+
if s[0] == " ":
12+
prefix = "if 0:\n"
13+
else:
14+
prefix = ""
15+
s = prefix + s
16+
result = subprocess.run(
17+
["black", "-q", "--pyi", "-"],
18+
input=s,
19+
check=True,
20+
stdout=subprocess.PIPE,
21+
encoding="utf-8",
22+
)
23+
result = result.stdout[len(prefix) :]
24+
result = (result.rstrip() + "\n").split("\n")
25+
return "\n".join("//| " + line if line else "//|" for line in result) + "\n"
26+
27+
28+
for fn in sys.argv[1:]:
29+
with open(fn, "r", encoding="utf-8") as f:
30+
content = f.read()
31+
32+
old_end = 0
33+
34+
newcontent = []
35+
for m in re.finditer("(?m)((?:^//\|.*\n)+)", content):
36+
newcontent.append(content[old_end : m.start()])
37+
newcontent.append(transform(m.group()))
38+
old_end = m.end()
39+
newcontent.append(content[old_end:])
40+
41+
with open(fn, "w", encoding="utf-8") as f:
42+
f.write("".join(newcontent))

0 commit comments

Comments
 (0)