<s> import os <EOL> def is_file ( entry ) : <EOL> return isinstance ( entry , basestring ) <EOL> def is_dir ( entry ) : <EOL> return type ( entry ) is dict <EOL> def sanitize ( path ) : <EOL> """<STR_LIT>""" <EOL> while '<STR_LIT>' in path : <EOL> path = path . replace ( '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> while len ( path ) > <NUM_LIT:1> and path . endswith ( '<STR_LIT:/>' ) : <EOL> path = path [ : - <NUM_LIT:1> ] <EOL> return path <EOL> def merge_dicts ( src , dst ) : <EOL> """<STR_LIT>""" <EOL> for k , v in src . items ( ) : <EOL> if k not in dst : <EOL> dst [ k ] = v <EOL> continue <EOL> if type ( dst [ k ] ) is dict : <EOL> dst [ k ] = merge_dicts ( v , dst [ k ] ) <EOL> continue <EOL> if type ( dst [ k ] ) is list and type ( v ) is list : <EOL> dst [ k ] . extend ( v ) <EOL> continue <EOL> dst [ k ] = v <EOL> return dst <EOL> def build_nested_dict ( entries ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> if not entries : <EOL> return { } <EOL> for raw_path , value in entries . items ( ) : <EOL> path = sanitize ( raw_path ) <EOL> basename = os . path . basename ( path ) <EOL> subpaths = path . split ( '<STR_LIT:/>' ) [ <NUM_LIT:1> : ] <EOL> subentry = result <EOL> current = subentry <EOL> for subpath in subpaths : <EOL> current = subentry <EOL> subentry = subentry . setdefault ( subpath , { } ) <EOL> current [ basename ] = value <EOL> return result <EOL> def build_nested_dir_dict ( dirpath ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> path = sanitize ( dirpath ) <EOL> basename = os . path . basename ( path ) <EOL> subpaths = path . split ( '<STR_LIT:/>' ) [ <NUM_LIT:1> : ] <EOL> subentry = result <EOL> current = subentry <EOL> for subpath in subpaths : <EOL> current = subentry <EOL> subentry = subentry . setdefault ( subpath , { } ) <EOL> current [ basename ] = { } <EOL> return result </s>
