|
| 1 | +# This file is part of the Dataphile package. |
| 2 | +# |
| 3 | +# This program is free software: you can redistribute it and/or modify it under the |
| 4 | +# terms of the Apache License (v2.0) as published by the Apache Software Foundation. |
| 5 | +# |
| 6 | +# This program is distributed in the hope that it will be useful, but WITHOUT ANY |
| 7 | +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 8 | +# PARTICULAR PURPOSE. See the Apache License for more details. |
| 9 | +# |
| 10 | +# You should have received a copy of the Apache License along with this program. |
| 11 | +# If not, see <https://www.apache.org/licenses/LICENSE-2.0>. |
| 12 | + |
| 13 | +"""Apply compression/decompression to data.""" |
| 14 | + |
| 15 | +# standard libs |
| 16 | +import sys |
| 17 | +import argparse |
| 18 | +import platform |
| 19 | + |
| 20 | +# internal libs |
| 21 | +from ..io.stream import BinaryStream |
| 22 | +from ..io.compression import compress, decompress |
| 23 | + |
| 24 | +if platform.system() == 'Windows': |
| 25 | + # FIXME: how do we ignore broken pipes on windows? |
| 26 | + pass |
| 27 | +else: |
| 28 | + from signal import signal, SIGPIPE, SIG_DFL # ignore broken pipes |
| 29 | + signal(SIGPIPE, SIG_DFL) |
| 30 | + |
| 31 | + |
| 32 | +parser = argparse.ArgumentParser(description=__doc__.split('\n')[0].strip()) |
| 33 | + |
| 34 | +parser.add_argument('sources', metavar='FILE', default=[], nargs='?', |
| 35 | + help='path to file') |
| 36 | + |
| 37 | +parser.add_argument('-b', '--buffersize', type=float, default=1.0, |
| 38 | + help='buffer size (in MB)') |
| 39 | +parser.add_argument('--encoding', type=str, default=None, |
| 40 | + help='specification (e.g., "utf-8")') |
| 41 | +parser.add_argument('-l', '--level', type=int, default=6, |
| 42 | + help='compression level to use') |
| 43 | + |
| 44 | +action_group = parser.add_mutually_exclusive_group() |
| 45 | +action_group.add_argument('-z', '--compress', action='store_true', |
| 46 | + help='compress data') |
| 47 | +action_group.add_argument('-d', '--decompress', action='store_true', |
| 48 | + help='decompress data') |
| 49 | + |
| 50 | +SCHEMES = 'gzip', 'bzip', 'lzma' |
| 51 | +spec_group = parser.add_mutually_exclusive_group() |
| 52 | +spec_group.add_argument('--gzip', action='store_true', |
| 53 | + help='use gzip/zlib compression algorithm') |
| 54 | +spec_group.add_argument('--bzip', action='store_true', |
| 55 | + help='use bzip2 compression algorithm') |
| 56 | +spec_group.add_argument('--lzma', action='store_true', |
| 57 | + help='use lzma compression algorithm') |
| 58 | + |
| 59 | + |
| 60 | +def main() -> int: |
| 61 | + """Entry point for 'compress'.""" |
| 62 | + |
| 63 | + try: |
| 64 | + argv = parser.parse_args() |
| 65 | + buffersize = int(argv.buffersize * 1024**2) |
| 66 | + |
| 67 | + action = compress if argv.decompress is False else decompress |
| 68 | + schemes = {getattr(argv, scheme): scheme for scheme in SCHEMES} |
| 69 | + if True not in schemes: |
| 70 | + kind = 'gzip' |
| 71 | + else: |
| 72 | + kind = schemes[True] |
| 73 | + |
| 74 | + options = {'kind': kind, 'encoding': argv.encoding} |
| 75 | + if argv.compress: |
| 76 | + options['level'] = argv.level |
| 77 | + |
| 78 | + writer = sys.stdout if argv.encoding is not None else sys.stdout.buffer |
| 79 | + with BinaryStream(*argv.sources) as stream: |
| 80 | + for buff in action(stream.iterbuffers(buffersize), **options): |
| 81 | + writer.write(buff) |
| 82 | + |
| 83 | + except KeyboardInterrupt: |
| 84 | + pass |
| 85 | + |
| 86 | + finally: |
| 87 | + sys.stdout.buffer.flush() |
| 88 | + |
| 89 | + return 0 |
0 commit comments