-
Notifications
You must be signed in to change notification settings - Fork 0
/
trimline.py
executable file
·64 lines (54 loc) · 1.58 KB
/
trimline.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
#!/usr/bin/env python
#coding='utf-8'
"""
Removes trailing whitespace. * Add text-wrapping.
"""
import argparse
import pydoc
import sys
import traceback
from cjh.cli import Cli
__author__ = 'Chris Horn <hammerhorn@gmail.com>'
__license__ = 'GPL'
def _parse_args():
"""Parse arguments"""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('filename', type=str, help='file to process')
if __name__ == '__main__':
args = parser.parse_args()
else:
args = None
return args
def main():
"""
Reads in a specified file, removes trailing whitespace, and re-saves.
"""
# Open file and store lines as str list
try:
file_handler = open(ARGS.filename, 'r+')
except IOError:
print(traceback.format_exc()) # pylint: disable=C0325
sys.exit()
lines_of_text = file_handler.readlines()
file_handler.seek(0)
# Preview and write text back to file and close file
print(lines_of_text) # pylint: disable=C0325
Cli.wait()
string = ''
preview_string = ''
for index, _ in enumerate(lines_of_text):
string += lines_of_text[index].rstrip() + '\n'
preview_string = string.strip() + Cli.term_fx('b', 'EOL')
pydoc.pipepager(preview_string, cmd='less -R')
try:
char = Cli.get_keypress('Write to file?')
assert char == 'y'
file_handler.write(string)
except AssertionError:
print('File not saved. Good bye.') # pylint: disable=C0325
finally:
file_handler.close()
Cli()
ARGS = _parse_args()
if __name__ == '__main__':
main()