<s> """<STR_LIT:U+0020>""" <EOL> import os , sys , posixpath <EOL> import py <EOL> iswin32 = sys . platform == "<STR_LIT:win32>" or ( getattr ( os , '<STR_LIT>' , False ) == '<STR_LIT>' ) <EOL> class Checkers : <EOL> _depend_on_existence = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:file>' <EOL> def __init__ ( self , path ) : <EOL> self . path = path <EOL> def dir ( self ) : <EOL> raise NotImplementedError <EOL> def file ( self ) : <EOL> raise NotImplementedError <EOL> def dotfile ( self ) : <EOL> return self . path . basename . startswith ( '<STR_LIT:.>' ) <EOL> def ext ( self , arg ) : <EOL> if not arg . startswith ( '<STR_LIT:.>' ) : <EOL> arg = '<STR_LIT:.>' + arg <EOL> return self . path . ext == arg <EOL> def exists ( self ) : <EOL> raise NotImplementedError <EOL> def basename ( self , arg ) : <EOL> return self . path . basename == arg <EOL> def basestarts ( self , arg ) : <EOL> return self . path . basename . startswith ( arg ) <EOL> def relto ( self , arg ) : <EOL> return self . path . relto ( arg ) <EOL> def fnmatch ( self , arg ) : <EOL> return self . path . fnmatch ( arg ) <EOL> def endswith ( self , arg ) : <EOL> return str ( self . path ) . endswith ( arg ) <EOL> def _evaluate ( self , kw ) : <EOL> for name , value in kw . items ( ) : <EOL> invert = False <EOL> meth = None <EOL> try : <EOL> meth = getattr ( self , name ) <EOL> except AttributeError : <EOL> if name [ : <NUM_LIT:3> ] == '<STR_LIT>' : <EOL> invert = True <EOL> try : <EOL> meth = getattr ( self , name [ <NUM_LIT:3> : ] ) <EOL> except AttributeError : <EOL> pass <EOL> if meth is None : <EOL> raise TypeError ( <EOL> "<STR_LIT>" % ( name , self . path ) ) <EOL> try : <EOL> if py . code . getrawcode ( meth ) . co_argcount > <NUM_LIT:1> : <EOL> if ( not meth ( value ) ) ^ invert : <EOL> return False <EOL> else : <EOL> if bool ( value ) ^ bool ( meth ( ) ) ^ invert : <EOL> return False <EOL> except ( py . error . ENOENT , py . error . ENOTDIR , py . error . EBUSY ) : <EOL> for name in self . _depend_on_existence : <EOL> if name in kw : <EOL> if kw . get ( name ) : <EOL> return False <EOL> name = '<STR_LIT>' + name <EOL> if name in kw : <EOL> if not kw . get ( name ) : <EOL> return False <EOL> return True <EOL> class NeverRaised ( Exception ) : <EOL> pass <EOL> class PathBase ( object ) : <EOL> """<STR_LIT>""" <EOL> Checkers = Checkers <EOL> def __div__ ( self , other ) : <EOL> return self . join ( str ( other ) ) <EOL> __truediv__ = __div__ <EOL> def basename ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _getbyspec ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> basename = property ( basename , None , None , basename . __doc__ ) <EOL> def dirname ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _getbyspec ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> dirname = property ( dirname , None , None , dirname . __doc__ ) <EOL> def purebasename ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _getbyspec ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> purebasename = property ( purebasename , None , None , purebasename . __doc__ ) <EOL> def ext ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _getbyspec ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> ext = property ( ext , None , None , ext . __doc__ ) <EOL> def dirpath ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . new ( basename = '<STR_LIT>' ) . join ( * args , ** kwargs ) <EOL> def read_binary ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . open ( '<STR_LIT:rb>' ) as f : <EOL> return f . read ( ) <EOL> def read_text ( self , encoding ) : <EOL> """<STR_LIT>""" <EOL> with self . open ( "<STR_LIT:r>" , encoding = encoding ) as f : <EOL> return f . read ( ) <EOL> def read ( self , mode = '<STR_LIT:r>' ) : <EOL> """<STR_LIT>""" <EOL> with self . open ( mode ) as f : <EOL> return f . read ( ) <EOL> def readlines ( self , cr = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if not cr : <EOL> content = self . read ( '<STR_LIT>' ) <EOL> return content . split ( '<STR_LIT:\n>' ) <EOL> else : <EOL> f = self . open ( '<STR_LIT>' ) <EOL> try : <EOL> return f . readlines ( ) <EOL> finally : <EOL> f . close ( ) <EOL> def load ( self ) : <EOL> """<STR_LIT>""" <EOL> f = self . open ( '<STR_LIT:rb>' ) <EOL> try : <EOL> return py . error . checked_call ( py . std . pickle . load , f ) <EOL> finally : <EOL> f . close ( ) <EOL> def move ( self , target ) : <EOL> """<STR_LIT>""" <EOL> if target . relto ( self ) : <EOL> raise py . error . EINVAL ( target , <EOL> "<STR_LIT>" ) <EOL> try : <EOL> self . rename ( target ) <EOL> except py . error . EXDEV : <EOL> self . copy ( target ) <EOL> self . remove ( ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return repr ( str ( self ) ) <EOL> def check ( self , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if not kw : <EOL> kw = { '<STR_LIT>' : <NUM_LIT:1> } <EOL> return self . Checkers ( self ) . _evaluate ( kw ) <EOL> def fnmatch ( self , pattern ) : <EOL> """<STR_LIT>""" <EOL> return FNMatcher ( pattern ) ( self ) <EOL> def relto ( self , relpath ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( relpath , ( str , PathBase ) ) : <EOL> raise TypeError ( "<STR_LIT>" % ( relpath , ) ) <EOL> strrelpath = str ( relpath ) <EOL> if strrelpath and strrelpath [ - <NUM_LIT:1> ] != self . sep : <EOL> strrelpath += self . sep <EOL> strself = self . strpath <EOL> if sys . platform == "<STR_LIT:win32>" or getattr ( os , '<STR_LIT>' , None ) == '<STR_LIT>' : <EOL> if os . path . normcase ( strself ) . startswith ( <EOL> os . path . normcase ( strrelpath ) ) : <EOL> return strself [ len ( strrelpath ) : ] <EOL> elif strself . startswith ( strrelpath ) : <EOL> return strself [ len ( strrelpath ) : ] <EOL> return "<STR_LIT>" <EOL> def ensure_dir ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> return self . ensure ( * args , ** { "<STR_LIT>" : True } ) <EOL> def bestrelpath ( self , dest ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if self == dest : <EOL> return os . curdir <EOL> base = self . common ( dest ) <EOL> if not base : <EOL> return str ( dest ) <EOL> self2base = self . relto ( base ) <EOL> reldest = dest . relto ( base ) <EOL> if self2base : <EOL> n = self2base . count ( self . sep ) + <NUM_LIT:1> <EOL> else : <EOL> n = <NUM_LIT:0> <EOL> l = [ os . pardir ] * n <EOL> if reldest : <EOL> l . append ( reldest ) <EOL> target = dest . sep . join ( l ) <EOL> return target <EOL> except AttributeError : <EOL> return str ( dest ) <EOL> def exists ( self ) : <EOL> return self . check ( ) <EOL> def isdir ( self ) : <EOL> return self . check ( dir = <NUM_LIT:1> ) <EOL> def isfile ( self ) : <EOL> return self . check ( file = <NUM_LIT:1> ) <EOL> def parts ( self , reverse = False ) : <EOL> """<STR_LIT>""" <EOL> current = self <EOL> l = [ self ] <EOL> while <NUM_LIT:1> : <EOL> last = current <EOL> current = current . dirpath ( ) <EOL> if last == current : <EOL> break <EOL> l . append ( current ) <EOL> if not reverse : <EOL> l . reverse ( ) <EOL> return l <EOL> def common ( self , other ) : <EOL> """<STR_LIT>""" <EOL> last = None <EOL> for x , y in zip ( self . parts ( ) , other . parts ( ) ) : <EOL> if x != y : <EOL> return last <EOL> last = x <EOL> return last <EOL> def __add__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return self . new ( basename = self . basename + str ( other ) ) <EOL> def __cmp__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return cmp ( self . strpath , other . strpath ) <EOL> except AttributeError : <EOL> return cmp ( str ( self ) , str ( other ) ) <EOL> def __lt__ ( self , other ) : <EOL> try : <EOL> return self . strpath < other . strpath <EOL> except AttributeError : <EOL> return str ( self ) < str ( other ) <EOL> def visit ( self , fil = None , rec = None , ignore = NeverRaised , bf = False , sort = False ) : <EOL> """<STR_LIT>""" <EOL> for x in Visitor ( fil , rec , ignore , bf , sort ) . gen ( self ) : <EOL> yield x <EOL> def _sortlist ( self , res , sort ) : <EOL> if sort : <EOL> if hasattr ( sort , '<STR_LIT>' ) : <EOL> res . sort ( sort ) <EOL> else : <EOL> res . sort ( ) <EOL> def samefile ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return self . strpath == str ( other ) <EOL> class Visitor : <EOL> def __init__ ( self , fil , rec , ignore , bf , sort ) : <EOL> if isinstance ( fil , str ) : <EOL> fil = FNMatcher ( fil ) <EOL> if isinstance ( rec , str ) : <EOL> self . rec = FNMatcher ( rec ) <EOL> elif not hasattr ( rec , '<STR_LIT>' ) and rec : <EOL> self . rec = lambda path : True <EOL> else : <EOL> self . rec = rec <EOL> self . fil = fil <EOL> self . ignore = ignore <EOL> self . breadthfirst = bf <EOL> self . optsort = sort and sorted or ( lambda x : x ) <EOL> def gen ( self , path ) : <EOL> try : <EOL> entries = path . listdir ( ) <EOL> except self . ignore : <EOL> return <EOL> rec = self . rec <EOL> dirs = self . optsort ( [ p for p in entries <EOL> if p . check ( dir = <NUM_LIT:1> ) and ( rec is None or rec ( p ) ) ] ) <EOL> if not self . breadthfirst : <EOL> for subdir in dirs : <EOL> for p in self . gen ( subdir ) : <EOL> yield p <EOL> for p in self . optsort ( entries ) : <EOL> if self . fil is None or self . fil ( p ) : <EOL> yield p <EOL> if self . breadthfirst : <EOL> for subdir in dirs : <EOL> for p in self . gen ( subdir ) : <EOL> yield p <EOL> class FNMatcher : <EOL> def __init__ ( self , pattern ) : <EOL> self . pattern = pattern <EOL> def __call__ ( self , path ) : <EOL> pattern = self . pattern <EOL> if ( pattern . find ( path . sep ) == - <NUM_LIT:1> and <EOL> iswin32 and <EOL> pattern . find ( posixpath . sep ) != - <NUM_LIT:1> ) : <EOL> pattern = pattern . replace ( posixpath . sep , path . sep ) <EOL> if pattern . find ( path . sep ) == - <NUM_LIT:1> : <EOL> name = path . basename <EOL> else : <EOL> name = str ( path ) <EOL> if not os . path . isabs ( pattern ) : <EOL> pattern = '<STR_LIT:*>' + path . sep + pattern <EOL> return py . std . fnmatch . fnmatch ( name , pattern ) </s>
