<s> """<STR_LIT>""" <EOL> import logging <EOL> import os <EOL> import threading <EOL> import copy <EOL> from Queue import PriorityQueue <EOL> import sys <EOL> import traceback <EOL> from mpf . system . config import CaseInsensitiveDict <EOL> class AssetLoader ( threading . Thread ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , queue ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . log = logging . getLogger ( '<STR_LIT>' ) <EOL> self . queue = queue <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> while True : <EOL> asset = self . queue . get ( ) <EOL> if not asset [ <NUM_LIT:1> ] . loaded : <EOL> self . log . debug ( "<STR_LIT>" , asset [ <NUM_LIT:1> ] , <EOL> asset [ <NUM_LIT:2> ] ) <EOL> asset [ <NUM_LIT:1> ] . do_load ( asset [ <NUM_LIT:2> ] ) <EOL> self . log . debug ( "<STR_LIT>" , <EOL> asset [ <NUM_LIT:1> ] , self . queue . qsize ( ) ) <EOL> elif asset [ <NUM_LIT:2> ] : <EOL> self . log . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , asset [ <NUM_LIT:1> ] , <EOL> asset [ <NUM_LIT:2> ] ) <EOL> asset [ <NUM_LIT:2> ] ( ) <EOL> except Exception : <EOL> exc_type , exc_value , exc_traceback = sys . exc_info ( ) <EOL> lines = traceback . format_exception ( exc_type , exc_value , exc_traceback ) <EOL> msg = '<STR_LIT>' . join ( line for line in lines ) <EOL> self . exception_queue . put ( msg ) <EOL> class AssetManager ( object ) : <EOL> """<STR_LIT>""" <EOL> total_assets = <NUM_LIT:0> <EOL> loader_queue = PriorityQueue ( ) <EOL> loader_thread = AssetLoader ( loader_queue ) <EOL> loader_thread . daemon = True <EOL> loader_thread . start ( ) <EOL> def __init__ ( self , machine , config_section , path_string , asset_class , <EOL> asset_attribute , file_extensions ) : <EOL> self . log = logging . getLogger ( config_section + '<STR_LIT>' ) <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> self . machine = machine <EOL> self . loader_thread . exception_queue = self . machine . crash_queue <EOL> self . max_memory = None <EOL> self . registered_assets = set ( ) <EOL> self . path_string = path_string <EOL> self . config_section = config_section <EOL> self . asset_class = asset_class <EOL> self . file_extensions = file_extensions <EOL> self . machine . asset_managers [ config_section ] = self <EOL> if not hasattr ( self . machine , asset_attribute ) : <EOL> setattr ( self . machine , asset_attribute , CaseInsensitiveDict ( ) ) <EOL> self . asset_list = getattr ( self . machine , asset_attribute ) <EOL> self . machine . mode_controller . register_load_method ( self . load_assets , <EOL> self . config_section , <EOL> load_key = '<STR_LIT>' , <EOL> priority = asset_class . load_priority ) <EOL> self . machine . mode_controller . register_start_method ( self . load_assets , <EOL> self . config_section , <EOL> load_key = '<STR_LIT>' , <EOL> priority = asset_class . load_priority ) <EOL> self . machine . events . add_handler ( '<STR_LIT>' , <EOL> self . register_and_load_machine_assets , <EOL> priority = self . asset_class . load_priority ) <EOL> self . defaults = self . setup_defaults ( self . machine . config ) <EOL> def process_assets_from_disk ( self , config , path = None ) : <EOL> """<STR_LIT>""" <EOL> if not path : <EOL> path = self . machine . machine_path <EOL> if not config : <EOL> config = dict ( ) <EOL> root_path = os . path . join ( path , self . path_string ) <EOL> self . log . debug ( "<STR_LIT>" , root_path ) <EOL> for path , _ , files in os . walk ( root_path , followlinks = True ) : <EOL> valid_files = [ f for f in files if f . endswith ( self . file_extensions ) ] <EOL> for file_name in valid_files : <EOL> folder = os . path . basename ( path ) <EOL> name = os . path . splitext ( file_name ) [ <NUM_LIT:0> ] . lower ( ) <EOL> full_file_path = os . path . join ( path , file_name ) <EOL> if folder == self . path_string or folder not in self . defaults : <EOL> default_string = '<STR_LIT:default>' <EOL> else : <EOL> default_string = folder <EOL> built_up_config = copy . deepcopy ( self . defaults [ default_string ] ) <EOL> for k , v in config . iteritems ( ) : <EOL> if ( '<STR_LIT:file>' in v and v [ '<STR_LIT:file>' ] == file_name ) or name == k : <EOL> if name != k : <EOL> name = k <EOL> built_up_config . update ( config [ k ] ) <EOL> break <EOL> built_up_config [ '<STR_LIT:file>' ] = full_file_path <EOL> config [ name ] = built_up_config <EOL> self . log . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , name , file_name , <EOL> default_string , built_up_config ) <EOL> return config <EOL> def register_and_load_machine_assets ( self ) : <EOL> """<STR_LIT>""" <EOL> self . log . debug ( "<STR_LIT>" , self . config_section ) <EOL> if self . config_section in self . machine . config : <EOL> config = self . machine . config [ self . config_section ] <EOL> else : <EOL> config = None <EOL> self . machine . config [ self . config_section ] = self . register_assets ( <EOL> config = config ) <EOL> self . log . debug ( "<STR_LIT>" , self . config_section ) <EOL> self . load_assets ( self . machine . config [ self . config_section ] , <EOL> load_key = '<STR_LIT>' ) <EOL> def setup_defaults ( self , config ) : <EOL> """<STR_LIT>""" <EOL> default_config_dict = dict ( ) <EOL> if '<STR_LIT>' in config and config [ '<STR_LIT>' ] : <EOL> if ( self . config_section in config [ '<STR_LIT>' ] and <EOL> config [ '<STR_LIT>' ] [ self . config_section ] ) : <EOL> this_config = config [ '<STR_LIT>' ] [ self . config_section ] <EOL> default_config_dict [ '<STR_LIT:default>' ] = this_config . pop ( '<STR_LIT:default>' ) <EOL> for default_section_name in this_config : <EOL> default_config_dict [ default_section_name ] = ( <EOL> copy . deepcopy ( default_config_dict [ '<STR_LIT:default>' ] ) ) <EOL> default_config_dict [ default_section_name ] . update ( <EOL> this_config [ default_section_name ] ) <EOL> return default_config_dict <EOL> def register_assets ( self , config , mode_path = None ) : <EOL> """<STR_LIT>""" <EOL> config = self . process_assets_from_disk ( config = config , path = mode_path ) <EOL> for asset in config : <EOL> if not os . path . isfile ( config [ asset ] [ '<STR_LIT:file>' ] ) : <EOL> config [ asset ] [ '<STR_LIT:file>' ] = self . locate_asset_file ( <EOL> file_name = config [ asset ] [ '<STR_LIT:file>' ] , <EOL> path = mode_path ) <EOL> self . register_asset ( asset = asset . lower ( ) , <EOL> config = config [ asset ] ) <EOL> return config <EOL> def load_assets ( self , config , mode = None , load_key = None , callback = None , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> asset_set = set ( ) <EOL> for asset in config : <EOL> if self . asset_list [ asset ] . config [ '<STR_LIT>' ] == load_key : <EOL> self . asset_list [ asset ] . load ( callback = callback ) <EOL> asset_set . add ( self . asset_list [ asset ] ) <EOL> return self . unload_assets , asset_set <EOL> def register_asset ( self , asset , config ) : <EOL> """<STR_LIT>""" <EOL> self . asset_list [ asset ] = self . asset_class ( self . machine , config , <EOL> config [ '<STR_LIT:file>' ] , self ) <EOL> def unload_assets ( self , asset_set ) : <EOL> """<STR_LIT>""" <EOL> for asset in asset_set : <EOL> self . log . debug ( "<STR_LIT>" , asset . file_name ) <EOL> asset . unload ( ) <EOL> def load_asset ( self , asset , callback , priority = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> self . loader_queue . put ( ( - priority , asset , callback ) ) <EOL> self . log . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , asset , priority , self . loader_queue . qsize ( ) ) <EOL> AssetManager . total_assets += <NUM_LIT:1> <EOL> def locate_asset_file ( self , file_name , path = None ) : <EOL> """<STR_LIT>""" <EOL> if path : <EOL> path_list = [ path ] <EOL> else : <EOL> path_list = list ( ) <EOL> path_list . append ( self . machine . machine_path ) <EOL> for path in path_list : <EOL> full_path = os . path . join ( path , self . path_string , file_name ) <EOL> if os . path . isfile ( full_path ) : <EOL> return full_path <EOL> self . log . critical ( "<STR_LIT>" , <EOL> file_name ) <EOL> raise Exception ( ) <EOL> class Asset ( object ) : <EOL> load_priority = <NUM_LIT:100> <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , machine , config , file_name , asset_manager ) : <EOL> self . machine = machine <EOL> self . config = config <EOL> self . file_name = file_name <EOL> self . asset_manager = asset_manager <EOL> self . loaded = False <EOL> self . _initialize_asset ( ) <EOL> def load ( self , callback = None ) : <EOL> self . asset_manager . load_asset ( self , callback ) <EOL> def do_load ( self , callback ) : <EOL> pass <EOL> def unload ( self ) : <EOL> self . _unload ( ) <EOL> self . loaded = False </s>
