Skip to content

Commit b051d7f

Browse files
committed
Add a '-c' flag to the command line
1 parent 435669c commit b051d7f

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/isal/igzip.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ def main():
276276
"-d", "--decompress", action="store_false",
277277
dest="compress",
278278
help="Decompress the file instead of compressing.")
279+
parser.add_argument("-c", "--stdout", action="store_true",
280+
help="write on standard output")
279281
args = parser.parse_args()
280282

281283
compresslevel = args.compresslevel or _COMPRESS_LEVEL_TRADEOFF
@@ -291,14 +293,21 @@ def main():
291293
else:
292294
if args.compress:
293295
in_file = io.open(args.file, mode="rb")
294-
out_file = open(args.file + ".gz", mode="wb",
295-
compresslevel=compresslevel)
296+
if args.stdout:
297+
out_file = IGzipFile(mode="wb", compresslevel=compresslevel,
298+
fileobj=sys.stdout.buffer)
299+
else:
300+
out_file = open(args.file + ".gz", mode="wb",
301+
compresslevel=compresslevel)
296302
else:
297303
base, extension = os.path.splitext(args.file)
298304
if extension != ".gz":
299305
raise ValueError(f"filename doesn't end in .gz: {args.file}")
300306
in_file = open(args.file, "rb")
301-
out_file = io.open(base, "wb")
307+
if args.stdout:
308+
out_file = sys.stdout.buffer
309+
else:
310+
out_file = io.open(base, "wb")
302311
try:
303312
while True:
304313
block = in_file.read(BUFFER_SIZE)

tests/test_igzip_cli.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,23 @@ def test_decompress_infile_outfile_error(capsysbinary):
9191
assert error.match("filename doesn't end")
9292
out, err = capsysbinary.readouterr()
9393
assert out == b''
94+
95+
96+
def test_decompress_infile_stdout(capsysbinary, tmp_path):
97+
test_gz = tmp_path / "test.gz"
98+
test_gz.write_bytes(gzip.compress(DATA))
99+
sys.argv = ['', '-cd', str(test_gz)]
100+
igzip.main()
101+
out, err = capsysbinary.readouterr()
102+
assert out == DATA
103+
assert err == b''
104+
105+
106+
def test_compress_infile_stdout(capsysbinary, tmp_path):
107+
test = tmp_path / "test"
108+
test.write_bytes(DATA)
109+
sys.argv = ['', '-c', str(test)]
110+
igzip.main()
111+
out, err = capsysbinary.readouterr()
112+
assert gzip.decompress(out) == DATA
113+
assert err == b''

0 commit comments

Comments
 (0)