<s> """<STR_LIT>""" <EOL> import os <EOL> import glob <EOL> import sys <EOL> import os . path <EOL> import traceback <EOL> plugins = [ ] <EOL> callbacks = { } <EOL> bad_plugins = [ ] <EOL> def catalogue_plugin ( plugin_module ) : <EOL> """<STR_LIT>""" <EOL> listing = dir ( plugin_module ) <EOL> listing = [ item for item in listing if item . startswith ( "<STR_LIT>" ) ] <EOL> for mem in listing : <EOL> func = getattr ( plugin_module , mem ) <EOL> memadj = mem [ <NUM_LIT:3> : ] <EOL> if callable ( func ) : <EOL> callbacks . setdefault ( memadj , [ ] ) . append ( func ) <EOL> def get_callback_chain ( chain ) : <EOL> """<STR_LIT>""" <EOL> return callbacks . get ( chain , [ ] ) <EOL> def initialize_plugins ( plugin_dirs , plugin_list ) : <EOL> """<STR_LIT>""" <EOL> if plugins or bad_plugins : <EOL> return <EOL> callbacks . clear ( ) <EOL> for mem in plugin_dirs : <EOL> if os . path . isdir ( mem ) : <EOL> sys . path . append ( mem ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" "<STR_LIT>" % mem ) <EOL> plugin_list = get_plugin_list ( plugin_list , plugin_dirs ) <EOL> for mem in plugin_list : <EOL> try : <EOL> _module = __import__ ( mem ) <EOL> except ( SystemExit , KeyboardInterrupt ) : <EOL> raise <EOL> except : <EOL> bad_plugins . append ( ( mem , "<STR_LIT>" . join ( traceback . format_exc ( ) ) ) ) <EOL> continue <EOL> for comp in mem . split ( "<STR_LIT:.>" ) [ <NUM_LIT:1> : ] : <EOL> _module = getattr ( _module , comp ) <EOL> catalogue_plugin ( _module ) <EOL> plugins . append ( _module ) <EOL> def get_plugin_by_name ( name ) : <EOL> """<STR_LIT>""" <EOL> if plugins : <EOL> for mem in plugins : <EOL> if mem . __name__ == name : <EOL> return mem <EOL> return None <EOL> def get_module_name ( filename ) : <EOL> """<STR_LIT>""" <EOL> return os . path . splitext ( os . path . split ( filename ) [ <NUM_LIT:1> ] ) [ <NUM_LIT:0> ] <EOL> def get_plugin_list ( plugin_list , plugin_dirs ) : <EOL> """<STR_LIT>""" <EOL> if plugin_list is None : <EOL> plugin_list = [ ] <EOL> for mem in plugin_dirs : <EOL> file_list = glob . glob ( os . path . join ( mem , "<STR_LIT>" ) ) <EOL> file_list = [ get_module_name ( filename ) for filename in file_list ] <EOL> file_list = [ plugin for plugin in file_list if not plugin . startswith ( '<STR_LIT:_>' ) ] <EOL> plugin_list += file_list <EOL> plugin_list . sort ( ) <EOL> return plugin_list </s>
