|
12 | 12 |
|
13 | 13 | import bz2 |
14 | 14 | import gzip |
| 15 | +import io |
15 | 16 | import typing as ty |
16 | 17 |
|
17 | 18 | from .optpkg import optional_package |
|
30 | 31 |
|
31 | 32 | HAVE_INDEXED_GZIP = True |
32 | 33 | HAVE_ZSTD = True |
| 34 | + |
| 35 | + ModeRT = ty.Literal['r', 'rt'] |
| 36 | + ModeRB = ty.Literal['rb'] |
| 37 | + ModeWT = ty.Literal['w', 'wt'] |
| 38 | + ModeWB = ty.Literal['wb'] |
| 39 | + ModeR = ty.Union[ModeRT, ModeRB] |
| 40 | + ModeW = ty.Union[ModeWT, ModeWB] |
| 41 | + Mode = ty.Union[ModeR, ModeW] |
| 42 | + |
33 | 43 | else: |
34 | 44 | indexed_gzip, HAVE_INDEXED_GZIP, _ = optional_package('indexed_gzip') |
35 | 45 | zstd, HAVE_ZSTD, _ = optional_package(('compression.zstd', |
@@ -101,6 +111,30 @@ def gzip_open( |
101 | 111 | mtime: int = 0, |
102 | 112 | keep_open: bool = False, |
103 | 113 | ) -> gzip.GzipFile: |
| 114 | + """Open a gzip file for reading or writing. |
| 115 | +
|
| 116 | + If opening a file for reading, and ``indexed_gzip`` is available, |
| 117 | + an ``IndexedGzipFile`` is returned. |
| 118 | +
|
| 119 | + Otherwise (opening for writing, or ``indexed_gzip`` not available), |
| 120 | + a ``DeterministicGzipFile`` is returned. |
| 121 | +
|
| 122 | + Parameters: |
| 123 | + ----------- |
| 124 | +
|
| 125 | + filename : str |
| 126 | + Path of file to open. |
| 127 | + mode : Mode |
| 128 | + Opening mode - either ``rb`` or ``wb``. |
| 129 | + compresslevel: int |
| 130 | + Compression level when writing. |
| 131 | + mtime: int |
| 132 | + Modification time used when writing a file - passed to the |
| 133 | + ``DetemrinisticGzipFile``. Ignored when reading. |
| 134 | + keep_open: bool |
| 135 | + Whether to keep the file handle open between reads. Ignored when writing, |
| 136 | + or when ``indexed_gzip`` is not present. |
| 137 | + """ |
104 | 138 | if not HAVE_INDEXED_GZIP or mode != 'rb': |
105 | 139 | gzip_file = DeterministicGzipFile(filename, mode, compresslevel, mtime=mtime) |
106 | 140 |
|
|
0 commit comments