Open a Zstandard compressed file in binary mode. *file* can be either an file-like object, or a file name to open. *mode* can be 'r' for reading (default), 'w' for (over)writing, 'x' for creating exclusively, or 'a' for appending. These can equivalently be given as
(self, file, /, mode='r', *,
level=None, options=None, zstd_dict=None)
| 31 | FLUSH_FRAME = ZstdCompressor.FLUSH_FRAME |
| 32 | |
| 33 | def __init__(self, file, /, mode='r', *, |
| 34 | level=None, options=None, zstd_dict=None): |
| 35 | """Open a Zstandard compressed file in binary mode. |
| 36 | |
| 37 | *file* can be either an file-like object, or a file name to open. |
| 38 | |
| 39 | *mode* can be 'r' for reading (default), 'w' for (over)writing, 'x' for |
| 40 | creating exclusively, or 'a' for appending. These can equivalently be |
| 41 | given as 'rb', 'wb', 'xb' and 'ab' respectively. |
| 42 | |
| 43 | *level* is an optional int specifying the compression level to use, |
| 44 | or COMPRESSION_LEVEL_DEFAULT if not given. |
| 45 | |
| 46 | *options* is an optional dict for advanced compression parameters. |
| 47 | See CompressionParameter and DecompressionParameter for the possible |
| 48 | options. |
| 49 | |
| 50 | *zstd_dict* is an optional ZstdDict object, a pre-trained Zstandard |
| 51 | dictionary. See train_dict() to train ZstdDict on sample data. |
| 52 | """ |
| 53 | self._fp = None |
| 54 | self._close_fp = False |
| 55 | self._mode = _MODE_CLOSED |
| 56 | self._buffer = None |
| 57 | |
| 58 | if not isinstance(mode, str): |
| 59 | raise ValueError('mode must be a str') |
| 60 | if options is not None and not isinstance(options, dict): |
| 61 | raise TypeError('options must be a dict or None') |
| 62 | mode = mode.removesuffix('b') # handle rb, wb, xb, ab |
| 63 | if mode == 'r': |
| 64 | if level is not None: |
| 65 | raise TypeError('level is illegal in read mode') |
| 66 | self._mode = _MODE_READ |
| 67 | elif mode in {'w', 'a', 'x'}: |
| 68 | if level is not None and not isinstance(level, int): |
| 69 | raise TypeError('level must be int or None') |
| 70 | self._mode = _MODE_WRITE |
| 71 | self._compressor = ZstdCompressor(level=level, options=options, |
| 72 | zstd_dict=zstd_dict) |
| 73 | self._pos = 0 |
| 74 | else: |
| 75 | raise ValueError(f'Invalid mode: {mode!r}') |
| 76 | |
| 77 | if isinstance(file, (str, bytes, PathLike)): |
| 78 | self._fp = io.open(file, f'{mode}b') |
| 79 | self._close_fp = True |
| 80 | elif ((mode == 'r' and hasattr(file, 'read')) |
| 81 | or (mode != 'r' and hasattr(file, 'write'))): |
| 82 | self._fp = file |
| 83 | else: |
| 84 | raise TypeError('file must be a file-like object ' |
| 85 | 'or a str, bytes, or PathLike object') |
| 86 | |
| 87 | if self._mode == _MODE_READ: |
| 88 | raw = _streams.DecompressReader( |
| 89 | self._fp, |
| 90 | ZstdDecompressor, |
nothing calls this directly
no test coverage detected