grim/pyscovery

13f48ef9ba20
Parents c42c665e3b2b
Children 19570d2d0da1
fixed the remove_module which was calling _real_add_module
  • +42 -41
    pyscovery.py
  • --- 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 @@
    class Loader(object):
    + """ A pyscovery loader """
    +
    def __init__(self):
    self.modules = []
    self.discovered = None
    @@ -85,7 +87,7 @@
    """
    self.lock.acquire()
    - self._real_add_module(module)
    + self._real_remove_module(module)
    self.lock.release()
    @@ -124,8 +126,41 @@
    :returns: Generator of subclasses of cls
    """
    + if not inspect.isclass(cls):
    + raise TypeError('{} is not a class instance'.format(cls))
    - @classmethod
    + cls_name = cls.__name__
    +
    + modules = []
    + modules.extend(self.modules)
    +
    + for module in modules:
    + mod = importlib.import_module(module)
    +
    + if recurse and _is_package(mod):
    + _recurse(mod)
    +
    + for symbol_name in dir(mod):
    + if symbol_name == cls_name:
    + continue
    +
    + symbol = getattr(mod, symbol_name, None)
    +
    + if not inspect.isclass(symbol):
    + continue
    +
    + if inspect.isabstract(symbol):
    + continue
    +
    + if not issubclass(symbol, cls):
    + continue
    +
    + if create:
    + yield symbol(*args, **kwargs)
    + else:
    + yield symbol
    +
    + @classmethod
    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()
    -
    def add_module(module):
    """Adds a module to search for plugins under
    @@ -196,15 +230,14 @@
    def clear_modules():
    - """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.
    :type cls: class
    :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 = []
    - modules.extend(MODULES)
    -
    - for module in modules:
    - mod = importlib.import_module(module)
    -
    - if recurse and _is_package(mod):
    - _recurse(mod)
    -
    - for symbol_name in dir(mod):
    - if symbol_name == cls_name:
    - continue
    -
    - symbol = getattr(mod, symbol_name, None)
    -
    - if not inspect.isclass(symbol):
    - continue
    -
    - if inspect.isabstract(symbol):
    - continue
    -
    - if not issubclass(symbol, cls):
    - continue
    -
    - if create:
    - yield symbol(*args, **kwargs)
    - else:
    - yield symbol
    + return _DEFAULT_LOADER.find(cls, recurse, create, *args, **kwargs)
    def _is_package(module):