<s> import os <EOL> import itertools <EOL> from waterbutler . core import exceptions <EOL> class WaterButlerPathPart : <EOL> """<STR_LIT>""" <EOL> DECODE = lambda x : x <EOL> ENCODE = lambda x : x <EOL> def __init__ ( self , part , _id = None ) : <EOL> self . _id = _id <EOL> self . _count = <NUM_LIT:0> <EOL> self . _orig_id = _id <EOL> self . _orig_part = part <EOL> self . _name , self . _ext = os . path . splitext ( self . original_value ) <EOL> @ property <EOL> def identifier ( self ) : <EOL> return self . _id <EOL> @ property <EOL> def value ( self ) : <EOL> if self . _count : <EOL> return '<STR_LIT>' . format ( self . _name , self . _count , self . _ext ) <EOL> return '<STR_LIT>' . format ( self . _name , self . _ext ) <EOL> @ property <EOL> def raw ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ . ENCODE ( self . value ) <EOL> @ property <EOL> def original_value ( self ) : <EOL> return self . __class__ . DECODE ( self . _orig_part ) <EOL> @ property <EOL> def original_raw ( self ) : <EOL> return self . _orig_part <EOL> @ property <EOL> def ext ( self ) : <EOL> return self . _ext <EOL> def increment_name ( self , _id = None ) : <EOL> self . _id = _id <EOL> self . _count += <NUM_LIT:1> <EOL> return self <EOL> def renamed ( self , name ) : <EOL> return self . __class__ ( self . __class__ . ENCODE ( name ) , _id = self . _id ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . __class__ . __name__ , self . _orig_part , self . _count ) <EOL> class WaterButlerPath : <EOL> """<STR_LIT>""" <EOL> PART_CLASS = WaterButlerPathPart <EOL> @ classmethod <EOL> def generic_path_validation ( cls , path ) : <EOL> """<STR_LIT>""" <EOL> if not path : <EOL> raise exceptions . InvalidPathError ( '<STR_LIT>' ) <EOL> if not path . startswith ( '<STR_LIT:/>' ) : <EOL> raise exceptions . InvalidPathError ( '<STR_LIT>' . format ( path ) ) <EOL> if '<STR_LIT>' in path : <EOL> raise exceptions . InvalidPathError ( '<STR_LIT>' . format ( path ) ) <EOL> absolute_path = os . path . abspath ( path ) <EOL> if not path == '<STR_LIT:/>' and path . endswith ( '<STR_LIT:/>' ) : <EOL> absolute_path += '<STR_LIT:/>' <EOL> if not path == absolute_path : <EOL> raise exceptions . InvalidPathError ( '<STR_LIT>' . format ( absolute_path ) ) <EOL> @ classmethod <EOL> def validate_folder ( cls , path ) : <EOL> if not path . is_dir : <EOL> raise exceptions . CreateFolderError ( '<STR_LIT>' , code = <NUM_LIT> ) <EOL> if path . is_root : <EOL> raise exceptions . CreateFolderError ( '<STR_LIT>' , code = <NUM_LIT> ) <EOL> @ classmethod <EOL> def from_parts ( cls , parts , folder = False , ** kwargs ) : <EOL> _ids , _parts = [ ] , [ ] <EOL> for part in parts : <EOL> _ids . append ( part . identifier ) <EOL> _parts . append ( part . raw ) <EOL> path = '<STR_LIT:/>' . join ( _parts ) <EOL> if parts and not path : <EOL> path = '<STR_LIT:/>' <EOL> return cls ( path , _ids = _ids , folder = folder , ** kwargs ) <EOL> def __init__ ( self , path , _ids = ( ) , prepend = None , folder = None ) : <EOL> self . __class__ . generic_path_validation ( path ) <EOL> self . _orig_path = path <EOL> self . _prepend = prepend <EOL> if prepend : <EOL> self . _prepend_parts = [ self . PART_CLASS ( part , None ) for part in prepend . rstrip ( '<STR_LIT:/>' ) . split ( '<STR_LIT:/>' ) ] <EOL> else : <EOL> self . _prepend_parts = [ ] <EOL> self . _parts = [ <EOL> self . PART_CLASS ( part , _id ) <EOL> for _id , part in <EOL> itertools . zip_longest ( _ids , path . rstrip ( '<STR_LIT:/>' ) . split ( '<STR_LIT:/>' ) ) <EOL> ] <EOL> if folder is not None : <EOL> self . _is_folder = bool ( folder ) <EOL> else : <EOL> self . _is_folder = self . _orig_path . endswith ( '<STR_LIT:/>' ) <EOL> if self . is_dir and not self . _orig_path . endswith ( '<STR_LIT:/>' ) : <EOL> self . _orig_path += '<STR_LIT:/>' <EOL> @ property <EOL> def is_root ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . _parts ) == <NUM_LIT:1> <EOL> @ property <EOL> def is_dir ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _is_folder <EOL> @ property <EOL> def is_folder ( self ) : <EOL> return self . _is_folder <EOL> @ property <EOL> def is_file ( self ) : <EOL> """<STR_LIT>""" <EOL> return not self . _is_folder <EOL> @ property <EOL> def kind ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' if self . _is_folder else '<STR_LIT:file>' <EOL> @ property <EOL> def parts ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _parts <EOL> @ property <EOL> def name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _parts [ - <NUM_LIT:1> ] . value <EOL> @ property <EOL> def identifier ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _parts [ - <NUM_LIT:1> ] . identifier <EOL> @ property <EOL> def identifier_path ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT:/>' + self . _parts [ - <NUM_LIT:1> ] . identifier + ( '<STR_LIT:/>' if self . is_dir else '<STR_LIT>' ) <EOL> @ property <EOL> def ext ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _parts [ - <NUM_LIT:1> ] . ext <EOL> @ property <EOL> def path ( self ) : <EOL> """<STR_LIT>""" <EOL> if len ( self . parts ) == <NUM_LIT:1> : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT:/>' . join ( [ x . value for x in self . parts [ <NUM_LIT:1> : ] ] ) + ( '<STR_LIT:/>' if self . is_dir else '<STR_LIT>' ) <EOL> @ property <EOL> def raw_path ( self ) : <EOL> """<STR_LIT>""" <EOL> if len ( self . parts ) == <NUM_LIT:1> : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT:/>' . join ( [ x . raw for x in self . parts [ <NUM_LIT:1> : ] ] ) + ( '<STR_LIT:/>' if self . is_dir else '<STR_LIT>' ) <EOL> @ property <EOL> def full_path ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT:/>' . join ( [ x . value for x in self . _prepend_parts + self . parts [ <NUM_LIT:1> : ] ] ) + ( '<STR_LIT:/>' if self . is_dir else '<STR_LIT>' ) <EOL> @ property <EOL> def parent ( self ) : <EOL> """<STR_LIT>""" <EOL> if len ( self . parts ) == <NUM_LIT:1> : <EOL> return None <EOL> return self . __class__ . from_parts ( self . parts [ : - <NUM_LIT:1> ] , folder = True , prepend = self . _prepend ) <EOL> def child ( self , name , _id = None , folder = False ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ . from_parts ( self . parts + [ self . PART_CLASS ( name , _id = _id ) ] , folder = folder , prepend = self . _prepend ) <EOL> def increment_name ( self ) : <EOL> self . _parts [ - <NUM_LIT:1> ] . increment_name ( ) <EOL> return self <EOL> def rename ( self , name ) : <EOL> self . _parts [ - <NUM_LIT:1> ] = self . _parts [ - <NUM_LIT:1> ] . renamed ( name ) <EOL> return self <EOL> def __eq__ ( self , other ) : <EOL> return isinstance ( other , self . __class__ ) and str ( self ) == str ( other ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT:/>' . join ( [ x . value for x in self . parts ] ) + ( '<STR_LIT:/>' if self . is_dir else '<STR_LIT>' ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . __class__ . __name__ , self . _orig_path , self . _prepend ) </s>
