def enumerate_plugins(dirpath, module_prefix, namespace, class_, attributes={}):
 if os.path.isfile(dirpath):
  dirpath = os.path.dirname(dirpath)
 for fname in os.listdir(dirpath):
  if (fname.endswith('.py') and (not fname.startswith('__init__'))):
   (module_name, _) = os.path.splitext(fname)
   importlib.import_module(('%s.%s' % (module_prefix, module_name)))
 plugins = []
 for subclass in class_.__subclasses__():
  if (module_prefix != '.'.join(subclass.__module__.split('.')[:(-1)])):
   continue
  namespace[subclass.__name__] = subclass
  for (key, value) in attributes.items():
   setattr(subclass, key, value)
  plugins.append(subclass)
 return plugins

def enumerate_plugins ( dirpath , module_prefix , namespace , class_ , attributes = { } ) : if os . path . isfile ( dirpath ) : dirpath = os . path . dirname ( dirpath ) for fname in os . listdir ( dirpath ) : if ( fname . endswith ( '.py' ) and ( not fname . startswith ( '__init__' ) ) ) : ( module_name , _ ) = os . path . splitext ( fname ) importlib . import_module ( ( '%s.%s' % ( module_prefix , module_name ) ) ) plugins = [ ] for subclass in class_ . __subclasses__ ( ) : if ( module_prefix != '.' . join ( subclass . __module__ . split ( '.' ) [ : ( - 1 ) ] ) ) : continue namespace [ subclass . __name__ ] = subclass for ( key , value ) in attributes . items ( ) : setattr ( subclass , key , value ) plugins . append ( subclass ) return plugins
import plugins of type class located at dirpath into the namespace that starts with module_prefix .