--- a/pyscovery.py Sun May 26 05:37:31 2013 -0500
+++ b/pyscovery.py Sun May 26 05:37:49 2013 -0500
@@ -36,6 +36,8 @@
+ """ A pyscovery loader """ @@ -85,7 +87,7 @@
- self._real_add_module(module)
+ self._real_remove_module(module) @@ -124,8 +126,41 @@
:returns: Generator of subclasses of cls
+ if not inspect.isclass(cls): + raise TypeError('{} is not a class instance'.format(cls))
+ cls_name = cls.__name__ + modules.extend(self.modules) + mod = importlib.import_module(module) + if recurse and _is_package(mod): + for symbol_name in dir(mod): + if symbol_name == cls_name: + symbol = getattr(mod, symbol_name, None) + if not inspect.isclass(symbol): + if inspect.isabstract(symbol): + if not issubclass(symbol, cls): + yield symbol(*args, **kwargs) def _is_package(cls, module):
Returns true if the module's name is __init__, false otherwise
@@ -155,17 +190,16 @@
filename = os.path.join(dirname, name)
if os.path.isdir(filename):
- add_module(make_module(base))
+ self.add_module(make_module(base)) elif os.path.isfile(filename):
if fnmatch.fnmatch(name, '*.py') or \
fnmatch.fnmatch(name, '*.py[co]'):
- add_module(make_module(base))
+ self.add_module(make_module(base)) _DEFAULT_LOADER = Loader()
"""Adds a module to search for plugins under
@@ -196,15 +230,14 @@
- """Clears all search modules
+ """Clears all search modules """ _DEFAULT_LOADER.clear_modules()
def find(cls, recurse, create, *args, **kwargs):
"""Find all plugins that are subclasses of cls in the current search paths
:param cls: The class whose subclasses to find.
:param recurse: Whether or not to recurse into packages.
@@ -220,39 +253,7 @@
:returns: Generator of subclasses of cls
- if not inspect.isclass(cls):
- raise TypeError('{} is not a class instance'.format(cls))
- cls_name = cls.__name__
- modules.extend(MODULES)
- mod = importlib.import_module(module)
- if recurse and _is_package(mod):
- for symbol_name in dir(mod):
- if symbol_name == cls_name:
- symbol = getattr(mod, symbol_name, None)
- if not inspect.isclass(symbol):
- if inspect.isabstract(symbol):
- if not issubclass(symbol, cls):
- yield symbol(*args, **kwargs)
+ return _DEFAULT_LOADER.find(cls, recurse, create, *args, **kwargs)