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

1import io 

2import posixpath 

3import zipfile 

4import itertools 

5import contextlib 

6import pathlib 

7import re 

8import fnmatch 

9 

10from .py310compat import text_encoding 

11 

12 

13__all__ = ['Path'] 

14 

15 

16def _parents(path): 

17 """ 

18 Given a path with elements separated by 

19 posixpath.sep, generate all parents of that path. 

20 

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) 

33 

34 

35def _ancestry(path): 

36 """ 

37 Given a path with elements separated by 

38 posixpath.sep, generate all elements of that path 

39 

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) 

55 

56 

57_dedupe = dict.fromkeys 

58"""Deduplicate an iterable in original order""" 

59 

60 

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) 

67 

68 

69class InitializedState: 

70 """ 

71 Mix-in to save the initialization state for pickling. 

72 """ 

73 

74 def __init__(self, *args, **kwargs): 

75 self.__args = args 

76 self.__kwargs = kwargs 

77 super().__init__(*args, **kwargs) 

78 

79 def __getstate__(self): 

80 return self.__args, self.__kwargs 

81 

82 def __setstate__(self, state): 

83 args, kwargs = state 

84 super().__init__(*args, **kwargs) 

85 

86 

87class CompleteDirs(InitializedState, zipfile.ZipFile): 

88 """ 

89 A ZipFile subclass that ensures that implied directories 

90 are always included in the namelist. 

91 

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 """ 

97 

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)) 

103 

104 def namelist(self): 

105 names = super().namelist() 

106 return names + list(self._implied_dirs(names)) 

107 

108 def _name_set(self): 

109 return set(self.namelist()) 

110 

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 

120 

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) 

131 

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 

140 

141 if not isinstance(source, zipfile.ZipFile): 

142 return cls(source) 

143 

144 # Only allow for FastLookup when supplied zipfile is read-only 

145 if 'r' not in source.mode: 

146 cls = CompleteDirs 

147 

148 source.__class__ = cls 

149 return source 

150 

151 

152class FastLookup(CompleteDirs): 

153 """ 

154 ZipFile subclass to ensure implicit 

155 dirs exist and are resolved rapidly. 

156 """ 

157 

158 def namelist(self): 

159 with contextlib.suppress(AttributeError): 

160 return self.__names 

161 self.__names = super().namelist() 

162 return self.__names 

163 

164 def _name_set(self): 

165 with contextlib.suppress(AttributeError): 

166 return self.__lookup 

167 self.__lookup = super()._name_set() 

168 return self.__lookup 

169 

170 

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 

174 

175 

176class Path: 

177 """ 

178 A pathlib-compatible interface for zip files. 

179 

180 Consider a zip file with this structure:: 

181 

182 . 

183 ├── a.txt 

184 └── b 

185 ├── c.txt 

186 └── d 

187 └── e.txt 

188 

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' 

195 

196 Path accepts the zipfile object itself or a filename 

197 

198 >>> root = Path(zf) 

199 

200 From there, several path operations are available. 

201 

202 Directory iteration (including the zip file itself): 

203 

204 >>> a, b = root.iterdir() 

205 >>> a 

206 Path('mem/abcde.zip', 'a.txt') 

207 >>> b 

208 Path('mem/abcde.zip', 'b/') 

209 

210 name property: 

211 

212 >>> b.name 

213 'b' 

214 

215 join with divide operator: 

216 

217 >>> c = b / 'c.txt' 

218 >>> c 

219 Path('mem/abcde.zip', 'b/c.txt') 

220 >>> c.name 

221 'c.txt' 

222 

223 Read text: 

224 

225 >>> c.read_text(encoding='utf-8') 

226 'content of c' 

227 

228 existence: 

229 

230 >>> c.exists() 

231 True 

232 >>> (b / 'missing.txt').exists() 

233 False 

234 

235 Coercion to string: 

236 

237 >>> import os 

238 >>> str(c).replace(os.sep, posixpath.sep) 

239 'mem/abcde.zip/b/c.txt' 

240 

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. 

245 

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 """ 

253 

254 __repr = "{self.__class__.__name__}({self.root.filename!r}, {self.at!r})" 

255 

256 def __init__(self, root, at=""): 

257 """ 

258 Construct a Path from a ZipFile or filename. 

259 

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 

268 

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) 

277 

278 def __hash__(self): 

279 return hash((self.root, self.at)) 

280 

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) 

300 

301 @property 

302 def name(self): 

303 return pathlib.Path(self.at).name or self.filename.name 

304 

305 @property 

306 def suffix(self): 

307 return pathlib.Path(self.at).suffix or self.filename.suffix 

308 

309 @property 

310 def suffixes(self): 

311 return pathlib.Path(self.at).suffixes or self.filename.suffixes 

312 

313 @property 

314 def stem(self): 

315 return pathlib.Path(self.at).stem or self.filename.stem 

316 

317 @property 

318 def filename(self): 

319 return pathlib.Path(self.root.filename).joinpath(self.at) 

320 

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() 

325 

326 def read_bytes(self): 

327 with self.open('rb') as strm: 

328 return strm.read() 

329 

330 def _is_child(self, path): 

331 return posixpath.dirname(path.at.rstrip("/")) == self.at.rstrip("/") 

332 

333 def _next(self, at): 

334 return self.__class__(self.root, at) 

335 

336 def is_dir(self): 

337 return not self.at or self.at.endswith("/") 

338 

339 def is_file(self): 

340 return self.exists() and not self.is_dir() 

341 

342 def exists(self): 

343 return self.at in self.root._name_set() 

344 

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) 

350 

351 def match(self, path_pattern): 

352 return pathlib.Path(self.at).match(path_pattern) 

353 

354 def is_symlink(self): 

355 """ 

356 Return whether this path is a symlink. Always false (python/cpython#82102). 

357 """ 

358 return False 

359 

360 def _descendants(self): 

361 for child in self.iterdir(): 

362 yield child 

363 if child.is_dir(): 

364 yield from child._descendants() 

365 

366 def glob(self, pattern): 

367 if not pattern: 

368 raise ValueError(f"Unacceptable pattern: {pattern!r}") 

369 

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 ) 

376 

377 def rglob(self, pattern): 

378 return self.glob(f'**/{pattern}') 

379 

380 def relative_to(self, other, *extra): 

381 return posixpath.relpath(str(self), str(other.joinpath(*extra))) 

382 

383 def __str__(self): 

384 return posixpath.join(self.root.filename, self.at) 

385 

386 def __repr__(self): 

387 return self.__repr.format(self=self) 

388 

389 def joinpath(self, *other): 

390 next = posixpath.join(self.at, *other) 

391 return self._next(self.root.resolve_dir(next)) 

392 

393 __truediv__ = joinpath 

394 

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)