-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpycat
executable file
·73 lines (68 loc) · 2.89 KB
/
pycat
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
#! /usr/bin/env python
# works with both Python 2.7 and 3.7, nice
# Pycat - a skeleton.
# Requirements:
# 1. Must take a stream of data, either from a file, or STDIN, and redirect
# it to another file, or STDOUT.
# 2. Must take parameters via the commandline: -f filein, for the
# input file, -o fileout for the output file, -v to print the version,
# and -h to print the version and a Usage statement. The usage
# statement should be a complete manual.
# 3. Must have a basic filter capability: given a regexp, only matching lines
# should be able to pass through.
#
# test cases:
# 1. "cat /etc/printcap | pycat" should produce the printcap file
# 2. "cat /etc/printcap | pycat -v" should produce only the version string,
# ignoring the attempted input
# 3. pycat -f /etc/printcap" should produce the same as 1 or "cat /etc/printcap"
# 4. pycat -h should print a usage message
# 5. pycat -f /etc/printcap -o /tmp/printcarp should produce a copy of
# /etc/printcap into the file /etc/printcarp
# 6. pycat -f /etc/printcap -r "^indust" should only print those lines beginning with "indust"
# 7. Same as above, but with -nr (negate regexp) will print those lines NOT beginning with "indust"
# init
import sys
import argparse
import re
import fileinput
# set up options parser
Argument_Parser = argparse.ArgumentParser()
Argument_Parser.add_argument("-f", "--inputfile", type=str, dest="infile", default="-")
Argument_Parser.add_argument("-o", "--outputfile", type=str, dest="outfile", default="-")
Argument_Parser.add_argument("-n", "--negate", action='store_true', dest="invert_re")
Argument_Parser.add_argument("-r", "--regexp", type=str, dest="regexp", default="")
Argument_Parser.description = "A simple file filter"
# process options
args = Argument_Parser.parse_args()
# first we have the no regexp option. No point in engaging the machinery if
# we aren't asking it to do anything
if args.regexp == "":
if args.outfile == "-":
fileout = sys.stdout
else:
fileout = open(args.outfile, "w")
# the fileinput module takes care of a lot of things for you. In particular,
# if you send it a '-' for a filename, it uses the stdin.
for line in open(args.infile).readlines():
fileout.write(line)
fileout.close()
else:
# sadly, there is no matching fileoutput module. Maybe I missed it?
if args.outfile == "-":
fileout = sys.stdout
else:
fileout = open(args.outfile, "w")
# Okay, we can see our files, lets compile up the regexp and start
# looping over the lines
user_regexp = re.compile(args.regexp)
for line in fileinput.input(args.infile):
# is there a better way to invert the meaning of a search?
if args.invert_re:
if user_regexp.search(line) is None:
fileout.write(line)
else:
if user_regexp.search(line) is not None:
fileout.write(line)
fileout.close()
sys.exit(0)