/
proselint_wrapper.py
executable file
·59 lines (45 loc) · 1.51 KB
/
proselint_wrapper.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
#!/usr/bin/env python
"""Remove comments before passing output to proselint."""
import locale
import re
import subprocess
import sys
def main():
if len(sys.argv) != 2:
sys.stderr.write('Usage: ' + sys.argv[0] + ' filename\n')
return 2
filename = sys.argv[1]
input_lines = []
try:
input_file = open(filename)
try:
for line in input_file.readlines():
if line.startswith('#'):
# Ignore everything below the comments like the diff that
# shows up in verbose commit mode.
break
else:
input_lines.append(line)
finally:
input_file.close()
except IOError:
# Ignore unreadable file.
sys.stderr.write('%s\n' % (sys.exc_info()[1],))
return 1
try:
process = subprocess.Popen(['proselint', '-'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
except OSError:
# Ignore if proselint is not installed.
return 0
encoding = locale.getpreferredencoding()
output = process.communicate(''.join(input_lines).encode(encoding))[0]
if sys.version_info[0] > 2:
output = output.decode(encoding)
for line in output.splitlines(True):
filtered_line = re.sub('^-:', filename + ':', line)
sys.stdout.write(filtered_line)
return process.returncode
if __name__ == '__main__':
sys.exit(main())