-
Notifications
You must be signed in to change notification settings - Fork 0
/
linechecker
executable file
·33 lines (28 loc) · 1.31 KB
/
linechecker
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
#!/usr/bin/env python3
import argparse
import sys
"""
Check a plaintext document for lines containing uneven numbers of search characters.
Useful for finding lines in a LaTeX document with extra $, { etc. inserted, when
the compiler output isn't being useful
"""
seach_pairs = [('{', '}'), ('"', ''),
('$', ''), ('(', ')')]
# TODO add some method for adding / removing more seach pairs
if __name__ == "__main__":
# parse arguments
parser = argparse.ArgumentParser(description="Find lines of the input document containing uneven pairs of seach characters.")
parser.add_argument("-i", "--infile", type=str, default=None,
help="Input file to seach, default stdin")
args = parser.parse_args()
with (open(args.infile, 'r') if args.infile else sys.stdin) as infile:
for line_num, line in enumerate(infile):
for incrementer,decrementer in seach_pairs:
if decrementer == '':
count = line.count(incrementer) % 2
else:
count = line.count(incrementer) - line.count(decrementer)
if count is not 0:
outstring = "Line {0}: {1} {2} unmatched (diff {3})\n{4}"
print(outstring.format(line_num+1, incrementer, decrementer,
count, line))