Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/zipp/__init__.py: 54%

162 statements  

« prev     ^ index     » next       coverage.py v7.0.1, created at 2022-12-25 06:11 +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 

93 @staticmethod 

94 def _implied_dirs(names): 

95 parents = itertools.chain.from_iterable(map(_parents, names)) 

96 as_dirs = (p + posixpath.sep for p in parents) 

97 return _dedupe(_difference(as_dirs, names)) 

98 

99 def namelist(self): 

100 names = super(CompleteDirs, self).namelist() 

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

102 

103 def _name_set(self): 

104 return set(self.namelist()) 

105 

106 def resolve_dir(self, name): 

107 """ 

108 If the name represents a directory, return that name 

109 as a directory (with the trailing slash). 

110 """ 

111 names = self._name_set() 

112 dirname = name + '/' 

113 dir_match = name not in names and dirname in names 

114 return dirname if dir_match else name 

115 

116 @classmethod 

117 def make(cls, source): 

118 """ 

119 Given a source (filename or zipfile), return an 

120 appropriate CompleteDirs subclass. 

121 """ 

122 if isinstance(source, CompleteDirs): 

123 return source 

124 

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

126 return cls(source) 

127 

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

129 if 'r' not in source.mode: 

130 cls = CompleteDirs 

131 

132 source.__class__ = cls 

133 return source 

134 

135 

136class FastLookup(CompleteDirs): 

137 """ 

138 ZipFile subclass to ensure implicit 

139 dirs exist and are resolved rapidly. 

140 """ 

141 

142 def namelist(self): 

143 with contextlib.suppress(AttributeError): 

144 return self.__names 

145 self.__names = super(FastLookup, self).namelist() 

146 return self.__names 

147 

148 def _name_set(self): 

149 with contextlib.suppress(AttributeError): 

150 return self.__lookup 

151 self.__lookup = super(FastLookup, self)._name_set() 

152 return self.__lookup 

153 

154 

155class Path: 

156 """ 

157 A pathlib-compatible interface for zip files. 

158 

159 Consider a zip file with this structure:: 

160 

161 . 

162 ├── a.txt 

163 └── b 

164 ├── c.txt 

165 └── d 

166 └── e.txt 

167 

168 >>> data = io.BytesIO() 

169 >>> zf = zipfile.ZipFile(data, 'w') 

170 >>> zf.writestr('a.txt', 'content of a') 

171 >>> zf.writestr('b/c.txt', 'content of c') 

172 >>> zf.writestr('b/d/e.txt', 'content of e') 

173 >>> zf.filename = 'mem/abcde.zip' 

174 

175 Path accepts the zipfile object itself or a filename 

176 

177 >>> root = Path(zf) 

178 

179 From there, several path operations are available. 

180 

181 Directory iteration (including the zip file itself): 

182 

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

184 >>> a 

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

186 >>> b 

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

188 

189 name property: 

190 

191 >>> b.name 

192 'b' 

193 

194 join with divide operator: 

195 

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

197 >>> c 

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

199 >>> c.name 

200 'c.txt' 

201 

202 Read text: 

203 

204 >>> c.read_text() 

205 'content of c' 

206 

207 existence: 

208 

209 >>> c.exists() 

210 True 

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

212 False 

213 

214 Coercion to string: 

215 

216 >>> import os 

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

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

219 

220 At the root, ``name``, ``filename``, and ``parent`` 

221 resolve to the zipfile. Note these attributes are not 

222 valid and will raise a ``ValueError`` if the zipfile 

223 has no filename. 

224 

225 >>> root.name 

226 'abcde.zip' 

227 >>> str(root.filename).replace(os.sep, posixpath.sep) 

228 'mem/abcde.zip' 

229 >>> str(root.parent) 

230 'mem' 

231 """ 

232 

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

234 

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

236 """ 

237 Construct a Path from a ZipFile or filename. 

238 

239 Note: When the source is an existing ZipFile object, 

240 its type (__class__) will be mutated to a 

241 specialized type. If the caller wishes to retain the 

242 original type, the caller should either create a 

243 separate ZipFile object or pass a filename. 

244 """ 

245 self.root = FastLookup.make(root) 

246 self.at = at 

247 

248 def __eq__(self, other): 

249 """ 

250 >>> Path(zipfile.ZipFile(io.BytesIO(), 'w')) == 'foo' 

251 False 

252 """ 

253 if self.__class__ is not other.__class__: 

254 return NotImplemented 

255 return (self.root, self.at) == (other.root, other.at) 

256 

257 def __hash__(self): 

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

259 

260 def open(self, mode='r', *args, pwd=None, **kwargs): 

261 """ 

262 Open this entry as text or binary following the semantics 

263 of ``pathlib.Path.open()`` by passing arguments through 

264 to io.TextIOWrapper(). 

265 """ 

266 if self.is_dir(): 

267 raise IsADirectoryError(self) 

268 zip_mode = mode[0] 

269 if not self.exists() and zip_mode == 'r': 

270 raise FileNotFoundError(self) 

271 stream = self.root.open(self.at, zip_mode, pwd=pwd) 

272 if 'b' in mode: 

273 if args or kwargs: 

274 raise ValueError("encoding args invalid for binary operation") 

275 return stream 

276 else: 

277 kwargs["encoding"] = text_encoding(kwargs.get("encoding")) 

278 return io.TextIOWrapper(stream, *args, **kwargs) 

279 

280 @property 

281 def name(self): 

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

283 

284 @property 

285 def suffix(self): 

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

287 

288 @property 

289 def suffixes(self): 

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

291 

292 @property 

293 def stem(self): 

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

295 

296 @property 

297 def filename(self): 

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

299 

300 def read_text(self, *args, **kwargs): 

301 kwargs["encoding"] = text_encoding(kwargs.get("encoding")) 

302 with self.open('r', *args, **kwargs) as strm: 

303 return strm.read() 

304 

305 def read_bytes(self): 

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

307 return strm.read() 

308 

309 def _is_child(self, path): 

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

311 

312 def _next(self, at): 

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

314 

315 def is_dir(self): 

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

317 

318 def is_file(self): 

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

320 

321 def exists(self): 

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

323 

324 def iterdir(self): 

325 if not self.is_dir(): 

326 raise ValueError("Can't listdir a file") 

327 subs = map(self._next, self.root.namelist()) 

328 return filter(self._is_child, subs) 

329 

330 def match(self, path_pattern): 

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

332 

333 def is_symlink(self): 

334 """ 

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

336 """ 

337 return False 

338 

339 def _descendants(self): 

340 for child in self.iterdir(): 

341 yield child 

342 if child.is_dir(): 

343 yield from child._descendants() 

344 

345 def glob(self, pattern): 

346 if not pattern: 

347 raise ValueError("Unacceptable pattern: {!r}".format(pattern)) 

348 

349 matches = re.compile(fnmatch.translate(pattern)).fullmatch 

350 return ( 

351 child 

352 for child in self._descendants() 

353 if matches(str(child.relative_to(self))) 

354 ) 

355 

356 def rglob(self, pattern): 

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

358 

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

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

361 

362 def __str__(self): 

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

364 

365 def __repr__(self): 

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

367 

368 def joinpath(self, *other): 

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

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

371 

372 __truediv__ = joinpath 

373 

374 @property 

375 def parent(self): 

376 if not self.at: 

377 return self.filename.parent 

378 parent_at = posixpath.dirname(self.at.rstrip('/')) 

379 if parent_at: 

380 parent_at += '/' 

381 return self._next(parent_at)