Ñò ÐOyHc@sdZddklZddkZddkZddkZddkZddkZddkZdZ dgZ e Z yei io eZ nWnej onXyeWnej oe efZnXdZeedƒo dZnde fd „ƒYZ dS( so path.py - An object representing a path to a file or directory. Example: from path import path d = path('/home/guido/bin') for f in d.files('*.py'): f.chmod(0755) This module requires Python 2.2 or later. URL: http://www.jorendorff.com/articles/python/path Author: Jason Orendorff (and others - see the url!) Date: 7 Mar 2004 iÿÿÿÿ(t generatorsNs2.0.4tpathtrtnewlinestUcBsbeZdZd„Zd„Zd„Zd„ZeZd„Ze eƒZd„Z d„Z d„Z d „Z d „Zd „Zd „ZeiiZd „Zd„Zd„Zd„Zeed[d[dƒZeed[d[dƒZeed[d[dƒZeed[d[dƒZeed[d[dƒZd„Zd„Z d„Z!d„Z"e#eidƒo+d„Z$d„Z%ee%d[d[dƒZ&nd„Z'd„Z(d „Z)d!„Z*d[d"„Z+d[d#„Z,d[d$„Z-d[d%„Z.d[d&„Z/d[d'„Z0d(„Z1d)„Z2d*d+„Z3d,„Z4e5d-„Z6d[d.d/„Z7d[d.ei8e5d0„Z9d[d.e:d1„Z;d[d.ei8e5d2„Z<eii=Z=eii>Z>eii?Z?eii@Z@eiiAZAeiiBZBe#eid3ƒoeiiCZCneiiDZDeeDd[d[d4ƒZEeiiFZFeeFd[d[d5ƒZGe#eid6ƒo%eiiHZHeeHd[d[d7ƒZIneiiJZJeeJd[d[d8ƒZKe#ed9ƒo d:„ZLnd;„ZMd<„ZNe#ed=ƒo d>„ZOne#ed?ƒo d@„ZPndA„ZQdB„ZRe#edCƒo dD„ZSndE„ZTdF„ZUdGdH„ZVdGdI„ZWdJ„ZXdK„ZYdL„ZZdM„Z[dN„Z\e#edOƒo dP„Z]ne#edQƒo dR„Z^ne#edSƒodT„Z_dU„Z`neaibZbeaicZceaidZdeaieZeeaifZfeaigZge#eadVƒo eaihZhneaiiZie#edWƒo dX„Zjne#edYƒo dZ„ZknRS(\s| Represents a filesystem path. For documentation on individual methods, consult their counterparts in os.path. cCsdti|ƒS(Nspath(%s)(t_baset__repr__(tself((smodules/path.pyRDscCstt|ƒ|ƒS(N(RR(Rtmore((smodules/path.pyt__add__HscCst|t|ƒƒS(N(RR(Rtother((smodules/path.pyt__radd__KscCsttii||ƒƒS(sŒ fp.__div__(rel) == fp / rel == fp.joinpath(rel) Join two path components, adding a separator character if needed. (Rtostjoin(Rtrel((smodules/path.pyt__div__OscCsttiƒƒS(s8 Return the current working directory as a path object. (RR tgetcwd(((smodules/path.pyRZscCsttii|ƒƒS(N(RR tabspath(R((smodules/path.pyRbscCsttii|ƒƒS(N(RR tnormcase(R((smodules/path.pyRcscCsttii|ƒƒS(N(RR tnormpath(R((smodules/path.pyRdscCsttii|ƒƒS(N(RR trealpath(R((smodules/path.pyRescCsttii|ƒƒS(N(RR t expanduser(R((smodules/path.pyRfscCsttii|ƒƒS(N(RR t expandvars(R((smodules/path.pyRgscCsttii|ƒƒS(N(RR tdirname(R((smodules/path.pyRhscCs|iƒiƒiƒS(sÚ Clean up a filename by calling expandvars(), expanduser(), and normpath() on it. This is commonly everything needed to clean up a filename read from a configuration file, for example. (RRR(R((smodules/path.pytexpandkscCstii|iƒ\}}|S(N(R Rtsplitexttname(Rtbasetext((smodules/path.pyt _get_namebasetscCs"tiit|ƒƒ\}}|S(N(R RRR(RtfR((smodules/path.pyt_get_extxscCs"tii|ƒ\}}t|ƒS(N(R Rt splitdrive(RtdriveR((smodules/path.pyt _get_drive|ss˜ This path's parent directory, as a new path object. For example, path('/usr/local/lib/libpython.so').parent == path('/usr/local/lib') s” The name of this file or directory without the full path. For example, path('/usr/local/lib/libpython.so').name == 'libpython.so' sï The same as path.name, but with one file extension stripped off. For example, path('/home/guido/python.tar.gz').name == 'python.tar.gz', but path('/home/guido/python.tar.gz').namebase == 'python.tar' s( The file extension, for example '.py'. sy The drive specifier, for example 'C:'. This is always empty on systems that don't use drive specifiers. cCs(tii|ƒ\}}t|ƒ|fS(s- p.splitpath() -> Return (p.parent, p.name). (R Rtsplit(Rtparenttchild((smodules/path.pyt splitpath scCs(tii|ƒ\}}t|ƒ|fS(s p.splitdrive() -> Return (p.drive, ). Split the drive specifier from this path. If there is no drive specifier, p.drive is empty, so the return value is simply (path(''), p). This is always the case on Unix. (R RR (RR!R((smodules/path.pyR ¥scCs(tii|ƒ\}}t|ƒ|fS(sT p.splitext() -> Return (p.stripext(), p.ext). Split the filename extension from this path and return the two parts. Either part may be empty. The extension is everything from '.' to the end of the last path segment. This has the property that if (a, b) == p.splitext(), then a + b == p. (R RR(RtfilenameR((smodules/path.pyR¯s cCs|iƒdS(sµ p.stripext() -> Remove one file extension from the path. For example, path('/home/guido/python.tar.gz').stripext() returns path('/home/guido/python.tar'). i(R(R((smodules/path.pytstripext¼stsplitunccCs(tii|ƒ\}}t|ƒ|fS(N(R RR)(Rtunctrest((smodules/path.pyR)ÅscCs"tii|ƒ\}}t|ƒS(N(R RR)(RR*R((smodules/path.pyt _get_uncshareÉssY The UNC mount point for this path. This is empty for paths on local drives. cGsttii||ŒƒS(sˆ Join two or more path components, adding a separator character (os.sep) if needed. Returns a new path object. (RR R (Rtargs((smodules/path.pytjoinpathÒscCs†g}|}x\|tijoK|tijo;|}|iƒ\}}||joPn|i|ƒqW|i|ƒ|iƒ|S(sp Return a list of the path components in this path. The first item in the list will be a path. Its value will be either os.curdir, os.pardir, empty, or the root directory of this path (for example, '/' or 'C:\'). The other items in the list will be strings. path.path.joinpath(*result) will yield the original path. (R tcurdirtpardirR&tappendtreverse(RtpartstloctprevR%((smodules/path.pytsplitallÙs     cCsttiƒƒ}|i|ƒS(s` Return this path as a relative path, based from the current working directory. (RR Rt relpathto(Rtcwd((smodules/path.pytrelpathïsc Cs|iƒ}t|ƒiƒ}|iƒiƒ}|iƒ}|dtii|dƒjo|Sd}xEt||ƒD]4\}}|tii|ƒjoPn|d7}qxWtigt|ƒ|}|||7}t|ƒdjottiƒSttii |ŒƒSdS(sÙ Return a relative path from self to dest. If there is no relative path from self to dest, for example if they reside on different drives in Windows, then this returns dest.abspath(). iiN( RRRR6R tzipR0tlenR/R ( Rtdesttorigint orig_listt dest_listtit start_segtdest_segtsegments((smodules/path.pyR7ös"  ! cCsUti|ƒ}|dj oti||ƒ}ng}|D]}|||q=~S(sN D.listdir() -> List of items in this directory. Use D.files() or D.dirs() instead if you want a listing of just files or just subdirectories. The elements of the list are path objects. With the optional 'pattern' argument, this only lists items whose names match the given pattern. N(R tlistdirtNonetfnmatchtfilter(Rtpatterntnamest_[1]R%((smodules/path.pyRDs  cCs9g}|i|ƒD]}|iƒo ||qq~S(so D.dirs() -> List of this directory's subdirectories. The elements of the list are path objects. This does not walk recursively into subdirectories (but see path.walkdirs). With the optional 'pattern' argument, this only lists directories whose names match the given pattern. For example, d.dirs('build-*'). (RDtisdir(RRHRJtp((smodules/path.pytdirs.s cCs9g}|i|ƒD]}|iƒo ||qq~S(sN D.files() -> List of the files in this directory. The elements of the list are path objects. This does not walk into subdirectories (see path.walkfiles). With the optional 'pattern' argument, this only lists files whose names match the given pattern. For example, d.files('*.pyc'). (RDtisfile(RRHRJRL((smodules/path.pytfiles;s ccsqxj|iƒD]\}|djp|i|ƒo |Vn|iƒo#x |i|ƒD] }|VqVWq q WdS(sa D.walk() -> iterator over files and subdirs, recursively. The iterator yields path objects naming each child item of this directory and its descendants. This requires that D.isdir(). This performs a depth-first traversal of the directory tree. Each directory is returned just before all its children. N(RDRERFRKtwalk(RRHR%titem((smodules/path.pyRPHs   ccs`xY|iƒD]K}|djp|i|ƒo |Vnx|i|ƒD] }|VqIWq WdS(s! D.walkdirs() -> iterator over subdirs, recursively. With the optional 'pattern' argument, this yields only directories whose names match the given pattern. For example, mydir.walkdirs('*test') yields only directories with names ending in 'test'. N(RMRERFtwalkdirs(RRHR%t subsubdir((smodules/path.pyRRYs  ccs‚x{|iƒD]m}|iƒo*|djp|i|ƒo |Vqzq |iƒo#x |i|ƒD] }|VqgWq q WdS(s D.walkfiles() -> iterator over files in D, recursively. The optional argument, pattern, limits the results to files with names that match the pattern. For example, mydir.walkfiles('*.tmp') yields only files with the .tmp extension. N(RDRNRERFRKt walkfiles(RRHR%R((smodules/path.pyRTgs    cCsti|i|ƒS(s“ Return True if self.name matches the given pattern. pattern - A filename pattern with wildcards, for example '*.py'. (RFR(RRH((smodules/path.pyRFwscCs tttit||ƒƒƒS(s Return a list of path objects that match the pattern. pattern - a path relative to this directory, with wildcards. For example, path('/users').glob('*/bin/*') returns a list of all the files users have in their bin directories. (tmapRtglobR(RRH((smodules/path.pyRVsRcCs t||ƒS(s( Open this file. Return a file object. (tfile(Rtmode((smodules/path.pytopenŒscCs/|idƒ}z|iƒSWd|iƒXdS(s: Open this file, read all bytes, return them as a string. trbN(RYtreadtclose(RR((smodules/path.pytbytesscCsI|o d}nd}|i|ƒ}z|i|ƒWd|iƒXdS(sÀ Open this file and write the given bytes to it. Default behavior is to overwrite any existing file. Call this with write_bytes(bytes, append=True) to append instead. tabtwbN(RYtwriteR\(RR]R1RXR((smodules/path.pyt write_bytes˜s tstrictcCs¶|djo/|itƒ}z|iƒSWd|iƒXnwti|d||ƒ}z|iƒ}Wd|iƒX|iddƒiddƒiddƒiddƒiddƒSdS( s4 Open this file, read it in, return the content as a string. This uses 'U' mode in Python 2.3 and later, so ' ' and ' ' are automatically translated to ' '. Optional arguments: encoding - The Unicode encoding (or character set) of the file. If present, the content of the file is decoded and returned as a unicode object; otherwise it is returned as an 8-bit str. errors - How to handle Unicode errors; see help(str.decode) for the options. Default is 'strict'. NRu u u Â…u uÂ…u
(RERYt _textmodeR[R\tcodecstreplace(RtencodingterrorsRtt((smodules/path.pyttext¨s     cCst|tƒo˜|d j oX|iddƒiddƒiddƒiddƒiddƒ}|id|ƒ}n|d jotiƒ}n|i||ƒ}nV|d jpt‚|d j o4|iddƒid dƒ}|id|ƒ}n|i||ƒd S( s¾ Write the given text to this file. The default behavior is to overwrite any existing file; to append instead, use the 'append=True' keyword argument. There are two differences between path.write_text() and path.write_bytes(): newline handling and Unicode handling. See below. Parameters: - text - str/unicode - The text to be written. - encoding - str - The Unicode encoding that will be used. This is ignored if 'text' isn't a Unicode string. - errors - str - How to handle Unicode encoding errors. Default is 'strict'. See help(unicode.encode) for the options. This is ignored if 'text' isn't a Unicode string. - linesep - keyword argument - str/unicode - The sequence of characters to be used to mark end-of-line. The default is os.linesep. You can also specify None; this means to leave all newlines as they are in 'text'. - append - keyword argument - bool - Specifies what to do if the file already exists (True: append to the end of it; False: overwrite it.) The default is False. --- Newline handling. write_text() converts all standard end-of-line sequences (' ', ' ', and ' ') to your platform's default end-of-line sequence (see os.linesep; on Windows, for example, the end-of-line marker is ' '). If you don't like your platform's default, you can override it using the 'linesep=' keyword argument. If you specifically want write_text() to preserve the newlines as-is, use 'linesep=None'. This applies to Unicode text the same as to 8-bit text, except there are three additional standard Unicode end-of-line sequences: u'…', u' …', and u'\u2028'. (This is slightly different from when you open a file for writing with fopen(filename, "w") in C or file(filename, 'w') in Python.) --- Unicode If 'text' isn't Unicode, then apart from newline handling, the bytes are written verbatim to the file. The 'encoding' and 'errors' arguments are not used and must be omitted. If 'text' is Unicode, it is first converted to bytes using the specified 'encoding' (or the default encoding if 'encoding' isn't specified). The 'errors' argument applies only to this conversion. u u u Â…u uÂ…u
s s s N( t isinstancetunicodeRERetsystgetdefaultencodingtencodetAssertionErrorRa(RRiRfRgtlinesepR1R]((smodules/path.pyt write_textÍs"@        cCs`|djo6|o/|itƒ}z|iƒSWd|iƒXn|i||ƒi|ƒSdS(s  Open this file, read all lines, return them in a list. Optional arguments: encoding - The Unicode encoding (or character set) of the file. The default is None, meaning the content of the file is read as 8-bit characters and returned as a list of (non-Unicode) str objects. errors - How to handle Unicode errors; see help(str.decode) for the options. Default is 'strict' retain - If true, retain newline characters; but all newline character combinations (' ', ' ', ' ') are translated to ' '. If false, newline characters are stripped off. Default is True. This uses 'U' mode in Python 2.3 and later. N(RERYRct readlinesR\Rit splitlines(RRfRgtretainR((smodules/path.pytlines&s c CsE|o d}nd}|i|ƒ}z x|D]þ}t|tƒ} |dj o•| oB|ddjo|d }qÙ|ddjo|d }qÙn?|dd jo|d }n |ddjo|d }n||7}n| o3|djotiƒ}n|i||ƒ}n|i|ƒq0WWd|iƒXdS(sv Write the given lines of text to this file. By default this overwrites any existing file at this path. This puts a platform-specific newline sequence on every line. See 'linesep' below. lines - A list of strings. encoding - A Unicode encoding to use. This applies only if 'lines' contains any Unicode strings. errors - How to handle errors in Unicode encoding. This also applies only to Unicode strings. linesep - The desired line-ending. This line-ending is applied to every line. If a line already has any standard line ending (' ', ' ', ' ', u'…', u' …', u'\u2028'), that will be stripped off and this will be used instead. The default is os.linesep, which is platform-dependent (' ' on Windows, ' ' on Unix, etc.) Specify None to write the lines as-is, like file.writelines(). Use the keyword argument append=True to append lines to the file. The default is to overwrite the file. Warning: When you use this with Unicode data, if the encoding of the existing data in the file is different from the encoding you specify with the encoding= parameter, the result is mixed-encoding data, which can really confuse someone trying to read the file later. R^R_iþÿÿÿu u Â…iÿÿÿÿu u uÂ…u
s s s N(u u Â…(u u uÂ…u
(s s ( RYRjRkRERlRmRnR`R\( RRuRfRgRpR1RXRtlinet isUnicode((smodules/path.pyt write_lines@s4"    tsamefiles Last access time of the file. s! Last-modified time of the file. tgetctimes Creation time of the file. s Size of the file, in bytes. taccesscCsti||ƒS(sŽ Return true if current user has access to this path. mode - One of the constants os.F_OK, os.R_OK, os.W_OK, os.X_OK (R R{(RRX((smodules/path.pyR{¤scCs ti|ƒS(s, Perform a stat() system call on this path. (R tstat(R((smodules/path.pyR|«scCs ti|ƒS(s5 Like path.stat(), but do not follow symbolic links. (R tlstat(R((smodules/path.pyR}¯ststatvfscCs ti|ƒS(s/ Perform a statvfs() system call on this path. (R R~(R((smodules/path.pyR~´stpathconfcCsti||ƒS(N(R R(RR((smodules/path.pyR¹scCsti||ƒdS(s1 Set the access and modified times of this file. N(R tutime(Rttimes((smodules/path.pyR€¿scCsti||ƒdS(N(R tchmod(RRX((smodules/path.pyR‚ÃstchowncCsti|||ƒdS(N(R Rƒ(Rtuidtgid((smodules/path.pyRƒÇscCsti||ƒdS(N(R trename(Rtnew((smodules/path.pyR†ÊscCsti||ƒdS(N(R trenames(RR‡((smodules/path.pyRˆÍsiÿcCsti||ƒdS(N(R tmkdir(RRX((smodules/path.pyR‰ÓscCsti||ƒdS(N(R tmakedirs(RRX((smodules/path.pyRŠÖscCsti|ƒdS(N(R trmdir(R((smodules/path.pyR‹ÙscCsti|ƒdS(N(R t removedirs(R((smodules/path.pyRŒÜscCs@ti|titiBdƒ}ti|ƒti|dƒdS(sw Set the access/modified times of this file to the current time. Create the file if it does not exist. i¶N(R RYtO_WRONLYtO_CREATR\R€RE(Rtfd((smodules/path.pyttouchâs cCsti|ƒdS(N(R tremove(R((smodules/path.pyR‘êscCsti|ƒdS(N(R tunlink(R((smodules/path.pyR’ístlinkcCsti||ƒdS(s9 Create a hard link at 'newpath', pointing to this file. N(R R“(Rtnewpath((smodules/path.pyR“ôstsymlinkcCsti||ƒdS(s5 Create a symbolic link at 'newlink', pointing here. N(R R•(Rtnewlink((smodules/path.pyR•ùstreadlinkcCstti|ƒƒS(s€ Return the path to which this symbolic link points. The result may be an absolute or a relative path. (RR R—(R((smodules/path.pyR—þscCs3|iƒ}|iƒo|S|i|iƒSdS(su Return the path to which this symbolic link points. The result is always an absolute path. N(R—tisabsR$R(RRL((smodules/path.pyt readlinkabss  tmovetchrootcCsti|ƒdS(N(R R›(R((smodules/path.pyR›!st startfilecCsti|ƒdS(N(R Rœ(R((smodules/path.pyRœ%sN(lt__name__t __module__t__doc__RR R Rt __truediv__Rt staticmethodRRRRRRRR RtbasenameRRRR"tpropertyRER$RtnamebaseRR!R&R RR(thasattrR)R,tuncshareR.R6R9R7RDRMRORPRRRTRFRVRYR]tFalseRaRiRpRqtTrueRuRxtexistsR˜RKRNtislinktismountRytgetatimetatimetgetmtimetmtimeRztctimetgetsizetsizeR{R|R}R~RR€R‚RƒR†RˆR‰RŠR‹RŒRR‘R’R“R•R—R™tshutiltcopyfiletcopymodetcopystattcopytcopy2tcopytreeRštrmtreeR›Rœ(((smodules/path.pyR;s                                     (        %YC                                                (RŸt __future__RRlR RFRVR³Rdt __version__t__all__tstrRRtsupports_unicode_filenamesRktAttributeErrort basestringt NameErrorRcR¥RW(((smodules/path.pyts$H