U e^,@sddlZddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z dgZ ddZ dd ZejZd d ZGd d d ZGdddeejZGdddeZdddZGdddZdS)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 ;/opt/hc_python/lib/python3.8/site-packages/zipp/__init__.py_parentssr ccs4|tj}|r0|tjkr0|Vt|\}}q dS)aR 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('')) [] 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__)minuend subtrahendr r r _difference>srcs4eZdZdZfddZddZfddZZS)InitializedStatez? Mix-in to save the initialization state for pickling. cs||_||_tj||dSN)_InitializedState__args_InitializedState__kwargssuper__init__)selfargskwargs __class__r r rKszInitializedState.__init__cCs |j|jfSr)rrrr r r __getstate__PszInitializedState.__getstate__cs|\}}tj||dSr)rr)rstaterr r!r r __setstate__SszInitializedState.__setstate__)__name__ __module__ __qualname____doc__rr$r& __classcell__r r r!r rFs 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 fsz-CompleteDirs._implied_dirs..)rchain from_iterablemapr _deduper)namesparentsas_dirsr r r _implied_dirscszCompleteDirs._implied_dirscst}|t||Sr)rnamelistlistr7)rr4r!r r r8is zCompleteDirs.namelistcCs t|Sr)rr8r#r r r _name_setmszCompleteDirs._name_setcCs,|}|d}||ko||k}|r(|S|S)zx If the name represents a directory, return that name as a directory (with the trailing slash). /)r:)rnamer4dirname dir_matchr r r resolve_dirpszCompleteDirs.resolve_dircsLzt|WStk rF|dr4||kr6tj|dYSXdS)z6 Supplement getinfo for implied dirs. r;filenameN)rgetinfoKeyErrorendswithr:zipfileZipInfo)rr<r!r r rBzs zCompleteDirs.getinfocCs:t|tr|St|tjs"||Sd|jkr0t}||_|S)zl Given a source (filename or zipfile), return an appropriate CompleteDirs subclass. r) isinstancer,rEZipFilemoder")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. )r7r8writestr)rKrNr<r r r injectszCompleteDirs.inject)r'r(r)r* staticmethodr7r8r:r?rB classmethodrMrErIrRr+r r r!r r,Xs     r,cs,eZdZdZfddZfddZZS) FastLookupzV ZipFile subclass to ensure implicit dirs exist and are resolved rapidly. c s6tt|jW5QRSQRXt|_|jSr) contextlibsuppressAttributeError_FastLookup__namesrr8r#r!r r r8s  zFastLookup.namelistc s6tt|jW5QRSQRXt|_|jSr)rVrWrX_FastLookup__lookuprr:r#r!r r r:s  zFastLookup._name_set)r'r(r)r*r8r:r+r r r!r rUs rUcOs$tjjdk}d|}t||||fS)Npypy)sysimplementationr<r)encodingrr is_pypyZ stack_levelr r r _extract_text_encodings rac@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 pathlib-compatible interface for zip files. 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 attribtues 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)rUrMrootat)rrcrdr r r r#s z Path.__init__cCs(|j|jk rtS|j|jf|j|jfkS)zU >>> Path(zipfile.ZipFile(io.BytesIO(), 'w')) == 'foo' False )r"NotImplementedrcrd)rotherr r r __eq__0s z Path.__eq__cCst|j|jfSr)hashrcrdr#r r r __hash__9sz Path.__hash__rGNpwdcOs|rt||d}|s0|dkr0t||jj|j||d}d|kr`|sT|r\td|St||\}}}t j ||f||S)z Open this entry as text or binary following the semantics of ``pathlib.Path.open()`` by passing arguments through to io.TextIOWrapper(). rrGrjbz*encoding args invalid for binary operation) is_dirIsADirectoryErrorexistsFileNotFoundErrorrcopenrd ValueErrorraio TextIOWrapper)rrJrkrr Zzip_modestreamr_r r r rq<sz Path.opencCst|jp|jjSr)pathlib PurePosixPathrdrcrAr#r r r _basePsz Path._basecCs |jSr)rxr<r#r r r r<Ssz Path.namecCs |jSr)rxsuffixr#r r r ryWsz Path.suffixcCs |jSr)rxsuffixesr#r r r rz[sz Path.suffixescCs |jSr)rxstemr#r r r r{_sz Path.stemcCst|jj|jSr)rvrrcrAjoinpathrdr#r r r rAcsz Path.filenamec OsDt||\}}}|jd|f||}|W5QRSQRXdS)NrG)rarqread)rrr r_strmr r r read_textgszPath.read_textc Cs*|d}|W5QRSQRXdS)Nrb)rqr})rr~r r r read_bytesls zPath.read_bytescCst|jd|jdkSNr;)rr=rdr )rr r r r _is_childpszPath._is_childcCs||j|Sr)r"rc)rrdr r r _nextssz Path._nextcCs|j p|jdSr)rdrDr#r r r rmvsz Path.is_dircCs|o| Sr)rormr#r r r is_fileysz Path.is_filecCs|j|jkSr)rdrcr:r#r r r ro|sz Path.existscCs.|stdt|j|j}t|j|S)NzCan't listdir a file)rmrrr2rrcr8filterr)rsubsr r r iterdirsz Path.iterdircCst|j|Sr)rvrwrdmatch)r path_patternr r r rsz Path.matchcCsdS)z] Return whether this path is a symlink. Always false (python/cpython#82102). Fr r#r r r is_symlinkszPath.is_symlinkcCsb|std|t|j}tdd}t|||j}dd|jj D}t |j t ||S)NzUnacceptable pattern: r;)sepscss|] }|jVqdSrr@)r-datar r r r/szPath.glob..) rrreescaperdrcompile translate fullmatchrcfilelistr2rr)rpatternprefixtrmatchesr4r r r globs  z Path.globcCs|d|S)Nz**/)r)rrr r r rglobsz Path.rglobcGstt|t|j|Sr)rrelpathstrr|)rrfextrar r r relative_toszPath.relative_tocCst|jj|jSr)rjoinrcrArdr#r r r __str__sz Path.__str__cCs|jj|dS)Nr#) _Path__reprformatr#r r r __repr__sz Path.__repr__cGs$tj|jf|}||j|Sr)rrrdrrcr?)rrfnextr r r r|sz Path.joinpathcCs6|js|jjSt|jd}|r,|d7}||Sr)rdrAparentrr=r r)r parent_atr r r rs z Path.parent)rb)rG)"r'r(r)r*rrrgrirqrxpropertyr<ryrzr{rArrrrrmrrorrrrrrrrr| __truediv__rr r r r rsHc        )N)rsrrErrVrvrr]Z compat.py310rrr__all__r rdictfromkeysr3rrrIr,rUrarr r r r s&  K