-
-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathline-length.py
More file actions
45 lines (32 loc) · 1.1 KB
/
line-length.py
File metadata and controls
45 lines (32 loc) · 1.1 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
#!/usr/bin/env python
"""Measure line length in given files, run as:
python line-length.py *.po
It does not count zero-width caracters from the Mn Unicode category
(Nonspacing Mark).
It returns 0 on success, 1 on failure.
"""
import fileinput
import sys
from unicodedata import category
MAX_LINE_LENGTH = 80
def clean(line):
"""Prepare the line to be measured.
See https://lists.gnu.org/archive/html/bug-gettext/2025-10/msg00010.html
about hiding spaces before colon.
"""
line = "".join(char for char in line if category(char) != "Mn").rstrip("\n")
line = line.replace(" :", " :") # It's not allowed to split a line before `:`
return line
return_code = 0
for line in fileinput.input(encoding="utf-8"):
line = clean(line)
if line.count(" ") <= 2:
continue # Could be hard to break.
if len(line) > MAX_LINE_LENGTH:
print(
f"{fileinput.filename()}:{fileinput.filelineno()} line too long "
f"({len(line)} > {MAX_LINE_LENGTH} characters)",
file=sys.stderr,
)
return_code = 1
sys.exit(return_code)