grim/pyscovery

Added license headers

2013-03-23, Gary Kramlich
231ec5dc3d81
Added license headers
# pyplugin - A python plugin finder
# Copyright (C) 2013 Gary Kramlich <grim@reaperworld.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import importlib
import inspect
PATHS = []
def add_path(path):
"""
Adds a path to search for plugins under
"""
global PATHS # pylint:disable-msg=W0602
if not path in PATHS:
PATHS.append(path)
def remove_path(path):
"""
Removes a plugin search path
"""
global PATHS # pylint:disable-msg=W0602
if path in PATHS:
PATHS.remove(path)
def get_paths():
"""
Returns the list of all plugin paths
"""
return PATHS
def find(cls):
"""
Find all plugins that are subclasses of cls in the current search paths
"""
if not inspect.isclass(cls):
raise TypeError('{} is not a class instance')
cls_name = cls.__name__
for path in PATHS:
mod = importlib.import_module(path)
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
yield symbol
__all__ = [add_path, remove_path, get_paths, find]