Debian Codegraph @

src/python/backports/zstd/zipfile/__init__.pyi

raw
1 import io
2 from _typeshed import SizedBuffer, StrOrBytesPath, StrPath
3 from collections.abc import Callable, Iterable, Iterator
4 from io import TextIOWrapper
5 from os import PathLike
6 from types import TracebackType
7 from typing import IO, Final, Literal, Protocol, overload, type_check_only
8 from typing_extensions import Self, TypeAlias
9
10 __all__ = [
11 "BadZipFile",
12 "BadZipfile",
13 "Path",
14 "error",
15 "ZIP_STORED",
16 "ZIP_DEFLATED",
17 "ZIP_BZIP2",
18 "ZIP_LZMA",
19 "is_zipfile",
20 "ZipInfo",
21 "ZipFile",
22 "PyZipFile",
23 "LargeZipFile",
24 "ZIP_ZSTANDARD",
25 ]
26
27 # TODO: use TypeAlias for these two when mypy bugs are fixed
28 # https://github.com/python/mypy/issues/16581
29 _DateTuple = tuple[int, int, int, int, int, int] # noqa: Y026
30 _ZipFileMode = Literal["r", "w", "x", "a"] # noqa: Y026
31
32 _ReadWriteMode: TypeAlias = Literal["r", "w"]
33
34 class BadZipFile(Exception): ...
35
36 BadZipfile = BadZipFile
37 error = BadZipfile
38
39 class LargeZipFile(Exception): ...
40
41 @type_check_only
42 class _ZipStream(Protocol):
43 def read(self, n: int, /) -> bytes: ...
44 # The following methods are optional:
45 # def seekable(self) -> bool: ...
46 # def tell(self) -> int: ...
47 # def seek(self, n: int, /) -> object: ...
48
49 # Stream shape as required by _EndRecData() and _EndRecData64().
50 @type_check_only
51 class _SupportsReadSeekTell(Protocol):
52 def read(self, n: int = ..., /) -> bytes: ...
53 def seek(self, cookie: int, whence: int, /) -> object: ...
54 def tell(self) -> int: ...
55
56 @type_check_only
57 class _ClosableZipStream(_ZipStream, Protocol):
58 def close(self) -> object: ...
59
60 class ZipExtFile(io.BufferedIOBase):
61 MAX_N: int
62 MIN_READ_SIZE: int
63 MAX_SEEK_READ: int
64 newlines: list[bytes] | None
65 mode: _ReadWriteMode
66 name: str
67 @overload
68 def __init__(
69 self, fileobj: _ClosableZipStream, mode: _ReadWriteMode, zipinfo: ZipInfo, pwd: bytes | None, close_fileobj: Literal[True]
70 ) -> None: ...
71 @overload
72 def __init__(
73 self,
74 fileobj: _ClosableZipStream,
75 mode: _ReadWriteMode,
76 zipinfo: ZipInfo,
77 pwd: bytes | None = None,
78 *,
79 close_fileobj: Literal[True],
80 ) -> None: ...
81 @overload
82 def __init__(
83 self,
84 fileobj: _ZipStream,
85 mode: _ReadWriteMode,
86 zipinfo: ZipInfo,
87 pwd: bytes | None = None,
88 close_fileobj: Literal[False] = False,
89 ) -> None: ...
90 def read(self, n: int | None = -1) -> bytes: ...
91 def readline(self, limit: int = -1) -> bytes: ... # type: ignore[override]
92 def peek(self, n: int = 1) -> bytes: ...
93 def read1(self, n: int | None) -> bytes: ... # type: ignore[override]
94 def seek(self, offset: int, whence: int = 0) -> int: ...
95
96 @type_check_only
97 class _Writer(Protocol):
98 def write(self, s: str, /) -> object: ...
99
100 @type_check_only
101 class _ZipReadable(Protocol):
102 def seek(self, offset: int, whence: int = 0, /) -> int: ...
103 def read(self, n: int = -1, /) -> bytes: ...
104
105 @type_check_only
106 class _ZipTellable(Protocol):
107 def tell(self) -> int: ...
108
109 @type_check_only
110 class _ZipReadableTellable(_ZipReadable, _ZipTellable, Protocol): ...
111
112 @type_check_only
113 class _ZipWritable(Protocol):
114 def flush(self) -> None: ...
115 def close(self) -> None: ...
116 def write(self, b: bytes, /) -> int: ...
117
118 class ZipFile:
119 filename: str | None
120 debug: int
121 comment: bytes
122 filelist: list[ZipInfo]
123 fp: IO[bytes] | None
124 NameToInfo: dict[str, ZipInfo]
125 start_dir: int # undocumented
126 compression: int # undocumented
127 compresslevel: int | None # undocumented
128 mode: _ZipFileMode # undocumented
129 pwd: bytes | None # undocumented
130 @overload
131 def __init__(
132 self,
133 file: StrPath | IO[bytes],
134 mode: _ZipFileMode = "r",
135 compression: int = 0,
136 allowZip64: bool = True,
137 compresslevel: int | None = None,
138 *,
139 strict_timestamps: bool = True,
140 metadata_encoding: str | None = None,
141 ) -> None: ...
142 # metadata_encoding is only allowed for read mode
143 @overload
144 def __init__(
145 self,
146 file: StrPath | _ZipReadable,
147 mode: Literal["r"] = "r",
148 compression: int = 0,
149 allowZip64: bool = True,
150 compresslevel: int | None = None,
151 *,
152 strict_timestamps: bool = True,
153 metadata_encoding: str | None = None,
154 ) -> None: ...
155 @overload
156 def __init__(
157 self,
158 file: StrPath | _ZipWritable,
159 mode: Literal["w", "x"],
160 compression: int = 0,
161 allowZip64: bool = True,
162 compresslevel: int | None = None,
163 *,
164 strict_timestamps: bool = True,
165 metadata_encoding: None = None,
166 ) -> None: ...
167 @overload
168 def __init__(
169 self,
170 file: StrPath | _ZipReadableTellable,
171 mode: Literal["a"],
172 compression: int = 0,
173 allowZip64: bool = True,
174 compresslevel: int | None = None,
175 *,
176 strict_timestamps: bool = True,
177 metadata_encoding: None = None,
178 ) -> None: ...
179
180 def __enter__(self) -> Self: ...
181 def __exit__(
182 self, type: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
183 ) -> None: ...
184 def close(self) -> None: ...
185 def getinfo(self, name: str) -> ZipInfo: ...
186 def infolist(self) -> list[ZipInfo]: ...
187 def namelist(self) -> list[str]: ...
188 def open(
189 self, name: str | ZipInfo, mode: _ReadWriteMode = "r", pwd: bytes | None = None, *, force_zip64: bool = False
190 ) -> IO[bytes]: ...
191 def extract(self, member: str | ZipInfo, path: StrPath | None = None, pwd: bytes | None = None) -> str: ...
192 def extractall(
193 self, path: StrPath | None = None, members: Iterable[str | ZipInfo] | None = None, pwd: bytes | None = None
194 ) -> None: ...
195 def printdir(self, file: _Writer | None = None) -> None: ...
196 def setpassword(self, pwd: bytes) -> None: ...
197 def read(self, name: str | ZipInfo, pwd: bytes | None = None) -> bytes: ...
198 def testzip(self) -> str | None: ...
199 def write(
200 self,
201 filename: StrPath,
202 arcname: StrPath | None = None,
203 compress_type: int | None = None,
204 compresslevel: int | None = None,
205 ) -> None: ...
206 def writestr(
207 self,
208 zinfo_or_arcname: str | ZipInfo,
209 data: SizedBuffer | str,
210 compress_type: int | None = None,
211 compresslevel: int | None = None,
212 ) -> None: ...
213 def mkdir(self, zinfo_or_directory_name: str | ZipInfo, mode: int = 0o777) -> None: ...
214
215 def __del__(self) -> None: ...
216
217 class PyZipFile(ZipFile):
218 def __init__(
219 self, file: str | IO[bytes], mode: _ZipFileMode = "r", compression: int = 0, allowZip64: bool = True, optimize: int = -1
220 ) -> None: ...
221 def writepy(self, pathname: str, basename: str = "", filterfunc: Callable[[str], bool] | None = None) -> None: ...
222
223 class ZipInfo:
224 __slots__ = (
225 "orig_filename",
226 "filename",
227 "date_time",
228 "compress_type",
229 "compress_level",
230 "comment",
231 "extra",
232 "create_system",
233 "create_version",
234 "extract_version",
235 "reserved",
236 "flag_bits",
237 "volume",
238 "internal_attr",
239 "external_attr",
240 "header_offset",
241 "CRC",
242 "compress_size",
243 "file_size",
244 "_raw_time",
245 "_end_offset",
246 )
247 filename: str
248 date_time: _DateTuple
249 compress_type: int
250 comment: bytes
251 extra: bytes
252 create_system: int
253 create_version: int
254 extract_version: int
255 reserved: int
256 flag_bits: int
257 volume: int
258 internal_attr: int
259 external_attr: int
260 header_offset: int
261 CRC: int
262 compress_size: int
263 file_size: int
264 orig_filename: str # undocumented
265 compress_level: int | None
266
267 def __init__(self, filename: str = "NoName", date_time: _DateTuple = (1980, 1, 1, 0, 0, 0)) -> None: ...
268 @classmethod
269 def from_file(cls, filename: StrPath, arcname: StrPath | None = None, *, strict_timestamps: bool = True) -> Self: ...
270 def is_dir(self) -> bool: ...
271 def FileHeader(self, zip64: bool | None = None) -> bytes: ...
272 def _for_archive(self, archive: ZipFile) -> Self: ...
273
274 from zipfile._path import CompleteDirs as CompleteDirs, Path as Path
275
276 def is_zipfile(filename: StrOrBytesPath | _SupportsReadSeekTell) -> bool: ...
277
278 ZIP64_LIMIT: Final[int]
279 ZIP_FILECOUNT_LIMIT: Final[int]
280 ZIP_MAX_COMMENT: Final[int]
281
282 ZIP_STORED: Final = 0
283 ZIP_DEFLATED: Final = 8
284 ZIP_BZIP2: Final = 12
285 ZIP_LZMA: Final = 14
286 ZIP_ZSTANDARD: Final = 93
287
288 DEFAULT_VERSION: Final[int]
289 ZIP64_VERSION: Final[int]
290 BZIP2_VERSION: Final[int]
291 LZMA_VERSION: Final[int]
292 ZSTANDARD_VERSION: Final[int]
293 MAX_EXTRACT_VERSION: Final[int]