Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/zipp/__init__.py: 53%
171 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
1import io
2import posixpath
3import zipfile
4import itertools
5import contextlib
6import pathlib
7import re
8import fnmatch
10from .py310compat import text_encoding
13__all__ = ['Path']
16def _parents(path):
17 """
18 Given a path with elements separated by
19 posixpath.sep, generate all parents of that path.
21 >>> list(_parents('b/d'))
22 ['b']
23 >>> list(_parents('/b/d/'))
24 ['/b']
25 >>> list(_parents('b/d/f/'))
26 ['b/d', 'b']
27 >>> list(_parents('b'))
28 []
29 >>> list(_parents(''))
30 []
31 """
32 return itertools.islice(_ancestry(path), 1, None)
35def _ancestry(path):
36 """
37 Given a path with elements separated by
38 posixpath.sep, generate all elements of that path
40 >>> list(_ancestry('b/d'))
41 ['b/d', 'b']
42 >>> list(_ancestry('/b/d/'))
43 ['/b/d', '/b']
44 >>> list(_ancestry('b/d/f/'))
45 ['b/d/f', 'b/d', 'b']
46 >>> list(_ancestry('b'))
47 ['b']
48 >>> list(_ancestry(''))
49 []
50 """
51 path = path.rstrip(posixpath.sep)
52 while path and path != posixpath.sep:
53 yield path
54 path, tail = posixpath.split(path)
57_dedupe = dict.fromkeys
58"""Deduplicate an iterable in original order"""
61def _difference(minuend, subtrahend):
62 """
63 Return items in minuend not in subtrahend, retaining order
64 with O(1) lookup.
65 """
66 return itertools.filterfalse(set(subtrahend).__contains__, minuend)
69class InitializedState:
70 """
71 Mix-in to save the initialization state for pickling.
72 """
74 def __init__(self, *args, **kwargs):
75 self.__args = args
76 self.__kwargs = kwargs
77 super().__init__(*args, **kwargs)
79 def __getstate__(self):
80 return self.__args, self.__kwargs
82 def __setstate__(self, state):
83 args, kwargs = state
84 super().__init__(*args, **kwargs)
87class CompleteDirs(InitializedState, zipfile.ZipFile):
88 """
89 A ZipFile subclass that ensures that implied directories
90 are always included in the namelist.
92 >>> list(CompleteDirs._implied_dirs(['foo/bar.txt', 'foo/bar/baz.txt']))
93 ['foo/', 'foo/bar/']
94 >>> list(CompleteDirs._implied_dirs(['foo/bar.txt', 'foo/bar/baz.txt', 'foo/bar/']))
95 ['foo/']
96 """
98 @staticmethod
99 def _implied_dirs(names):
100 parents = itertools.chain.from_iterable(map(_parents, names))
101 as_dirs = (p + posixpath.sep for p in parents)
102 return _dedupe(_difference(as_dirs, names))
104 def namelist(self):
105 names = super().namelist()
106 return names + list(self._implied_dirs(names))
108 def _name_set(self):
109 return set(self.namelist())
111 def resolve_dir(self, name):
112 """
113 If the name represents a directory, return that name
114 as a directory (with the trailing slash).
115 """
116 names = self._name_set()
117 dirname = name + '/'
118 dir_match = name not in names and dirname in names
119 return dirname if dir_match else name
121 def getinfo(self, name):
122 """
123 Supplement getinfo for implied dirs.
124 """
125 try:
126 return super().getinfo(name)
127 except KeyError:
128 if not name.endswith('/') or name not in self._name_set():
129 raise
130 return zipfile.ZipInfo(filename=name)
132 @classmethod
133 def make(cls, source):
134 """
135 Given a source (filename or zipfile), return an
136 appropriate CompleteDirs subclass.
137 """
138 if isinstance(source, CompleteDirs):
139 return source
141 if not isinstance(source, zipfile.ZipFile):
142 return cls(source)
144 # Only allow for FastLookup when supplied zipfile is read-only
145 if 'r' not in source.mode:
146 cls = CompleteDirs
148 source.__class__ = cls
149 return source
152class FastLookup(CompleteDirs):
153 """
154 ZipFile subclass to ensure implicit
155 dirs exist and are resolved rapidly.
156 """
158 def namelist(self):
159 with contextlib.suppress(AttributeError):
160 return self.__names
161 self.__names = super().namelist()
162 return self.__names
164 def _name_set(self):
165 with contextlib.suppress(AttributeError):
166 return self.__lookup
167 self.__lookup = super()._name_set()
168 return self.__lookup
171def _extract_text_encoding(encoding=None, *args, **kwargs):
172 # stacklevel=3 so that the caller of the caller see any warning.
173 return text_encoding(encoding, 3), args, kwargs
176class Path:
177 """
178 A pathlib-compatible interface for zip files.
180 Consider a zip file with this structure::
182 .
183 ├── a.txt
184 └── b
185 ├── c.txt
186 └── d
187 └── e.txt
189 >>> data = io.BytesIO()
190 >>> zf = zipfile.ZipFile(data, 'w')
191 >>> zf.writestr('a.txt', 'content of a')
192 >>> zf.writestr('b/c.txt', 'content of c')
193 >>> zf.writestr('b/d/e.txt', 'content of e')
194 >>> zf.filename = 'mem/abcde.zip'
196 Path accepts the zipfile object itself or a filename
198 >>> root = Path(zf)
200 From there, several path operations are available.
202 Directory iteration (including the zip file itself):
204 >>> a, b = root.iterdir()
205 >>> a
206 Path('mem/abcde.zip', 'a.txt')
207 >>> b
208 Path('mem/abcde.zip', 'b/')
210 name property:
212 >>> b.name
213 'b'
215 join with divide operator:
217 >>> c = b / 'c.txt'
218 >>> c
219 Path('mem/abcde.zip', 'b/c.txt')
220 >>> c.name
221 'c.txt'
223 Read text:
225 >>> c.read_text(encoding='utf-8')
226 'content of c'
228 existence:
230 >>> c.exists()
231 True
232 >>> (b / 'missing.txt').exists()
233 False
235 Coercion to string:
237 >>> import os
238 >>> str(c).replace(os.sep, posixpath.sep)
239 'mem/abcde.zip/b/c.txt'
241 At the root, ``name``, ``filename``, and ``parent``
242 resolve to the zipfile. Note these attributes are not
243 valid and will raise a ``ValueError`` if the zipfile
244 has no filename.
246 >>> root.name
247 'abcde.zip'
248 >>> str(root.filename).replace(os.sep, posixpath.sep)
249 'mem/abcde.zip'
250 >>> str(root.parent)
251 'mem'
252 """
254 __repr = "{self.__class__.__name__}({self.root.filename!r}, {self.at!r})"
256 def __init__(self, root, at=""):
257 """
258 Construct a Path from a ZipFile or filename.
260 Note: When the source is an existing ZipFile object,
261 its type (__class__) will be mutated to a
262 specialized type. If the caller wishes to retain the
263 original type, the caller should either create a
264 separate ZipFile object or pass a filename.
265 """
266 self.root = FastLookup.make(root)
267 self.at = at
269 def __eq__(self, other):
270 """
271 >>> Path(zipfile.ZipFile(io.BytesIO(), 'w')) == 'foo'
272 False
273 """
274 if self.__class__ is not other.__class__:
275 return NotImplemented
276 return (self.root, self.at) == (other.root, other.at)
278 def __hash__(self):
279 return hash((self.root, self.at))
281 def open(self, mode='r', *args, pwd=None, **kwargs):
282 """
283 Open this entry as text or binary following the semantics
284 of ``pathlib.Path.open()`` by passing arguments through
285 to io.TextIOWrapper().
286 """
287 if self.is_dir():
288 raise IsADirectoryError(self)
289 zip_mode = mode[0]
290 if not self.exists() and zip_mode == 'r':
291 raise FileNotFoundError(self)
292 stream = self.root.open(self.at, zip_mode, pwd=pwd)
293 if 'b' in mode:
294 if args or kwargs:
295 raise ValueError("encoding args invalid for binary operation")
296 return stream
297 # Text mode:
298 encoding, args, kwargs = _extract_text_encoding(*args, **kwargs)
299 return io.TextIOWrapper(stream, encoding, *args, **kwargs)
301 @property
302 def name(self):
303 return pathlib.Path(self.at).name or self.filename.name
305 @property
306 def suffix(self):
307 return pathlib.Path(self.at).suffix or self.filename.suffix
309 @property
310 def suffixes(self):
311 return pathlib.Path(self.at).suffixes or self.filename.suffixes
313 @property
314 def stem(self):
315 return pathlib.Path(self.at).stem or self.filename.stem
317 @property
318 def filename(self):
319 return pathlib.Path(self.root.filename).joinpath(self.at)
321 def read_text(self, *args, **kwargs):
322 encoding, args, kwargs = _extract_text_encoding(*args, **kwargs)
323 with self.open('r', encoding, *args, **kwargs) as strm:
324 return strm.read()
326 def read_bytes(self):
327 with self.open('rb') as strm:
328 return strm.read()
330 def _is_child(self, path):
331 return posixpath.dirname(path.at.rstrip("/")) == self.at.rstrip("/")
333 def _next(self, at):
334 return self.__class__(self.root, at)
336 def is_dir(self):
337 return not self.at or self.at.endswith("/")
339 def is_file(self):
340 return self.exists() and not self.is_dir()
342 def exists(self):
343 return self.at in self.root._name_set()
345 def iterdir(self):
346 if not self.is_dir():
347 raise ValueError("Can't listdir a file")
348 subs = map(self._next, self.root.namelist())
349 return filter(self._is_child, subs)
351 def match(self, path_pattern):
352 return pathlib.Path(self.at).match(path_pattern)
354 def is_symlink(self):
355 """
356 Return whether this path is a symlink. Always false (python/cpython#82102).
357 """
358 return False
360 def _descendants(self):
361 for child in self.iterdir():
362 yield child
363 if child.is_dir():
364 yield from child._descendants()
366 def glob(self, pattern):
367 if not pattern:
368 raise ValueError(f"Unacceptable pattern: {pattern!r}")
370 matches = re.compile(fnmatch.translate(pattern)).fullmatch
371 return (
372 child
373 for child in self._descendants()
374 if matches(str(child.relative_to(self)))
375 )
377 def rglob(self, pattern):
378 return self.glob(f'**/{pattern}')
380 def relative_to(self, other, *extra):
381 return posixpath.relpath(str(self), str(other.joinpath(*extra)))
383 def __str__(self):
384 return posixpath.join(self.root.filename, self.at)
386 def __repr__(self):
387 return self.__repr.format(self=self)
389 def joinpath(self, *other):
390 next = posixpath.join(self.at, *other)
391 return self._next(self.root.resolve_dir(next))
393 __truediv__ = joinpath
395 @property
396 def parent(self):
397 if not self.at:
398 return self.filename.parent
399 parent_at = posixpath.dirname(self.at.rstrip('/'))
400 if parent_at:
401 parent_at += '/'
402 return self._next(parent_at)