o f@sddlmZddlZddlZddlZddlZddlZddlZddlZddl Z e Z ddZ ddZ GdddZGd d d ZdS) )divisionNcCstt|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('')) [] N) itertoolsislice _ancestrypathr &/usr/lib/python3/dist-packages/zipp.py_parentssr ccsN|tj}|r!|tjkr%|Vt|\}}|r#|tjksdSdSdSdS)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)rtailr r r r%s  rcs,eZdZdZfddZeddZZS)SanitizedNamesz7 ZipFile mix-in to ensure names are sanitized. cstt|jtSN)listmap _sanitizesupernamelistself __class__r r r?szSanitizedNames.namelistcCs`dd}tjdd|tjd}|dd}|d}dt||}|s'td|d|dS) a] Ensure a relative path with posix separators and no dot names. Modeled after https://github.com/python/cpython/blob/bcc1be39cb1d04ad9fc0bd1b9193d3972835a57c/Lib/zipfile/__init__.py#L1799-L1813 but provides consistent cross-platform behavior. >>> san = SanitizedNames._sanitize >>> san('/foo/bar') 'foo/bar' >>> san('//foo.txt') 'foo.txt' >>> san('foo/.././bar.txt') 'foo/bar.txt' >>> san('foo../.bar.txt') 'foo../.bar.txt' >>> san('\\foo\\bar.txt') 'foo/bar.txt' >>> san('D:\\foo.txt') 'D/foo.txt' >>> san('\\\\server\\share\\file.txt') 'server/share/file.txt' >>> san('\\\\?\\GLOBALROOT\\Volume3') '?/GLOBALROOT/Volume3' >>> san('\\\\.\\PhysicalDrive1\\root') 'PhysicalDrive1/root' Retain any trailing slash. >>> san('abc/') 'abc/' Raises a ValueError if the result is empty. >>> san('../..') Traceback (most recent call last): ... ValueError: Empty filename cSs |o|dvS)N>...r )partr r r allowedf z)SanitizedNames._sanitize..allowedz ^([A-Z]):z\1)flags\/zEmpty filename) resub IGNORECASEreplacerjoinfilter ValueErrorendswith)namerbarecleanpartsjoinedr r r rBs$  zSanitizedNames._sanitize)__name__ __module__ __qualname____doc__r staticmethodr __classcell__r r rr r:s  rc@seZdZdZdZd,ddZeddZedd Z ed d Z d d Z ddZ ddZ ddZddZddZddZddZddZddZd d!ZeZed"d#Zed$d%Zed&d'Zd(d)Zejd*krleZd+Sd+S)-Pathu 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 = 'abcde.zip' Path accepts the zipfile object itself or a filename >>> root = Path(zf) From there, several path operations are available. Directory iteration (including the zip file itself): >>> a, b = root.iterdir() >>> a Path('abcde.zip', 'a.txt') >>> b Path('abcde.zip', 'b/') name property: >>> b.name 'b' join with divide operator: >>> c = b / 'c.txt' >>> c Path('abcde.zip', 'b/c.txt') >>> c.name 'c.txt' Read text: >>> c.read_text() 'content of c' existence: >>> c.exists() True >>> (b / 'missing.txt').exists() False Coercion to string: >>> str(c) 'abcde.zip/b/c.txt' z>{self.__class__.__name__}({self.root.filename!r}, {self.at!r})cCs,t|tjr|nt|||_||_dSr) isinstancezipfileZipFile_pathlib_compatrootat)rr=r>r r r __init__s  z Path.__init__cCs&z|WStyt|YSw)zu For path-like objects, convert to a filename for compatibility on Python 3.6.1 and earlier. ) __fspath__AttributeErrorstrrr r r r<s    zPath._pathlib_compatcCt|jj|jSr) functoolspartialr=openr>rr r r rFz Path.opencCst|jdSNr#)r basenamer>r rr r r r,rGz Path.namecOsH|}tj|g|Ri|WdS1swYdSr)rFio TextIOWrapperread)rargskwargsstrmr r r read_texts $zPath.read_textcCs4| }|WdS1swYdSr)rFrL)rrOr r r read_bytess $zPath.read_bytescCst|jd|jdkSrH)r dirnamer>r )rrr r r _is_childszPath._is_childcCs t|j|Sr)r7r=)rr>r r r _nextr z Path._nextcCs|j p |jdSrH)r>r+rr r r is_dirsz Path.is_dircCs | Sr)rUrr r r is_files z Path.is_filecCs|j|vSr)r>_namesrr r r existsz Path.existscCs,|stdt|j|}t|j|S)NzCan't listdir a file)rUr*rrTrWr)rS)rsubsr r r iterdirs z Path.iterdircCrCr)r r(r=filenamer>rr r r __str__sz Path.__str__cCs|jj|dS)Nr) _Path__reprformatrr r r __repr__rYz Path.__repr__cCsP||}t|j|}t|j|d}|}|||vr%||vr%|S|S)Nr8)r<r r(r>rWrT)raddnextnext_dirnamesr r r joinpaths  z Path.joinpathcstfddDS)Nc3s2|]}t|D] }|dvr|dVqqdS)r#N)r ).0r,parentrdr r s z%Path._implied_dirs..)more_itertoolsunique_everseenrhr rhr _implied_dirsszPath._implied_dirscCs|t||Sr)rrl)clsrdr r r _add_implied_dirsrGzPath._add_implied_dirscCs(t|jd}|r|d7}||SrH)r rRr>r rT)r parent_atr r r rg s z Path.parentcCs|tttj|jSr)rnrrrrr=rrr r r rWsz Path._names)N)r8)r1r2r3r4r^r?r5r<propertyrFr,rPrQrSrTrUrVrXr[r]r`re __truediv__rl classmethodrnrgrWsys version_info__div__r r r r r7ss@@        r7) __future__rrJrtr r:rDrr$rjtype __metaclass__r rrr7r r r r s 9