Skip to content

Instantly share code, notes, and snippets.

@evilchili
Created September 24, 2016 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evilchili/80686a340cf19c40f7505584ddfccf42 to your computer and use it in GitHub Desktop.
Save evilchili/80686a340cf19c40f7505584ddfccf42 to your computer and use it in GitHub Desktop.
import pkg_resources
import os
import glob
import inrospect
import sys
import re
class PluginLoader(object):
"""This is such a silly thing"""
def __init__(self):
for plugin_class in self._get_plugins():
plugin_class(self).initialize()
def _find_project_plugins(self):
for dist in pkg_resources.working_set:
for pkg_path in glob.glob(dist.location + os.sep + 'project*'):
if os.path.exists(os.path.join(pkg_path, 'plugins')):
module_name = '.'.join([os.path.basename(pkg_path), 'plugins'])
try:
importlib.import_module(module_name)
yield sys.modules[module_name]
except IndexError:
pass
def _get_plugins(self):
modpat = re.compile('^project.*\.plugins\.(?!baseplugin)')
clspat = re.compile('(?!Base).+Plugin$')
for m in self._find_project_plugins():
for mod in [x[1] for x in inspect.getmembers(m, inspect.ismodule) if modpat.match(x[1].__name__)]:
for plugin in [x[1] for x in inspect.getmembers(mod, inspect.isclass) if clspat.match(x[0])]:
yield plugin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment