-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtrim_ws.py
More file actions
73 lines (62 loc) · 1.54 KB
/
trim_ws.py
File metadata and controls
73 lines (62 loc) · 1.54 KB
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
# .pre-commit-scripts/trim_ws.py
# Minimal, quiet trailing-whitespace fixer for text-like files.
import sys
from pathlib import Path
import re
EXIT_CODE = 0
TRAILING_WS = re.compile(r"[ \t]+(\r?\n)")
def is_small_text_file(p: Path) -> bool:
# Adjust size threshold if needed
if not p.is_file():
return False
if p.suffix.lower() in {
".ipynb",
".png",
".jpg",
".jpeg",
".gif",
".svg",
".pdf",
".zip",
".bin",
".pt",
".onnx",
".csv",
".tsv",
".jsonl",
".ndjson",
}:
return False
# skip anything larger than ~2 MB
try:
if p.stat().st_size > 2_000_000:
return False
except OSError:
return False
return True
def fix_file(p: Path) -> bool:
"""Return True if modified."""
try:
text = p.read_text(encoding="utf-8")
except Exception:
return False # non-UTF8 or unreadable, skip quietly
fixed = TRAILING_WS.sub(r"\1", text)
if fixed != text:
p.write_text(fixed, encoding="utf-8", newline="")
return True
return False
def main(paths):
global EXIT_CODE
changed = []
for fp in paths:
p = Path(fp)
if not is_small_text_file(p):
continue
if fix_file(p):
changed.append(str(p))
# Quiet output, only list changed filenames
if changed:
print("trim_ws modified:", *changed, sep="\n ")
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))