diff options
author | shadchin <shadchin@yandex-team.ru> | 2022-02-10 16:44:39 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:44:39 +0300 |
commit | e9656aae26e0358d5378e5b63dcac5c8dbe0e4d0 (patch) | |
tree | 64175d5cadab313b3e7039ebaa06c5bc3295e274 /contrib/tools/python3/src/Lib/gzip.py | |
parent | 2598ef1d0aee359b4b6d5fdd1758916d5907d04f (diff) | |
download | ydb-e9656aae26e0358d5378e5b63dcac5c8dbe0e4d0.tar.gz |
Restoring authorship annotation for <shadchin@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/tools/python3/src/Lib/gzip.py')
-rw-r--r-- | contrib/tools/python3/src/Lib/gzip.py | 130 |
1 files changed, 65 insertions, 65 deletions
diff --git a/contrib/tools/python3/src/Lib/gzip.py b/contrib/tools/python3/src/Lib/gzip.py index 360857df38..11a5f41d56 100644 --- a/contrib/tools/python3/src/Lib/gzip.py +++ b/contrib/tools/python3/src/Lib/gzip.py @@ -11,18 +11,18 @@ import builtins import io import _compression -__all__ = ["BadGzipFile", "GzipFile", "open", "compress", "decompress"] +__all__ = ["BadGzipFile", "GzipFile", "open", "compress", "decompress"] FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16 READ, WRITE = 1, 2 -_COMPRESS_LEVEL_FAST = 1 -_COMPRESS_LEVEL_TRADEOFF = 6 -_COMPRESS_LEVEL_BEST = 9 - - -def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_BEST, +_COMPRESS_LEVEL_FAST = 1 +_COMPRESS_LEVEL_TRADEOFF = 6 +_COMPRESS_LEVEL_BEST = 9 + + +def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_BEST, encoding=None, errors=None, newline=None): """Open a gzip-compressed file in binary or text mode. @@ -112,11 +112,11 @@ class _PaddedFile: def seekable(self): return True # Allows fast-forwarding even in unseekable streams - -class BadGzipFile(OSError): - """Exception raised in some cases for invalid gzip files.""" - - + +class BadGzipFile(OSError): + """Exception raised in some cases for invalid gzip files.""" + + class GzipFile(_compression.BaseStream): """The GzipFile class simulates most of the methods of a file object with the exception of the truncate() method. @@ -131,7 +131,7 @@ class GzipFile(_compression.BaseStream): myfileobj = None def __init__(self, filename=None, mode=None, - compresslevel=_COMPRESS_LEVEL_BEST, fileobj=None, mtime=None): + compresslevel=_COMPRESS_LEVEL_BEST, fileobj=None, mtime=None): """Constructor for the GzipFile class. At least one of fileobj and filename must be given a @@ -177,7 +177,7 @@ class GzipFile(_compression.BaseStream): filename = '' else: filename = os.fspath(filename) - origmode = mode + origmode = mode if mode is None: mode = getattr(fileobj, 'mode', 'rb') @@ -188,13 +188,13 @@ class GzipFile(_compression.BaseStream): self.name = filename elif mode.startswith(('w', 'a', 'x')): - if origmode is None: - import warnings - warnings.warn( - "GzipFile was opened for writing, but this will " - "change in future Python releases. " - "Specify the mode argument for opening it for writing.", - FutureWarning, 2) + if origmode is None: + import warnings + warnings.warn( + "GzipFile was opened for writing, but this will " + "change in future Python releases. " + "Specify the mode argument for opening it for writing.", + FutureWarning, 2) self.mode = WRITE self._init_write(filename) self.compress = zlib.compressobj(compresslevel, @@ -209,7 +209,7 @@ class GzipFile(_compression.BaseStream): self.fileobj = fileobj if self.mode == WRITE: - self._write_gzip_header(compresslevel) + self._write_gzip_header(compresslevel) @property def filename(self): @@ -236,7 +236,7 @@ class GzipFile(_compression.BaseStream): self.bufsize = 0 self.offset = 0 # Current file offset for seek(), tell(), etc - def _write_gzip_header(self, compresslevel): + def _write_gzip_header(self, compresslevel): self.fileobj.write(b'\037\213') # magic header self.fileobj.write(b'\010') # compression method try: @@ -257,13 +257,13 @@ class GzipFile(_compression.BaseStream): if mtime is None: mtime = time.time() write32u(self.fileobj, int(mtime)) - if compresslevel == _COMPRESS_LEVEL_BEST: - xfl = b'\002' - elif compresslevel == _COMPRESS_LEVEL_FAST: - xfl = b'\004' - else: - xfl = b'\000' - self.fileobj.write(xfl) + if compresslevel == _COMPRESS_LEVEL_BEST: + xfl = b'\002' + elif compresslevel == _COMPRESS_LEVEL_FAST: + xfl = b'\004' + else: + xfl = b'\000' + self.fileobj.write(xfl) self.fileobj.write(b'\377') if fname: self.fileobj.write(fname + b'\000') @@ -302,7 +302,7 @@ class GzipFile(_compression.BaseStream): def read1(self, size=-1): """Implements BufferedIOBase.read1() - Reads up to a buffer's worth of data if size is negative.""" + Reads up to a buffer's worth of data if size is negative.""" self._check_not_closed() if self.mode != READ: import errno @@ -432,12 +432,12 @@ class _GzipReader(_compression.DecompressReader): return False if magic != b'\037\213': - raise BadGzipFile('Not a gzipped file (%r)' % magic) + raise BadGzipFile('Not a gzipped file (%r)' % magic) (method, flag, self._last_mtime) = struct.unpack("<BBIxx", self._read_exact(8)) if method != 8: - raise BadGzipFile('Unknown compression method') + raise BadGzipFile('Unknown compression method') if flag & FEXTRA: # Read & discard the extra field, if present @@ -516,15 +516,15 @@ class _GzipReader(_compression.DecompressReader): def _read_eof(self): # We've read to the end of the file - # We check that the computed CRC and size of the + # We check that the computed CRC and size of the # uncompressed data matches the stored values. Note that the size # stored is the true file size mod 2**32. crc32, isize = struct.unpack("<II", self._read_exact(8)) if crc32 != self._crc: - raise BadGzipFile("CRC check failed %s != %s" % (hex(crc32), - hex(self._crc))) + raise BadGzipFile("CRC check failed %s != %s" % (hex(crc32), + hex(self._crc))) elif isize != (self._stream_size & 0xffffffff): - raise BadGzipFile("Incorrect length of data produced") + raise BadGzipFile("Incorrect length of data produced") # Gzip files can be padded with zeroes and still have archives. # Consume all zero bytes and set the file position to the first @@ -539,12 +539,12 @@ class _GzipReader(_compression.DecompressReader): super()._rewind() self._new_member = True -def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=None): +def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=None): """Compress data in one shot and return the compressed string. Optional argument is the compression level, in range of 0-9. """ buf = io.BytesIO() - with GzipFile(fileobj=buf, mode='wb', compresslevel=compresslevel, mtime=mtime) as f: + with GzipFile(fileobj=buf, mode='wb', compresslevel=compresslevel, mtime=mtime) as f: f.write(data) return buf.getvalue() @@ -556,41 +556,41 @@ def decompress(data): return f.read() -def main(): - from argparse import ArgumentParser - parser = ArgumentParser(description= - "A simple command line interface for the gzip module: act like gzip, " - "but do not delete the input file.") - group = parser.add_mutually_exclusive_group() - group.add_argument('--fast', action='store_true', help='compress faster') - group.add_argument('--best', action='store_true', help='compress better') - group.add_argument("-d", "--decompress", action="store_true", - help="act like gunzip instead of gzip") - - parser.add_argument("args", nargs="*", default=["-"], metavar='file') - args = parser.parse_args() - - compresslevel = _COMPRESS_LEVEL_TRADEOFF - if args.fast: - compresslevel = _COMPRESS_LEVEL_FAST - elif args.best: - compresslevel = _COMPRESS_LEVEL_BEST - - for arg in args.args: - if args.decompress: +def main(): + from argparse import ArgumentParser + parser = ArgumentParser(description= + "A simple command line interface for the gzip module: act like gzip, " + "but do not delete the input file.") + group = parser.add_mutually_exclusive_group() + group.add_argument('--fast', action='store_true', help='compress faster') + group.add_argument('--best', action='store_true', help='compress better') + group.add_argument("-d", "--decompress", action="store_true", + help="act like gunzip instead of gzip") + + parser.add_argument("args", nargs="*", default=["-"], metavar='file') + args = parser.parse_args() + + compresslevel = _COMPRESS_LEVEL_TRADEOFF + if args.fast: + compresslevel = _COMPRESS_LEVEL_FAST + elif args.best: + compresslevel = _COMPRESS_LEVEL_BEST + + for arg in args.args: + if args.decompress: if arg == "-": f = GzipFile(filename="", mode="rb", fileobj=sys.stdin.buffer) g = sys.stdout.buffer else: if arg[-3:] != ".gz": - sys.exit(f"filename doesn't end in .gz: {arg!r}") + sys.exit(f"filename doesn't end in .gz: {arg!r}") f = open(arg, "rb") g = builtins.open(arg[:-3], "wb") else: if arg == "-": f = sys.stdin.buffer - g = GzipFile(filename="", mode="wb", fileobj=sys.stdout.buffer, - compresslevel=compresslevel) + g = GzipFile(filename="", mode="wb", fileobj=sys.stdout.buffer, + compresslevel=compresslevel) else: f = builtins.open(arg, "rb") g = open(arg + ".gz", "wb") @@ -605,4 +605,4 @@ def main(): f.close() if __name__ == '__main__': - main() + main() |