#NEWLINE# def init_plugins( plugindir , plugins_to_load = None ) : #NEWLINE# #INDENT# if ( plugindir and ( not os . path . isdir ( plugindir ) ) ) : #NEWLINE# #INDENT# raise InvalidPluginDir ( plugindir ) #NEWLINE# #UNINDENT# if ( not plugindir ) : #NEWLINE# #INDENT# plugindir = DIR ( 'plugins' ) #NEWLINE# #UNINDENT# logger . debug ( 'plugindir: {0}' . format ( plugindir ) ) #NEWLINE# if os . path . isdir ( plugindir ) : #NEWLINE# #INDENT# pluginfiles = glob ( os . path . join ( plugindir , '[!_]*.py' ) ) #NEWLINE# plugins = strip_extension ( ( os . path . basename ( p ) for p in pluginfiles ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# logger . debug ( 'trying pkg_resources' ) #NEWLINE# import pkg_resources #NEWLINE# try : #NEWLINE# #INDENT# plugins = strip_extension ( pkg_resources . resource_listdir ( __name__ , 'plugins' ) ) #NEWLINE# #UNINDENT# except OSError : #NEWLINE# #INDENT# raise InvalidPluginDir ( plugindir ) #NEWLINE# #UNINDENT# #UNINDENT# hooks = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# oldpath = copy . deepcopy ( sys . path ) #NEWLINE# sys . path . insert ( 0 , plugindir ) #NEWLINE# for plugin in plugins : #NEWLINE# #INDENT# if ( plugins_to_load and ( plugin not in plugins_to_load ) ) : #NEWLINE# #INDENT# logger . debug ( 'skipping plugin {0}, not in plugins_to_load {1}' . format ( plugin , plugins_to_load ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# logger . debug ( 'plugin: {0}' . format ( plugin ) ) #NEWLINE# try : #NEWLINE# #INDENT# mod = importlib . import_module ( plugin ) #NEWLINE# modname = mod . __name__ #NEWLINE# for hook in re . findall ( '\\bon_(\\w+)' , ' ' . join ( dir ( mod ) ) ) : #NEWLINE# #INDENT# hookfun = getattr ( mod , ( 'on_' + hook ) ) #NEWLINE# logger . debug ( 'plugin: attaching %s hook for %s' , hook , modname ) #NEWLINE# hooks . setdefault ( hook , [ ] ) . append ( hookfun ) #NEWLINE# #UNINDENT# if mod . __doc__ : #NEWLINE# #INDENT# firstline = mod . __doc__ . split ( '\n' ) [ 0 ] #NEWLINE# hooks . setdefault ( 'help' , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) [ modname ] = firstline #NEWLINE# hooks . setdefault ( 'extendedhelp' , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) [ modname ] = mod . __doc__ #NEWLINE# #UNINDENT# #UNINDENT# except : #NEWLINE# #INDENT# logger . warning ( 'import failed on module {0}, module not loaded' . format ( plugin ) ) #NEWLINE# logger . warning ( '{0}' . format ( sys . exc_info ( ) [ 0 ] ) ) #NEWLINE# logger . warning ( '{0}' . format ( traceback . format_exc ( ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# sys . path = oldpath #NEWLINE# return hooks