diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2015-06-13 08:22:43 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2015-06-13 08:22:43 -0400 |
commit | 57ee6c36690e62698165e84d40614aa4f2cb7dc5 (patch) | |
tree | 208ed212145490c8e7ec97434703f549704b49ec /coverage/plugin_support.py | |
parent | 0ce35d98b867a92f1022034e4b906ee8536aed63 (diff) | |
download | python-coveragepy-git-57ee6c36690e62698165e84d40614aa4f2cb7dc5.tar.gz |
Also include plugin_support.py!
Diffstat (limited to 'coverage/plugin_support.py')
-rw-r--r-- | coverage/plugin_support.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/coverage/plugin_support.py b/coverage/plugin_support.py new file mode 100644 index 00000000..9ab79544 --- /dev/null +++ b/coverage/plugin_support.py @@ -0,0 +1,51 @@ +"""Support for plugins.""" + +import sys + +from coverage.misc import CoverageException + + +class Plugins(object): + """The currently loaded collection of coverage.py plugins.""" + + def __init__(self): + self.order = [] + self.names = {} + + @classmethod + def load_plugins(cls, modules, config): + """Load plugins from `modules`. + + Returns a list of loaded and configured plugins. + + """ + plugins = cls() + + for module in modules: + __import__(module) + mod = sys.modules[module] + + plugin_class = getattr(mod, "Plugin", None) + if not plugin_class: + raise CoverageException("Plugin module %r didn't define a Plugin class" % module) + + options = config.get_plugin_options(module) + plugin = plugin_class(options) + plugin._coverage_plugin_name = module + plugin._coverage_enabled = True + plugins.order.append(plugin) + plugins.names[module] = plugin + + return plugins + + def __nonzero__(self): + return bool(self.order) + + __bool__ = __nonzero__ + + def __iter__(self): + return iter(self.order) + + def get(self, plugin_name): + """Return a plugin by name.""" + return self.names[plugin_name] |