Python stevedore.extension.ExtensionManager() Examples

The following are 30 code examples of stevedore.extension.ExtensionManager(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module stevedore.extension , or try the search function .
Example #1
Source File: command.py    From cliff with Apache License 2.0 6 votes vote down vote up
def _load_hooks(self):
        # Look for command extensions
        if self.app and self.cmd_name:
            namespace = '{}.{}'.format(
                self.app.command_manager.namespace,
                self.cmd_name.replace(' ', '_')
            )
            self._hooks = extension.ExtensionManager(
                namespace=namespace,
                invoke_on_load=True,
                invoke_kwds={
                    'command': self,
                },
            )
        else:
            # Setting _hooks to an empty list allows iteration without
            # checking if there are hooks every time.
            self._hooks = []
        return 
Example #2
Source File: test_callback.py    From bazarr with GNU General Public License v3.0 6 votes vote down vote up
def test_extension_failure_custom_callback(self):
        errors = []

        def failure_callback(manager, entrypoint, error):
            errors.append((manager, entrypoint, error))

        em = extension.ExtensionManager('stevedore.test.extension',
                                        invoke_on_load=True,
                                        on_load_failure_callback=
                                        failure_callback)
        extensions = list(em.extensions)
        self.assertThat(len(extensions), GreaterThan(0))
        self.assertEqual(len(errors), 2)
        for manager, entrypoint, error in errors:
            self.assertIs(manager, em)
            self.assertIsInstance(error, (IOError, ImportError)) 
Example #3
Source File: resource_tracker.py    From cyborg with Apache License 2.0 6 votes vote down vote up
def _initialize_drivers(self, enabled_drivers=None):
        """Load accelerator drivers.

        :return: [nvidia_gpu_driver_obj, intel_fpga_driver_obj]
        """
        acc_drivers = []
        if not enabled_drivers:
            enabled_drivers = CONF.agent.enabled_drivers
        valid_drivers = ExtensionManager(
            namespace='cyborg.accelerator.driver').names()
        for d in enabled_drivers:
            if d not in valid_drivers:
                raise exception.InvalidDriver(name=d)
            acc_driver = driver.DriverManager(
                namespace='cyborg.accelerator.driver', name=d,
                invoke_on_load=True).driver
            acc_drivers.append(acc_driver)
        self.acc_drivers = acc_drivers 
Example #4
Source File: test_callback.py    From stevedore with Apache License 2.0 6 votes vote down vote up
def test_extension_failure_custom_callback(self):
        errors = []

        def failure_callback(manager, entrypoint, error):
            errors.append((manager, entrypoint, error))

        em = extension.ExtensionManager('stevedore.test.extension',
                                        invoke_on_load=True,
                                        on_load_failure_callback=
                                        failure_callback)
        extensions = list(em.extensions)
        self.assertTrue(len(extensions), GreaterThan(0))
        self.assertEqual(len(errors), 2)
        for manager, entrypoint, error in errors:
            self.assertIs(manager, em)
            self.assertIsInstance(error, (IOError, ImportError)) 
Example #5
Source File: test_extension.py    From stevedore with Apache License 2.0 6 votes vote down vote up
def test_map_arguments(self):
        objs = []

        def mapped(ext, *args, **kwds):
            objs.append((ext, args, kwds))

        em = extension.ExtensionManager('stevedore.test.extension',
                                        invoke_on_load=True,
                                        )
        em.map(mapped, 1, 2, a='A', b='B')
        self.assertEqual(len(objs), 2)
        names = sorted([o[0].name for o in objs])
        self.assertEqual(names, WORKING_NAMES)
        for o in objs:
            self.assertEqual(o[1], (1, 2))
            self.assertEqual(o[2], {'a': 'A', 'b': 'B'}) 
Example #6
Source File: test_extension.py    From bazarr with GNU General Public License v3.0 6 votes vote down vote up
def test_map_arguments(self):
        objs = []

        def mapped(ext, *args, **kwds):
            objs.append((ext, args, kwds))

        em = extension.ExtensionManager('stevedore.test.extension',
                                        invoke_on_load=True,
                                        )
        em.map(mapped, 1, 2, a='A', b='B')
        self.assertEqual(len(objs), 2)
        names = sorted([o[0].name for o in objs])
        self.assertEqual(names, WORKING_NAMES)
        for o in objs:
            self.assertEqual(o[1], (1, 2))
            self.assertEqual(o[2], {'a': 'A', 'b': 'B'}) 
Example #7
Source File: runnersregistrar.py    From st2 with Apache License 2.0 6 votes vote down vote up
def register_runners(experimental=False, fail_on_failure=True):
    """
    Register runners
    """
    LOG.debug('Start : register runners')
    runner_count = 0

    manager = ExtensionManager(namespace=RUNNERS_NAMESPACE, invoke_on_load=False)
    extension_names = manager.names()

    for name in extension_names:
        LOG.debug('Found runner "%s"' % (name))

        manager = DriverManager(namespace=RUNNERS_NAMESPACE, invoke_on_load=False, name=name)
        runner_metadata = manager.driver.get_metadata()
        runner_count += register_runner(runner_metadata, experimental)

    LOG.debug('End : register runners')

    return runner_count 
Example #8
Source File: extension_loader.py    From bandit with Apache License 2.0 6 votes vote down vote up
def load_blacklists(self, blacklist_namespace):
        self.blacklists_mgr = extension.ExtensionManager(
            namespace=blacklist_namespace,
            invoke_on_load=False,
            verify_requirements=False,
            )
        self.blacklist = {}
        blacklist = list(self.blacklists_mgr)
        for item in blacklist:
            for key, val in item.plugin().items():
                utils.check_ast_node(key)
                self.blacklist.setdefault(key, []).extend(val)

        self.blacklist_by_id = {}
        self.blacklist_by_name = {}
        for val in six.itervalues(self.blacklist):
            for b in val:
                self.blacklist_by_id[b['id']] = b
                self.blacklist_by_name[b['name']] = b 
Example #9
Source File: extension_loader.py    From bandit with Apache License 2.0 6 votes vote down vote up
def load_plugins(self, plugins_namespace):
        self.plugins_mgr = extension.ExtensionManager(
            namespace=plugins_namespace,
            invoke_on_load=False,
            verify_requirements=False,
            )

        def test_has_id(plugin):
            if not hasattr(plugin.plugin, "_test_id"):
                # logger not setup yet, so using print
                print("WARNING: Test '%s' has no ID, skipping." % plugin.name,
                      file=sys.stderr)
                return False
            return True

        self.plugins = list(filter(test_has_id, list(self.plugins_mgr)))
        self.plugin_names = [plugin.name for plugin in self.plugins]
        self.plugins_by_id = {p.plugin._test_id: p for p in self.plugins}
        self.plugins_by_name = {p.name: p for p in self.plugins} 
Example #10
Source File: manager.py    From contrail-api-cli with MIT License 6 votes vote down vote up
def load_namespace(self, ns):
        """Load commands from namespace.

        :param ns: namespace name
        :type ns: str
        """
        mgr = extension.ExtensionManager(namespace=ns,
                                         verify_requirements=True,
                                         on_load_failure_callback=self._on_failure)
        # Load commands from ns
        for ext in mgr.extensions:
            try:
                obj = ext.plugin(ext.name)
            except Exception as err:
                self._on_failure(self, ext, err)
                obj = None
            ext.obj = obj

        self.mgrs.append(mgr) 
Example #11
Source File: multi_backend.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def _list_driver_opts():
    driver_opts = {}
    mgr = extension.ExtensionManager('glance_store.drivers')
    # NOTE(zhiyan): Handle available drivers entry_points provided
    # NOTE(nikhil): Return a sorted list of drivers to ensure that the sample
    # configuration files generated by oslo config generator retain the order
    # in which the config opts appear across different runs. If this order of
    # config opts is not preserved, some downstream packagers may see a long
    # diff of the changes though not relevant as only order has changed. See
    # some more details at bug 1619487.
    drivers = sorted([ext.name for ext in mgr])
    handled_drivers = []  # Used to handle backwards-compatible entries
    for store_entry in drivers:
        driver_cls = _load_multi_store(None, store_entry, False)
        if driver_cls and driver_cls not in handled_drivers:
            if getattr(driver_cls, 'OPTIONS', None) is not None:
                driver_opts[store_entry] = driver_cls.OPTIONS
            handled_drivers.append(driver_cls)

    # NOTE(zhiyan): This separated approach could list
    # store options before all driver ones, which easier
    # to read and configure by operator.
    return driver_opts 
Example #12
Source File: backend.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def _list_opts():
    driver_opts = []
    mgr = extension.ExtensionManager('glance_store.drivers')
    # NOTE(zhiyan): Handle available drivers entry_points provided
    # NOTE(nikhil): Return a sorted list of drivers to ensure that the sample
    # configuration files generated by oslo config generator retain the order
    # in which the config opts appear across different runs. If this order of
    # config opts is not preserved, some downstream packagers may see a long
    # diff of the changes though not relevant as only order has changed. See
    # some more details at bug 1619487.
    drivers = sorted([ext.name for ext in mgr])
    handled_drivers = []  # Used to handle backwards-compatible entries
    for store_entry in drivers:
        driver_cls = _load_store(None, store_entry, False)
        if driver_cls and driver_cls not in handled_drivers:
            if getattr(driver_cls, 'OPTIONS', None) is not None:
                driver_opts += driver_cls.OPTIONS
            handled_drivers.append(driver_cls)

    # NOTE(zhiyan): This separated approach could list
    # store options before all driver ones, which easier
    # to read and configure by operator.
    return ([(_STORE_CFG_GROUP, _STORE_OPTS)] +
            [(_STORE_CFG_GROUP, driver_opts)]) 
Example #13
Source File: test_extensions.py    From edx-analytics-pipeline with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_extensions_config(self):
        """
        Test if the edx.analytics.tasks entry_points are configured properly.

        Note that extensions are only defined after the package
        `edx.analytics.tasks` has been installed.

        """

        manager = ExtensionManager(
            namespace=EXTENSION_NAMESPACE,
            on_load_failure_callback=self.on_load_failure,
            verify_requirements=True
        )

        if len(manager.extensions) == 0:
            reason = 'no entry_points for {0} namespace'
            raise unittest.SkipTest(reason.format(EXTENSION_NAMESPACE)) 
Example #14
Source File: apps.py    From edx-proctoring with GNU Affero General Public License v3.0 6 votes vote down vote up
def ready(self):
        """
        Loads the available proctoring backends
        """
        from edx_proctoring import signals  # pylint: disable=unused-import
        config = settings.PROCTORING_BACKENDS

        self.backends = {}  # pylint: disable=W0201
        for extension in ExtensionManager(namespace='openedx.proctoring'):
            name = extension.name
            try:
                options = config[name]
                self.backends[name] = extension.plugin(**options)
            except KeyError:
                pass
        make_worker_config(list(self.backends.values()), out=os.path.join(settings.ENV_ROOT, 'workers.json')) 
Example #15
Source File: main.py    From doc8 with Apache License 2.0 6 votes vote down vote up
def fetch_checks(cfg):
    base = [
        checks.CheckValidity(cfg),
        checks.CheckTrailingWhitespace(cfg),
        checks.CheckIndentationNoTab(cfg),
        checks.CheckCarriageReturn(cfg),
        checks.CheckMaxLineLength(cfg),
        checks.CheckNewlineEndOfFile(cfg),
    ]
    mgr = extension.ExtensionManager(
        namespace="doc8.extension.check", invoke_on_load=True, invoke_args=(cfg.copy(),)
    )
    addons = []
    for e in mgr:
        addons.append(e.obj)
    return base + addons 
Example #16
Source File: base.py    From orquesta with Apache License 2.0 5 votes vote down vote up
def load():
    global _EXP_FUNC_CATALOG

    if _EXP_FUNC_CATALOG is None:
        _EXP_FUNC_CATALOG = {}

        mgr = extension.ExtensionManager(
            namespace="orquesta.expressions.functions", invoke_on_load=False
        )

        for name in mgr.names():
            _EXP_FUNC_CATALOG[name] = mgr[name].plugin

    return _EXP_FUNC_CATALOG 
Example #17
Source File: test_extension.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def test_map_errors_when_no_plugins(self):
        expected_str = 'No stevedore.test.extension.none extensions found'

        def mapped(ext, *args, **kwds):
            pass

        em = extension.ExtensionManager('stevedore.test.extension.none',
                                        invoke_on_load=True,
                                        )
        try:
            em.map(mapped, 1, 2, a='A', b='B')
        except exception.NoMatches as err:
            self.assertEqual(expected_str, str(err)) 
Example #18
Source File: protectable_registry.py    From karbor with Apache License 2.0 5 votes vote down vote up
def load_plugins(self):
        """Load all protectable plugins configured and register them.

        """
        mgr = extension.ExtensionManager(
            namespace='karbor.protectables',
            invoke_on_load=True,
            on_load_failure_callback=_warn_missing_protectable)

        for e in mgr:
            self.register_plugin(e.obj) 
Example #19
Source File: test_extension.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def setUp(self):
        super(TestLoadRequirementsOldSetuptools, self).setUp()
        self.mock_ep = mock.Mock(spec=['load', 'name'])
        self.em = extension.ExtensionManager.make_test_instance([]) 
Example #20
Source File: __init__.py    From aodh with Apache License 2.0 5 votes vote down vote up
def __init__(self, worker_id, conf):
        super(AlarmNotifierService, self).__init__(worker_id)
        self.conf = conf
        transport = messaging.get_transport(self.conf)
        self.notifiers = extension.ExtensionManager(
            self.NOTIFIER_EXTENSIONS_NAMESPACE,
            invoke_on_load=True,
            invoke_args=(self.conf,))

        target = oslo_messaging.Target(topic=self.conf.notifier_topic)
        self.listener = messaging.get_batch_notification_listener(
            transport, [target], [AlarmEndpoint(self.notifiers)], False,
            self.conf.notifier.batch_size, self.conf.notifier.batch_timeout)
        self.listener.start() 
Example #21
Source File: test_extension.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def test_map_method(self):
        em = extension.ExtensionManager('stevedore.test.extension',
                                        invoke_on_load=True,
                                        )

        result = em.map_method('get_args_and_data', 42)
        self.assertEqual(set(r[2] for r in result), set([42])) 
Example #22
Source File: test_extension.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def test_detect_plugins(self):
        em = extension.ExtensionManager('stevedore.test.extension')
        names = sorted(em.names())
        self.assertEqual(names, ALL_NAMES) 
Example #23
Source File: base.py    From orquesta with Apache License 2.0 5 votes vote down vote up
def get_evaluators():
    global _EXP_EVALUATORS

    if _EXP_EVALUATORS is None:
        _EXP_EVALUATORS = {}

        mgr = extension.ExtensionManager(namespace=_EXP_EVALUATOR_NAMESPACE, invoke_on_load=False)

        for name in mgr.names():
            _EXP_EVALUATORS[name] = get_evaluator(name)

    return _EXP_EVALUATORS 
Example #24
Source File: sphinxext.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def run(self):
        env = self.state.document.settings.env
        app = env.app

        namespace = ' '.join(self.content).strip()
        app.info('documenting plugins from %r' % namespace)
        overline_style = self.options.get('overline-style', '')
        underline_style = self.options.get('underline-style', '=')

        def report_load_failure(mgr, ep, err):
            app.warn(u'Failed to load %s: %s' % (ep.module_name, err))

        mgr = extension.ExtensionManager(
            namespace,
            on_load_failure_callback=report_load_failure,
        )

        result = ViewList()

        titlecase = 'titlecase' in self.options

        if 'detailed' in self.options:
            data = _detailed_list(
                mgr, over=overline_style, under=underline_style,
                titlecase=titlecase)
        else:
            data = _simple_list(mgr)
        for text, source in data:
            for line in text.splitlines():
                result.append(line, source)

        # Parse what we have into a new section.
        node = nodes.section()
        node.document = self.state.document
        nested_parse_with_titles(self.state, result, node)

        return node.children 
Example #25
Source File: extension.py    From python-tackerclient with Apache License 2.0 5 votes vote down vote up
def _discover_via_entry_points():
    emgr = extension.ExtensionManager('tackerclient.extension',
                                      invoke_on_load=False)
    return ((ext.name, ext.plugin) for ext in emgr) 
Example #26
Source File: exttools.py    From tosca-parser with Apache License 2.0 5 votes vote down vote up
def _load_extensions(self):
        '''Dynamically load all the extensions .'''
        extensions = collections.OrderedDict()

        extns = extension.ExtensionManager(namespace='toscaparser.extensions',
                                           invoke_on_load=True).extensions

        for e in extns:
            try:
                extinfo = importlib.import_module(e.plugin.__module__)
                base_path = os.path.dirname(extinfo.__file__)
                version = e.plugin().VERSION
                defs_file = base_path + '/' + e.plugin().DEFS_FILE

                # Sections is an optional attribute
                sections = getattr(e.plugin(), 'SECTIONS', ())

                extensions[version] = {'sections': sections,
                                       'defs_file': defs_file}
            except ImportError:
                raise ToscaExtImportError(ext_name=e.name)
            except AttributeError:
                attrs = ', '.join(REQUIRED_ATTRIBUTES)
                raise ToscaExtAttributeError(ext_name=e.name, attrs=attrs)

        return extensions 
Example #27
Source File: environment.py    From senlin with Apache License 2.0 5 votes vote down vote up
def _get_mapping(namespace):
    mgr = extension.ExtensionManager(
        namespace=namespace,
        invoke_on_load=False)
    return [[name, mgr[name].plugin] for name in mgr.names()] 
Example #28
Source File: __init__.py    From os-vif with Apache License 2.0 5 votes vote down vote up
def initialize(reset=False):
    """
    Loads all os_vif plugins and initializes them with a dictionary of
    configuration options. These configuration options are passed as-is
    to the individual VIF plugins that are loaded via stevedore.

    :param reset: Recreate and load the VIF plugin extensions.

    """
    global _EXT_MANAGER
    if _EXT_MANAGER is None:
        os_vif.objects.register_all()

    if reset or (_EXT_MANAGER is None):
        _EXT_MANAGER = extension.ExtensionManager(namespace='os_vif',
                                                  invoke_on_load=False)
        loaded_plugins = []
        for plugin_name in _EXT_MANAGER.names():
            cls = _EXT_MANAGER[plugin_name].plugin
            obj = cls.load(plugin_name)
            LOG.debug(("Loaded VIF plugin class '%(cls)s' "
                       "with name '%(plugin_name)s'"),
                      {'cls': cls, 'plugin_name': plugin_name})
            loaded_plugins.append(plugin_name)
            _EXT_MANAGER[plugin_name].obj = obj
        LOG.info("Loaded VIF plugins: %s", ", ".join(loaded_plugins)) 
Example #29
Source File: driver_loader.py    From st2 with Apache License 2.0 5 votes vote down vote up
def get_available_backends(namespace, invoke_on_load=False):
    """
    Return names of the available / installed backends.

    :rtype: ``list`` of ``str``
    """
    # NOTE: We use lazy import because importing from stevedore adds significat import time
    # overhead to other modules which don't need this package (stevedore needs to inspect various
    # entrypoint files on disk for all the installed Python packages which is slow)
    from stevedore.extension import ExtensionManager

    manager = ExtensionManager(namespace=namespace, invoke_on_load=invoke_on_load)
    return manager.names() 
Example #30
Source File: loader.py    From st2 with Apache License 2.0 5 votes vote down vote up
def get_available_plugins(namespace):
    """Return names of the available / installed plugins for a given namespace.
    """
    from stevedore.extension import ExtensionManager

    manager = ExtensionManager(namespace=namespace, invoke_on_load=False)
    return manager.names()