-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprocessor.py
97 lines (69 loc) · 2.57 KB
/
processor.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import argparse
import re
def trim_blanks(text_lines):
return [line.strip() for line in text_lines if line.strip()]
def read_file(filename):
with open(filename, 'r') as f:
return f.read()
def write_file(filename, content):
content += '\n'
with open(filename, 'w') as f:
f.write(content)
def complete(text_lines):
newlines = []
for line in text_lines:
newline = line
newline = re.sub(r' *… *', '…', newline)
newline = re.sub(r'sb.?', 'sb.', newline)
newline = re.sub(r'sth.?', 'sth.', newline)
if "sb.'s" in newline:
newlines.append(newline.replace("sb.'s", "one's"))
newlines.append(newline)
return newlines
def uniquefy_lines(text_lines):
line_set = set(text_lines)
return list(line_set)
def parse_args():
parser = argparse.ArgumentParser(description='Processing text.')
parser.add_argument('filepaths',
metavar='filepath',
type=str,
nargs='+',
help='input an absolute or a relative filepath')
parser.add_argument('-c', '--complete',
dest="should_complete",
action='store_true',
help='complete "sb" -> "sb.", "sth" -> "sth." etc.')
parser.add_argument('-o', '--override',
dest="should_override",
action='store_true',
help='override the original file, otherwise simply'
+ ' print the result')
parser.add_argument('-s', '--sort',
dest="should_sort",
action='store_true',
help='sort all lines in alphabetical order')
parser.add_argument('-u', '--unique',
dest="should_uniquefy",
action='store_true',
help='ensure each line is unique')
return parser.parse_args()
def main():
args = parse_args()
for fp in args.filepaths:
text = read_file(fp)
text_lines = text.split('\n')
if args.should_complete:
text_lines = complete(text_lines)
text_lines = trim_blanks(text_lines)
if args.should_uniquefy:
text_lines = uniquefy_lines(text_lines)
if args.should_sort:
text_lines = sorted(text_lines)
text = '\n'.join(text_lines)
if args.should_override:
write_file(fp, text)
else:
print(text)
if __name__ == '__main__':
main()