<s> '''<STR_LIT>''' <EOL> import os <EOL> import os . path <EOL> import glob <EOL> import shutil <EOL> try : <EOL> import urlparse <EOL> except ImportError : <EOL> import urllib . parse as urlparse <EOL> from . templatable import DocTemplatable <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> class Path ( object ) : <EOL> """<STR_LIT>""" <EOL> class ExtentionReplaceError ( DocTemplatable , ValueError ) : <EOL> "<STR_LIT>" <EOL> class InvalidPartError ( DocTemplatable , ValueError ) : <EOL> "<STR_LIT>" <EOL> class SegmentTypeError ( DocTemplatable , TypeError ) : <EOL> "<STR_LIT>" <EOL> string = None <EOL> def __init__ ( self , * paths , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . prefix = kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . segments = [ ] <EOL> for part in paths : <EOL> if isinstance ( part , str ) : <EOL> part = part . split ( '<STR_LIT:/>' ) <EOL> elif hasattr ( part , '<STR_LIT>' ) : <EOL> pass <EOL> else : <EOL> raise self . InvalidPartError ( part ) <EOL> for segment in part : <EOL> if not isinstance ( segment , str ) : <EOL> raise self . SegmentTypeError ( part ) <EOL> if segment == '<STR_LIT:..>' : <EOL> self . segments . pop ( ) <EOL> elif segment == '<STR_LIT:.>' : <EOL> pass <EOL> elif segment : <EOL> self . segments . append ( segment ) <EOL> def __len__ ( self ) : <EOL> '<STR_LIT>' <EOL> return len ( self . segments ) <EOL> def __iter__ ( self ) : <EOL> '<STR_LIT>' <EOL> return iter ( self . segments ) <EOL> def __hash__ ( self ) : <EOL> '<STR_LIT>' <EOL> return hash ( str ( self ) ) <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( other , Path ) : <EOL> return False <EOL> return ( self . prefix == other . prefix ) and ( self . segments == other . segments ) <EOL> def __truediv__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ ( self . segments , name , prefix = self . prefix ) <EOL> __div__ = __truediv__ <EOL> def __getitem__ ( self , names ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( names , tuple ) : <EOL> return self . __class__ ( self . segments , * names , prefix = self . prefix ) <EOL> else : <EOL> return self . __class__ ( self . segments , names , prefix = self . prefix ) <EOL> def __call__ ( self , * names ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ ( self . segments , * names , prefix = self . prefix ) <EOL> def up ( self , count = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if count == <NUM_LIT:0> : <EOL> return self . __class__ ( self . segments , prefix = self . prefix ) <EOL> else : <EOL> return self . __class__ ( self . segments [ : - count ] , prefix = self . prefix ) <EOL> def head ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . segments [ <NUM_LIT:0> ] <EOL> def tail ( self , count = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ ( self . segments [ count : ] , prefix = self . prefix ) <EOL> def ext ( self , new_ext = None ) : <EOL> """<STR_LIT>""" <EOL> name , old_ext = os . path . splitext ( self . segments [ - <NUM_LIT:1> ] ) <EOL> if new_ext is None : <EOL> return old_ext <EOL> if not old_ext : <EOL> raise self . ExtentionReplaceError ( old_ext ) <EOL> filename = name + ( '<STR_LIT>' if new_ext . startswith ( '<STR_LIT:.>' ) else '<STR_LIT:.>' ) + new_ext <EOL> return self . __class__ ( self . segments [ : - <NUM_LIT:1> ] , filename , prefix = self . prefix ) <EOL> def add_ext ( self , new_ext ) : <EOL> """<STR_LIT>""" <EOL> name , _ = os . path . splitext ( self . segments [ - <NUM_LIT:1> ] ) <EOL> filename = name + ( '<STR_LIT>' if new_ext . startswith ( '<STR_LIT:.>' ) else '<STR_LIT:.>' ) + new_ext <EOL> return self . __class__ ( self . segments [ : - <NUM_LIT:1> ] , filename , prefix = self . prefix ) <EOL> @ property <EOL> def filename ( self ) : <EOL> """<STR_LIT>""" <EOL> name , _ = os . path . splitext ( self . segments [ - <NUM_LIT:1> ] ) <EOL> return name <EOL> def __bool__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return bool ( self . segments ) <EOL> __nonzero__ = __bool__ <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . format ( <EOL> self . __class__ . __name__ , self . prefix , self . segments , id ( self ) ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . string is None : <EOL> path = [ self . prefix ] <EOL> path . extend ( self . segments ) <EOL> self . string = '<STR_LIT:/>' . join ( path ) <EOL> return self . string <EOL> def str ( self , front = False , back = False , prefix = True ) : <EOL> """<STR_LIT>""" <EOL> return ( <EOL> ( '<STR_LIT:/>' if front else '<STR_LIT>' ) + <EOL> str ( self ) if prefix else '<STR_LIT:/>' . join ( self . segments ) + <EOL> ( '<STR_LIT:/>' if back else '<STR_LIT>' ) ) <EOL> def startswith ( self , root ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( root , Path ) : <EOL> raise ValueError ( '<STR_LIT>' % type ( root ) . __name__ ) <EOL> root_length = len ( root . segments ) <EOL> if len ( self . segments ) < root_length : <EOL> return False <EOL> return ( self . segments [ : root_length ] == root . segments and <EOL> self . prefix == root . prefix ) <EOL> def exists ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . path . exists ( self . str ( ) ) <EOL> def isfile ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . path . isfile ( self . str ( ) ) <EOL> def isdir ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . path . isdir ( self . str ( ) ) <EOL> def listdir ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . listdir ( self . str ( ) ) <EOL> def makedirs ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . makedirs ( self . str ( ) ) <EOL> def remove ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . isfile ( ) : <EOL> return os . remove ( self . str ( ) ) <EOL> else : <EOL> return shutil . rmtree ( self . str ( ) ) <EOL> def open ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> return open ( self . str ( ) , * args ) <EOL> def iterfiles ( self , topdown = True , onerror = None , followlinks = False ) : <EOL> """<STR_LIT>""" <EOL> generator = os . walk ( self . str ( ) , topdown , onerror , followlinks ) <EOL> for root , _ , files in generator : <EOL> for name in files : <EOL> yield self . __class__ . from_file ( root , name ) <EOL> def glob ( self , pattern ) : <EOL> for path in glob . iglob ( str ( self / pattern ) ) : <EOL> yield self . __class__ . from_file ( path ) <EOL> @ classmethod <EOL> def from_file ( cls , * paths ) : <EOL> """<STR_LIT>""" <EOL> drive , path = os . path . splitdrive ( os . path . abspath ( os . path . join ( * paths ) ) ) <EOL> return cls ( path . split ( os . sep ) , prefix = drive ) <EOL> @ classmethod <EOL> def from_url ( cls , path ) : <EOL> """<STR_LIT>""" <EOL> scheme , netloc , path , _ , _ , _ = urlparse . urlparse ( path ) <EOL> return cls ( path , prefix = scheme + '<STR_LIT>' + netloc ) <EOL> @ classmethod <EOL> def cwd ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls . from_file ( os . getcwd ( ) ) <EOL> def stat ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . stat ( self . str ( ) ) </s>
