a f:.@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z dgZddZd d ZejZd d ZGd ddZGdddeejZGdddeZdddZGdddZdS)z A Path-like interface for zipfiles. This codebase is shared between zipfile.Path in the stdlib and zipp in PyPI. See https://github.com/python/importlib_metadata/wiki/Development-Methodology for more detail. N) text_encoding) TranslatorPathcCstt|ddS)a2 Given a path with elements separated by posixpath.sep, generate all parents of that path. >>> list(_parents('b/d')) ['b'] >>> list(_parents('/b/d/')) ['/b'] >>> list(_parents('b/d/f/')) ['b/d', 'b'] >>> list(_parents('b')) [] >>> list(_parents('')) [] rN) itertoolsislice _ancestry)pathr 1/usr/lib/python3.9/site-packages/zipp/__init__.py_parentssr ccs2|tj}|tjr.|Vt|\}}q dS)a Given a path with elements separated by posixpath.sep, generate all elements of that path. >>> list(_ancestry('b/d')) ['b/d', 'b'] >>> list(_ancestry('/b/d/')) ['/b/d', '/b'] >>> list(_ancestry('b/d/f/')) ['b/d/f', 'b/d', 'b'] >>> list(_ancestry('b')) ['b'] >>> list(_ancestry('')) [] Multiple separators are treated like a single. >>> list(_ancestry('//b//d///f//')) ['//b//d///f', '//b//d', '//b'] N)rstrip posixpathsepsplit)r tailr r r r.s  rcCstt|j|S)zZ Return items in minuend not in subtrahend, retaining order with O(1) lookup. )r filterfalseset __contains__)ZminuendZ subtrahendr r r _differenceMsrcs4eZdZdZfddZddZfddZZS)InitializedStatez? Mix-in to save the initialization state for pickling. cs"||_||_tj|i|dSN)_InitializedState__args_InitializedState__kwargssuper__init__)selfargskwargs __class__r r rZszInitializedState.__init__cCs |j|jfSr)rrrr r r __getstate___szInitializedState.__getstate__cs|\}}tj|i|dSr)rr)rstaterrrr r __setstate__bszInitializedState.__setstate__)__name__ __module__ __qualname____doc__rr"r$ __classcell__r r rr rUs rcsleZdZdZeddZfddZddZdd Zfd d Z e d d Z e e j e j dddZZS) CompleteDirsa8 A ZipFile subclass that ensures that implied directories are always included in the namelist. >>> list(CompleteDirs._implied_dirs(['foo/bar.txt', 'foo/bar/baz.txt'])) ['foo/', 'foo/bar/'] >>> list(CompleteDirs._implied_dirs(['foo/bar.txt', 'foo/bar/baz.txt', 'foo/bar/'])) ['foo/'] cCs.tjtt|}dd|D}tt||S)Ncss|]}|tjVqdSr)rr).0pr r r uz-CompleteDirs._implied_dirs..)rchain from_iterablemapr _deduper)namesparentsZas_dirsr r r _implied_dirsrszCompleteDirs._implied_dirscst}|t||Sr)rnamelistlistr5)rr3rr r r6xs zCompleteDirs.namelistcCs t|Sr)rr6r!r r r _name_set|szCompleteDirs._name_setcCs,|}|d}||vo||v}|r(|S|S)zx If the name represents a directory, return that name as a directory (with the trailing slash). /)r8)rnamer3dirnameZ dir_matchr r r resolve_dirszCompleteDirs.resolve_dircsJzt|WStyD|dr2||vr4tj|dYS0dS)z6 Supplement getinfo for implied dirs. r9)filenameN)rgetinfoKeyErrorendswithr8zipfileZZipInfo)rr:rr r r>s  zCompleteDirs.getinfocCs:t|tr|St|tjs"||Sd|jvr0t}||_|S)zl Given a source (filename or zipfile), return an appropriate CompleteDirs subclass. r) isinstancer*rAZipFilemoder )clssourcer r r makes   zCompleteDirs.make)zfreturncCs$||D]}||dq|S)z Given a writable zip file zf, inject directory entries for any directories implied by the presence of children. r.)r5r6Zwritestr)rFrIr:r r r injectszCompleteDirs.inject)r%r&r'r( staticmethodr5r6r8r<r> classmethodrHrArDrKr)r r rr r*gs     r*cs,eZdZdZfddZfddZZS) FastLookupzV ZipFile subclass to ensure implicit dirs exist and are resolved rapidly. csBtt|jWdS1s&0Yt|_|jSr) contextlibsuppressAttributeErrorZ_FastLookup__namesrr6r!rr r r6s $ zFastLookup.namelistcsBtt|jWdS1s&0Yt|_|jSr)rOrPrQZ_FastLookup__lookuprr8r!rr r r8s $ zFastLookup._name_set)r%r&r'r(r6r8r)r r rr rNs rNcOs$tjjdk}d|}t||||fS)NZpypy)sysimplementationr:r)encodingrrZis_pypyZ stack_levelr r r _extract_text_encodings rVc@seZdZdZdZd=ddZddZdd Zd>d d d dZddZ e ddZ e ddZ e ddZ e ddZe ddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Zd9d:ZeZ e d;d<Z!d S)?ru' A :class:`importlib.resources.abc.Traversable` interface for zip files. Implements many of the features users enjoy from :class:`pathlib.Path`. Consider a zip file with this structure:: . ├── a.txt └── b ├── c.txt └── d └── e.txt >>> data = io.BytesIO() >>> zf = zipfile.ZipFile(data, 'w') >>> zf.writestr('a.txt', 'content of a') >>> zf.writestr('b/c.txt', 'content of c') >>> zf.writestr('b/d/e.txt', 'content of e') >>> zf.filename = 'mem/abcde.zip' Path accepts the zipfile object itself or a filename >>> path = Path(zf) From there, several path operations are available. Directory iteration (including the zip file itself): >>> a, b = path.iterdir() >>> a Path('mem/abcde.zip', 'a.txt') >>> b Path('mem/abcde.zip', 'b/') name property: >>> b.name 'b' join with divide operator: >>> c = b / 'c.txt' >>> c Path('mem/abcde.zip', 'b/c.txt') >>> c.name 'c.txt' Read text: >>> c.read_text(encoding='utf-8') 'content of c' existence: >>> c.exists() True >>> (b / 'missing.txt').exists() False Coercion to string: >>> import os >>> str(c).replace(os.sep, posixpath.sep) 'mem/abcde.zip/b/c.txt' At the root, ``name``, ``filename``, and ``parent`` resolve to the zipfile. >>> str(path) 'mem/abcde.zip/' >>> path.name 'abcde.zip' >>> path.filename == pathlib.Path('mem/abcde.zip') True >>> str(path.parent) 'mem' If the zipfile has no filename, such attributes are not valid and accessing them will raise an Exception. >>> zf.filename = None >>> path.name Traceback (most recent call last): ... TypeError: ... >>> path.filename Traceback (most recent call last): ... TypeError: ... >>> path.parent Traceback (most recent call last): ... TypeError: ... # workaround python/cpython#106763 >>> pass z>{self.__class__.__name__}({self.root.filename!r}, {self.at!r})cCst||_||_dS)aX Construct a Path from a ZipFile or filename. Note: When the source is an existing ZipFile object, its type (__class__) will be mutated to a specialized type. If the caller wishes to retain the original type, the caller should either create a separate ZipFile object or pass a filename. N)rNrHrootat)rrXrYr r r r5s z Path.__init__cCs(|j|jurtS|j|jf|j|jfkS)zU >>> Path(zipfile.ZipFile(io.BytesIO(), 'w')) == 'foo' False )r NotImplementedrXrY)rotherr r r __eq__Bs z Path.__eq__cCst|j|jfSr)hashrXrYr!r r r __hash__Ksz Path.__hash__rBNpwdcOs|rt||d}|s0|dkr0t||jj|j||d}d|vr`|sT|r\td|St|i|\}}}t j ||g|Ri|S)z Open this entry as text or binary following the semantics of ``pathlib.Path.open()`` by passing arguments through to io.TextIOWrapper(). rrBr_bz*encoding args invalid for binary operation) is_dirIsADirectoryErrorexistsFileNotFoundErrorrXopenrY ValueErrorrVio TextIOWrapper)rrEr`rrZzip_modestreamrUr r r rfNsz Path.opencCst|jp|jjSr)pathlib PurePosixPathrYrXr=r!r r r _basebsz Path._basecCs |jSr)rmr:r!r r r r:esz Path.namecCs |jSr)rmsuffixr!r r r rnisz Path.suffixcCs |jSr)rmsuffixesr!r r r romsz Path.suffixescCs |jSr)rmstemr!r r r rpqsz Path.stemcCst|jj|jSr)rkrrXr=joinpathrYr!r r r r=usz Path.filenamecOsZt|i|\}}}|jd|g|Ri|}|WdS1sL0YdS)NrB)rVrfread)rrrrUstrmr r r read_textyszPath.read_textcCs6|d}|WdS1s(0YdS)Nrb)rfrr)rrsr r r read_bytes~s zPath.read_bytescCst|jd|jdkSNr9)rr;rYr )rr r r r _is_childszPath._is_childcCs||j|Sr)r rX)rrYr r r _nextsz Path._nextcCs|j p|jdSrw)rYr@r!r r r rbsz Path.is_dircCs|o| Sr)rdrbr!r r r is_filesz Path.is_filecCs|j|jvSr)rYrXr8r!r r r rdsz Path.existscCs.|stdt|j|j}t|j|S)NzCan't listdir a file)rbrgr1ryrXr6filterrx)rZsubsr r r iterdirsz Path.iterdircCst|j|Sr)rkrlrYmatch)r path_patternr r r r}sz Path.matchcCs"|j|j}|jd?}t|S)z8 Return whether this path is a symlink. )rXr>rYZ external_attrstatS_ISLNK)rinforEr r r is_symlinks zPath.is_symlinkcCsV|std|t|j}tdd}t|||j}t|j t ||j S)NzUnacceptable pattern: r9)seps) rgreescaperYrcompile translate fullmatchr1ryr{rXr6)rpatternprefixtrmatchesr r r globs   z Path.globcCs|d|S)Nz**/)r)rrr r r rglobsz Path.rglobcGstt|t|j|Sr)rrelpathstrrq)rr[Zextrar r r relative_toszPath.relative_tocCst|jj|jSr)rjoinrXr=rYr!r r r __str__sz Path.__str__cCs|jj|dS)Nr!) _Path__reprformatr!r r r __repr__sz Path.__repr__cGs&tj|jg|R}||j|Sr)rrrYryrXr<)rr[nextr r r rqsz Path.joinpathcCs6|js|jjSt|jd}|r,|d7}||Srw)rYr=parentrr;r ry)rZ parent_atr r r rs z Path.parent)rW)rB)"r%r&r'r(rrr\r^rfrmpropertyr:rnrorpr=rtrvrxryrbrzrdr|r}rrrrrrrq __truediv__rr r r r rsHf        )N)r(rhrrArrOrkrrrSZ compat.py310rrr__all__r rdictfromkeysr2rrrDr*rNrVrr r r r s*   K