-
Notifications
You must be signed in to change notification settings - Fork 0
/
stuff.py
executable file
·51 lines (39 loc) · 1.16 KB
/
stuff.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
#! /usr/bin/env python3
""" Print a file after removing comments and empty lines. If file is not
specified, input is read from stdin.
Usage:
stuff.py filename
Sample usage:
% stuff.py /etc/apt/sources.list
% cat /etc/apt/sources.list | stuff.py
% stuff.py < /etc/apt/sources.list
"""
import argparse
import re
import sys
def compact_print(fname, comment_pattern):
empty_pattern = r"^\s*$"
using_stdin = fname is None
if (using_stdin):
fh = sys.stdin
else:
fh = open(fname)
for line in fh:
line = line.rstrip()
if re.search(comment_pattern, line):
continue
elif re.search(empty_pattern, line):
continue
else:
print(line)
if (not using_stdin):
fh.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='''Print a file after removing comments and
empty lines. If file is not specified, input is read from stdin.''')
parser.add_argument(
"fname", nargs='?', action='store',
help="Input file name. Leave empty for stdin.")
args = parser.parse_args()
compact_print(args.fname, r"^\s*#")