<s> import os <EOL> import shutil <EOL> class StorageLayout ( object ) : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def get_request_path ( fixture ) : <EOL> separator = '<STR_LIT:->' <EOL> path = fixture . request_path . replace ( os . sep , separator ) . strip ( separator ) <EOL> return path <EOL> class RequestMethodLayout ( StorageLayout ) : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def path_components_for ( fixture ) : <EOL> request_path = StorageLayout . get_request_path ( fixture ) <EOL> return [ fixture . app_name , fixture . request_method , <EOL> request_path , fixture . name ] <EOL> class RouteLayout ( StorageLayout ) : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def path_components_for ( fixture ) : <EOL> request_path = StorageLayout . get_request_path ( fixture ) <EOL> return [ fixture . app_name , request_path , <EOL> fixture . request_method , fixture . name ] <EOL> _dir_map = { } <EOL> class Storage ( object ) : <EOL> pass <EOL> class FileStorage ( Storage ) : <EOL> def __init__ ( self , layout_class , dirname , root_path ) : <EOL> """<STR_LIT>""" <EOL> self . layout = layout_class <EOL> self . dirname = dirname <EOL> self . root_path = root_path <EOL> @ property <EOL> def fixture_directory ( self ) : <EOL> return os . path . join ( self . root_path , self . dirname ) <EOL> def reset_directory ( self ) : <EOL> if os . path . exists ( self . fixture_directory ) : <EOL> shutil . rmtree ( self . fixture_directory ) <EOL> def store_fixture ( self , fixture ) : <EOL> global _dir_map <EOL> path_components = self . layout . path_components_for ( fixture ) <EOL> version = <NUM_LIT:0> <EOL> index = self . _file_index ( path_components ) <EOL> if index in _dir_map : <EOL> version = _dir_map [ index ] + <NUM_LIT:1> <EOL> if version : <EOL> path_components [ - <NUM_LIT:1> ] += '<STR_LIT>' . format ( version ) <EOL> _dir_map [ index ] = version <EOL> path_components [ - <NUM_LIT:1> ] += '<STR_LIT>' . format ( fixture . type ) <EOL> fn = os . path . join ( self . fixture_directory , * path_components ) <EOL> os . makedirs ( os . path . dirname ( fn ) , exist_ok = True ) <EOL> with open ( fn , '<STR_LIT:w>' ) as fd : <EOL> fd . write ( fixture . payload ) <EOL> def _file_index ( self , path_components ) : <EOL> """<STR_LIT>""" <EOL> return str ( hash ( os . sep . join ( path_components ) ) ) </s>
