<s> """<STR_LIT>""" <EOL> import os <EOL> import imp <EOL> import logging <EOL> class ModuleLoader : <EOL> def __init__ ( self , app = None ) : <EOL> self . app = app <EOL> self . logger = logging . getLogger ( __name__ ) <EOL> self . curr_path = os . path . dirname ( os . path . realpath ( __file__ ) ) <EOL> self . module_path = os . path . join ( self . curr_path , "<STR_LIT>" + os . sep ) <EOL> self . modules = { } <EOL> def load ( self ) : <EOL> """<STR_LIT>""" <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> dirlist = os . listdir ( self . module_path ) <EOL> for item in dirlist : <EOL> if item [ <NUM_LIT:0> ] == "<STR_LIT:.>" : <EOL> continue <EOL> parts = item . split ( "<STR_LIT:.>" ) <EOL> if len ( parts ) < <NUM_LIT:2> : <EOL> continue <EOL> name = parts [ <NUM_LIT:0> ] <EOL> ext = parts [ - <NUM_LIT:1> ] <EOL> if ext == "<STR_LIT>" and name [ <NUM_LIT:0> ] != "<STR_LIT:_>" : <EOL> module = self . load_single ( name ) <EOL> if module : <EOL> inst = self . load_instance ( module ) <EOL> if inst : <EOL> self . modules [ module [ <NUM_LIT:0> ] ] = inst <EOL> def load_instance ( self , module ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> inst = module [ <NUM_LIT:1> ] [ "<STR_LIT:class>" ] ( self . app ) <EOL> inst . name = module [ <NUM_LIT:0> ] <EOL> inst . options = module [ <NUM_LIT:1> ] <EOL> return inst <EOL> except : <EOL> self . logger . error ( "<STR_LIT>" . format ( module [ <NUM_LIT:0> ] ) , exc_info = True ) <EOL> return False <EOL> def load_single ( self , name ) : <EOL> """<STR_LIT>""" <EOL> path = os . path . join ( self . module_path , name + "<STR_LIT>" ) <EOL> try : <EOL> mod = imp . load_source ( name , path ) <EOL> except : <EOL> self . logger . error ( "<STR_LIT>" . format ( name ) , exc_info = True ) <EOL> return False <EOL> if "<STR_LIT>" not in dir ( mod ) : <EOL> return False <EOL> if "<STR_LIT:status>" not in mod . module . keys ( ) : <EOL> mod . module [ "<STR_LIT:status>" ] = False <EOL> return name , mod . module <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> ml = ModuleLoader ( ) <EOL> ml . load ( ) </s>
