Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_parse_hookimpl_override():
class MyPluginManager(PluginManager):
def parse_hookimpl_opts(self, module_or_class, name):
opts = PluginManager.parse_hookimpl_opts(self, module_or_class, name)
if opts is None:
if name.startswith("x1"):
opts = {}
return opts
class Plugin:
def x1meth(self):
pass
@hookimpl(hookwrapper=True, tryfirst=True)
def x1meth2(self):
pass
class Spec:
def test_warning_on_call_vs_hookspec_arg_mismatch():
"""Verify that is a hook is called with less arguments then defined in the
spec that a warning is emitted.
"""
class Spec:
@hookspec
def myhook(self, arg1, arg2):
pass
class Plugin:
@hookimpl
def myhook(self, arg1):
pass
pm = PluginManager(hookspec.project_name)
pm.register(Plugin())
pm.add_hookspecs(Spec())
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter('always')
# calling should trigger a warning
pm.hook.myhook(arg1=1)
assert len(warns) == 1
warning = warns[-1]
assert issubclass(warning.category, Warning)
assert "Argument(s) ('arg2',)" in str(warning.message)
def myhook(self, arg1, arg2):
print("inside Plugin_1.myhook()")
return arg1 + arg2
class Plugin_2(object):
"""A 2nd hook implementation namespace.
"""
@hookimpl
def myhook(self, arg1, arg2):
print("inside Plugin_2.myhook()")
return arg1 - arg2
# create a manager and add the spec
pm = pluggy.PluginManager("myproject")
pm.add_hookspecs(MySpec)
# register plugins
pm.register(Plugin_1())
pm.register(Plugin_2())
# call our `myhook` hook
results = pm.hook.myhook(arg1=1, arg2=2)
print(results)
commands (Commands): The Commands instance
"""
@hookspec
def cli_execcmd(command, opts):
"""@API
PLUGIN API
Execute the command being added to CLI
@params:
command (str): The command
opts (dict): The options
"""
pluginmgr = pluggy.PluginManager(PMNAME)
pluginmgr.add_hookspecs(sys.modules[__name__])
def _get_plugin(name):
"""
Try to find the plugin by name with/without prefix.
If the plugin is not found, try to treat it as a module and import it
"""
if isinstance(name, str):
for plugin in pluginmgr.get_plugins():
plname = pluginmgr.get_name(plugin)
if plname.isdigit():
plname = plugin.__class__.__name__
if plname in ('pyppl-' + name, 'pyppl_' + name,
'PyPPL' + name.capitalize()):
return plugin
Copyright (c) 2019 The Fuel Rats Mischief,
All rights reserved.
Licensed under the BSD 3-Clause License.
See LICENSE.md
"""
import logging
import pluggy
from ._constants import _PLUGIN_NAME
PLUGIN_MANAGER = pluggy.PluginManager(_PLUGIN_NAME)
"""
Configuration plugin manager
def __init__(self, stage, module_qname, entrypoint, plugin_base_class):
"""
Initialize this plugin manager for the `stage` specified in the fully
qualified Python module name `module_qname` with plugins loaded from the
setuptools `entrypoint` that must subclass `plugin_base_class`.
"""
self.manager = PluggyPluginManager(project_name=stage)
self.managers[stage] = self
self.stage = stage
self.entrypoint = entrypoint
self.plugin_base_class = plugin_base_class
self.manager.add_hookspecs(sys.modules[module_qname])
# set to True once this manager is initialized by running its setup()
self.initialized = False
# mapping of {plugin.name: plugin_class} for all the plugins of this
# manager
self.plugin_classes = OrderedDict()
def setup_plugins(plugins=None):
"""
Install plugins & setup a pluginmanager
"""
# Install plugins
if plugins:
conda.ensure_pip_packages(HUB_ENV_PREFIX, plugins)
# Set up plugin infrastructure
pm = pluggy.PluginManager('tljh')
pm.add_hookspecs(hooks)
pm.load_setuptools_entrypoints('tljh')
return pm
def create_plugin_manager():
"""Create plugin manager and defined hooks specification."""
plugin_manager = pluggy.PluginManager(hookspecs.hookspec.project_name)
plugin_manager.add_hookspecs(hookspecs)
plugin_manager.load_setuptools_entrypoints(hookspecs.hookspec.project_name)
return plugin_manager
import os
import json
from notebook.utils import url_path_join
from notebook.base.handlers import IPythonHandler
from tornado import web, iostream
import asyncio
import pluggy
from nbresuse import hooks, default_resources
from collections import ChainMap
plugin_manager = pluggy.PluginManager('nbresuse')
plugin_manager.add_hookspecs(hooks)
# Register the resources nbresuse provides by default
plugin_manager.register(default_resources)
class MetricsHandler(IPythonHandler):
def initialize(self, nbapp):
self.set_header('content-type', 'text/event-stream')
self.set_header('cache-control', 'no-cache')
self.nbapp = nbapp
@web.authenticated
async def get(self):
"""
Calculate and return current resource usage metrics
"""
def _import_plugins(self) -> None:
"""
Import and register plugin in the plugin manager.
The pluggy library is used as plugin manager.
"""
logger.debug('Importing plugins')
self._pm = pluggy.PluginManager('sirbot')
self._pm.add_hookspecs(hookspecs)
for plugin in self.config['sirbot']['plugins']:
try:
p = importlib.import_module(plugin)
except (ModuleNotFoundError, ):
if os.getcwd() not in sys.path:
sys.path.append(os.getcwd())
p = importlib.import_module(plugin)
else:
raise
self._pm.register(p)