Python stevedore.driver.DriverManager() Examples

The following are 30 code examples of stevedore.driver.DriverManager(). 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.driver , or try the search function .
Example #1
Source File: formatter.py    From eastern with MIT License 6 votes vote down vote up
def format(filename, env=None):
    """
    Format a file

    :param filename: Path to file
    :type filename: str or :py:class:`pathlib.Path`
    :param dict[str,str] env: List of variables
    """
    ext = str(filename).split(".")[-1]
    driver = DriverManager("eastern.formatter", ext)
    driver.propagate_map_exceptions = True
    env = env or {}

    if isinstance(filename, Path):
        file_obj = filename
    else:
        file_obj = Path(filename)

    body = file_obj.read_text()
    return driver(lambda ext: ext.plugin(body, file_obj.parent, env).format()) 
Example #2
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 #3
Source File: os_vif_util.py    From zun with Apache License 2.0 6 votes vote down vote up
def neutron_to_osvif_vif(vif_translator, neutron_port, network, subnets):
    """Converts Neutron port to os-vif VIF object.

    :param vif_translator: name of the traslator for the os-vif plugin to use
    :param neutron_port: dict containing port information as returned by
                         neutron client
    :param subnets: subnet mapping
    :return: os-vif VIF object
    """

    try:
        mgr = _VIF_MANAGERS[vif_translator]
    except KeyError:
        mgr = stv_driver.DriverManager(
            namespace=_VIF_TRANSLATOR_NAMESPACE,
            name=vif_translator, invoke_on_load=False)
        _VIF_MANAGERS[vif_translator] = mgr

    return mgr.driver(vif_translator, neutron_port, network, subnets) 
Example #4
Source File: test_driver.py    From bazarr with GNU General Public License v3.0 6 votes vote down vote up
def test_multiple_drivers(self):
        # The idea for this test was contributed by clayg:
        # https://gist.github.com/clayg/6311348
        extensions = [
            extension.Extension(
                'backend',
                pkg_resources.EntryPoint.parse('backend = pkg1:driver'),
                'pkg backend',
                None,
            ),
            extension.Extension(
                'backend',
                pkg_resources.EntryPoint.parse('backend = pkg2:driver'),
                'pkg backend',
                None,
            ),
        ]
        try:
            dm = driver.DriverManager.make_test_instance(extensions[0])
            # Call the initialization code that verifies the extension
            dm._init_plugins(extensions)
        except exception.MultipleMatches as err:
            self.assertIn("Multiple", str(err))
        else:
            self.fail('Should have had an error') 
Example #5
Source File: __init__.py    From aodh with Apache License 2.0 6 votes vote down vote up
def get_connection_from_config(conf):
    retries = conf.database.max_retries
    url = conf.database.connection
    connection_scheme = urlparse.urlparse(url).scheme
    LOG.debug('looking for %(name)r driver in %(namespace)r',
              {'name': connection_scheme, 'namespace': _NAMESPACE})
    mgr = driver.DriverManager(_NAMESPACE, connection_scheme)

    @tenacity.retry(
        wait=tenacity.wait_fixed(conf.database.retry_interval),
        stop=tenacity.stop_after_attempt(retries if retries >= 0 else 5),
        reraise=True)
    def _get_connection():
        """Return an open connection to the database."""
        return mgr.driver(conf, url)

    return _get_connection() 
Example #6
Source File: os_vif_util.py    From kuryr-kubernetes with Apache License 2.0 6 votes vote down vote up
def neutron_to_osvif_vif(vif_translator, os_port, subnets):
    """Converts Neutron port to os-vif VIF object.

    :param vif_translator: name of the traslator for the os-vif plugin to use
    :param os_port: openstack.network.v2.port.Port object
    :param subnets: subnet mapping as returned by PodSubnetsDriver.get_subnets
    :return: os-vif VIF object
    """
    try:
        mgr = _VIF_MANAGERS[vif_translator]
    except KeyError:
        mgr = stv_driver.DriverManager(
            namespace=_VIF_TRANSLATOR_NAMESPACE,
            name=vif_translator, invoke_on_load=False)
        _VIF_MANAGERS[vif_translator] = mgr

    return mgr.driver(vif_translator, os_port, subnets) 
Example #7
Source File: __init__.py    From castellan with Apache License 2.0 6 votes vote down vote up
def API(configuration=None):
    conf = configuration or cfg.CONF
    conf.register_opts(key_manager_opts, group='key_manager')

    try:
        mgr = driver.DriverManager("castellan.drivers",
                                   conf.key_manager.backend,
                                   invoke_on_load=True,
                                   invoke_args=[conf])
        key_mgr = mgr.driver
    except exception.NoMatches:
        LOG.warning("Deprecation Warning : %s is not a stevedore based driver,"
                    " trying to load it as a class", conf.key_manager.backend)
        cls = importutils.import_class(conf.key_manager.backend)
        key_mgr = cls(configuration=conf)

    return migration.handle_migration(conf, key_mgr) 
Example #8
Source File: nova_driver.py    From octavia with Apache License 2.0 6 votes vote down vote up
def __init__(self):
        super(VirtualMachineManager, self).__init__()
        # Must initialize nova api
        self._nova_client = clients.NovaAuth.get_nova_client(
            endpoint=CONF.nova.endpoint,
            region=CONF.nova.region_name,
            endpoint_type=CONF.nova.endpoint_type,
            insecure=CONF.nova.insecure,
            cacert=CONF.nova.ca_certificates_file)
        self._glance_client = clients.GlanceAuth.get_glance_client(
            service_name=CONF.glance.service_name,
            endpoint=CONF.glance.endpoint,
            region=CONF.glance.region_name,
            endpoint_type=CONF.glance.endpoint_type,
            insecure=CONF.glance.insecure,
            cacert=CONF.glance.ca_certificates_file)
        self.manager = self._nova_client.servers
        self.server_groups = self._nova_client.server_groups
        self.flavor_manager = self._nova_client.flavors
        self.availability_zone_manager = self._nova_client.availability_zones
        self.volume_driver = stevedore_driver.DriverManager(
            namespace='octavia.volume.drivers',
            name=CONF.controller_worker.volume_driver,
            invoke_on_load=True
        ).driver 
Example #9
Source File: driver_factory.py    From octavia with Apache License 2.0 6 votes vote down vote up
def get_driver(provider):
    # If this came in None it must be a load balancer that existed before
    # provider support was added. These must be of type 'amphora' and not
    # whatever the current "default" is set to.
    if isinstance(provider, wtypes.UnsetType):
        provider = CONF.api_settings.default_provider_driver
    elif not provider:
        provider = 'amphora'

    if provider not in CONF.api_settings.enabled_provider_drivers:
        LOG.warning("Requested provider driver '%s' was not enabled in the "
                    "configuration file.", provider)
        raise exceptions.ProviderNotEnabled(prov=provider)

    try:
        driver = stevedore_driver.DriverManager(
            namespace='octavia.api.drivers',
            name=provider,
            invoke_on_load=True).driver
        driver.name = provider
    except Exception as e:
        LOG.error('Unable to load provider driver %s due to: %s',
                  provider, e)
        raise exceptions.ProviderNotFound(prov=provider)
    return driver 
Example #10
Source File: driver_loader.py    From st2 with Apache License 2.0 6 votes vote down vote up
def get_backend_driver(namespace, name, invoke_on_load=False):
    """
    Retrieve a driver (module / class / function) the provided backend.

    :param name: Backend name.
    :type name: ``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.driver import DriverManager

    LOG.debug('Retrieving driver for backend "%s"' % (name))

    try:
        manager = DriverManager(namespace=namespace, name=name,
                                invoke_on_load=invoke_on_load)
    except RuntimeError:
        message = 'Invalid "%s" backend specified: %s' % (namespace, name)
        LOG.exception(message)
        raise ValueError(message)

    return manager.driver 
Example #11
Source File: rest_api_driver.py    From octavia with Apache License 2.0 6 votes vote down vote up
def __init__(self):
        super(HaproxyAmphoraLoadBalancerDriver, self).__init__()
        self.clients = {
            'base': AmphoraAPIClientBase(),
            '0.5': AmphoraAPIClient0_5(),
            '1.0': AmphoraAPIClient1_0(),
        }
        self.cert_manager = stevedore_driver.DriverManager(
            namespace='octavia.cert_manager',
            name=CONF.certificates.cert_manager,
            invoke_on_load=True,
        ).driver

        self.jinja_combo = jinja_combo.JinjaTemplater(
            base_amp_path=CONF.haproxy_amphora.base_path,
            base_crt_dir=CONF.haproxy_amphora.base_cert_dir,
            haproxy_template=CONF.haproxy_amphora.haproxy_template,
            connection_logging=CONF.haproxy_amphora.connection_logging)
        self.jinja_split = jinja_split.JinjaTemplater(
            base_amp_path=CONF.haproxy_amphora.base_path,
            base_crt_dir=CONF.haproxy_amphora.base_cert_dir,
            haproxy_template=CONF.haproxy_amphora.haproxy_template,
            connection_logging=CONF.haproxy_amphora.connection_logging)
        self.udp_jinja = jinja_udp_cfg.LvsJinjaTemplater() 
Example #12
Source File: controller_worker.py    From octavia with Apache License 2.0 6 votes vote down vote up
def __init__(self):

        self._amphora_repo = repo.AmphoraRepository()
        self._amphora_health_repo = repo.AmphoraHealthRepository()
        self._health_mon_repo = repo.HealthMonitorRepository()
        self._lb_repo = repo.LoadBalancerRepository()
        self._listener_repo = repo.ListenerRepository()
        self._member_repo = repo.MemberRepository()
        self._pool_repo = repo.PoolRepository()
        self._l7policy_repo = repo.L7PolicyRepository()
        self._l7rule_repo = repo.L7RuleRepository()
        self._flavor_repo = repo.FlavorRepository()
        self._az_repo = repo.AvailabilityZoneRepository()

        persistence = tsk_driver.MysqlPersistenceDriver()

        self.jobboard_driver = stevedore_driver.DriverManager(
            namespace='octavia.worker.jobboard_driver',
            name=CONF.task_flow.jobboard_backend_driver,
            invoke_args=(persistence,),
            invoke_on_load=True).driver 
Example #13
Source File: test_driver.py    From stevedore with Apache License 2.0 6 votes vote down vote up
def test_multiple_drivers(self):
        # The idea for this test was contributed by clayg:
        # https://gist.github.com/clayg/6311348
        extensions = [
            extension.Extension(
                'backend',
                pkg_resources.EntryPoint.parse('backend = pkg1:driver'),
                'pkg backend',
                None,
            ),
            extension.Extension(
                'backend',
                pkg_resources.EntryPoint.parse('backend = pkg2:driver'),
                'pkg backend',
                None,
            ),
        ]
        try:
            dm = driver.DriverManager.make_test_instance(extensions[0])
            # Call the initialization code that verifies the extension
            dm._init_plugins(extensions)
        except exception.MultipleMatches as err:
            self.assertIn("Multiple", str(err))
        else:
            self.fail('Should have had an error') 
Example #14
Source File: multi_backend.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def _load_multi_store(conf, store_entry,
                      invoke_load=True,
                      backend=None):
    if backend:
        invoke_args = [conf, backend]
    else:
        invoke_args = [conf]
    try:
        LOG.debug("Attempting to import store %s", store_entry)
        mgr = driver.DriverManager('glance_store.drivers',
                                   store_entry,
                                   invoke_args=invoke_args,
                                   invoke_on_load=invoke_load)
        return mgr.driver
    except RuntimeError as e:
        LOG.warning("Failed to load driver %(driver)s. The "
                    "driver will be disabled", dict(driver=str([driver, e]))) 
Example #15
Source File: utils.py    From dragonflow with Apache License 2.0 6 votes vote down vote up
def load_driver(driver_cfg, namespace, *args, **kwargs):
    try:
        # Try to resolve by alias
        mgr = driver.DriverManager(namespace, driver_cfg)
        class_to_load = mgr.driver
    except RuntimeError:
        e1_info = sys.exc_info()
        # try with name
        try:
            class_to_load = importutils.import_class(driver_cfg)
        except (ImportError, ValueError):
            LOG.error("Error loading class %(class)s by alias e: %(e)s",
                      {'class': driver_cfg, 'e': e1_info},
                      exc_info=e1_info)
            LOG.error("Error loading class by class name",
                      exc_info=True)
            raise ImportError(_("Class not found."))
    return class_to_load(*args, **kwargs) 
Example #16
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 #17
Source File: default.py    From watcher with Apache License 2.0 6 votes vote down vote up
def load(self, name, **kwargs):
        try:
            LOG.debug("Loading in namespace %s => %s ", self.namespace, name)
            driver_manager = drivermanager.DriverManager(
                namespace=self.namespace,
                name=name,
                invoke_on_load=False,
            )

            driver_cls = driver_manager.driver
            config = self._load_plugin_config(name, driver_cls)

            driver = driver_cls(config, **kwargs)
        except Exception as exc:
            LOG.exception(exc)
            raise exception.LoadingError(name=name)

        return driver 
Example #18
Source File: test_driver.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def test_no_drivers(self):
        try:
            driver.DriverManager('stevedore.test.extension.none', 't1')
        except exception.NoMatches as err:
            self.assertIn("No 'stevedore.test.extension.none' driver found",
                          str(err)) 
Example #19
Source File: test_driver.py    From stevedore with Apache License 2.0 5 votes vote down vote up
def test_driver_property_not_invoked_on_load(self):
        em = driver.DriverManager('stevedore.test.extension', 't1',
                                  invoke_on_load=False)
        d = em.driver
        self.assertIs(d, test_extension.FauxExtension) 
Example #20
Source File: migration.py    From masakari with Apache License 2.0 5 votes vote down vote up
def get_backend():
    global _IMPL
    if _IMPL is None:
        with _LOCK:
            if _IMPL is None:
                _IMPL = driver.DriverManager(
                    "masakari.database.migration_backend",
                    cfg.CONF.database.backend).driver
    return _IMPL 
Example #21
Source File: driver.py    From masakari with Apache License 2.0 5 votes vote down vote up
def load_masakari_driver(masakari_driver=None):
    """Load a masakari driver module.

    Load the masakari driver module specified by the notification_driver
    configuration option or, if supplied, the driver name supplied as an
    argument.

    :param masakari_driver: a masakari driver name to override the config opt
    :returns: a NotificationDriver instance
    """
    if not masakari_driver:
        masakari_driver = CONF.notification_driver

    if not masakari_driver:
        LOG.error("Notification driver option required, but not specified")
        sys.exit(1)

    LOG.info("Loading masakari notification driver '%s'", masakari_driver)
    try:
        notification_driver = driver.DriverManager('masakari.driver',
                                                   masakari_driver,
                                                   invoke_on_load=True).driver
        return utils.check_isinstance(notification_driver, NotificationDriver)
    except ImportError:
        LOG.exception("Failed to load notification driver '%s'.",
                      masakari_driver)
        sys.exit(1) 
Example #22
Source File: __init__.py    From taskflow with Apache License 2.0 5 votes vote down vote up
def fetch(name, conf, namespace=BACKEND_NAMESPACE, **kwargs):
    """Fetch a jobboard backend with the given configuration.

    This fetch method will look for the entrypoint name in the entrypoint
    namespace, and then attempt to instantiate that entrypoint using the
    provided name, configuration and any board specific kwargs.

    NOTE(harlowja): to aid in making it easy to specify configuration and
    options to a board the configuration (which is typical just a dictionary)
    can also be a URI string that identifies the entrypoint name and any
    configuration specific to that board.

    For example, given the following configuration URI::

        zookeeper://<not-used>/?a=b&c=d

    This will look for the entrypoint named 'zookeeper' and will provide
    a configuration object composed of the URI's components, in this case that
    is ``{'a': 'b', 'c': 'd'}`` to the constructor of that board
    instance (also including the name specified).
    """
    board, conf = misc.extract_driver_and_conf(conf, 'board')
    LOG.debug('Looking for %r jobboard driver in %r', board, namespace)
    try:
        mgr = driver.DriverManager(namespace, board,
                                   invoke_on_load=True,
                                   invoke_args=(name, conf),
                                   invoke_kwds=kwargs)
        return mgr.driver
    except RuntimeError as e:
        raise exc.NotFound("Could not find jobboard %s" % (board), e) 
Example #23
Source File: test_driver.py    From stevedore with Apache License 2.0 5 votes vote down vote up
def test_detect_plugins(self):
        em = driver.DriverManager('stevedore.test.extension', 't1')
        names = sorted(em.names())
        self.assertEqual(names, ['t1']) 
Example #24
Source File: test_driver.py    From stevedore with Apache License 2.0 5 votes vote down vote up
def test_driver_property_invoked_on_load(self):
        em = driver.DriverManager('stevedore.test.extension', 't1',
                                  invoke_on_load=True)
        d = em.driver
        self.assertIsInstance(d, test_extension.FauxExtension) 
Example #25
Source File: test_driver.py    From stevedore with Apache License 2.0 5 votes vote down vote up
def test_no_drivers(self):
        try:
            driver.DriverManager('stevedore.test.extension.none', 't1')
        except exception.NoMatches as err:
            self.assertIn("No 'stevedore.test.extension.none' driver found",
                          str(err)) 
Example #26
Source File: __init__.py    From taskflow with Apache License 2.0 5 votes vote down vote up
def fetch(conf, namespace=BACKEND_NAMESPACE, **kwargs):
    """Fetch a persistence backend with the given configuration.

    This fetch method will look for the entrypoint name in the entrypoint
    namespace, and then attempt to instantiate that entrypoint using the
    provided configuration and any persistence backend specific kwargs.

    NOTE(harlowja): to aid in making it easy to specify configuration and
    options to a backend the configuration (which is typical just a dictionary)
    can also be a URI string that identifies the entrypoint name and any
    configuration specific to that backend.

    For example, given the following configuration URI::

        mysql://<not-used>/?a=b&c=d

    This will look for the entrypoint named 'mysql' and will provide
    a configuration object composed of the URI's components, in this case that
    is ``{'a': 'b', 'c': 'd'}`` to the constructor of that persistence backend
    instance.
    """
    backend, conf = misc.extract_driver_and_conf(conf, 'connection')
    # If the backend is like 'mysql+pymysql://...' which informs the
    # backend to use a dialect (supported by sqlalchemy at least) we just want
    # to look at the first component to find our entrypoint backend name...
    if backend.find("+") != -1:
        backend = backend.split("+", 1)[0]
    LOG.debug('Looking for %r backend driver in %r', backend, namespace)
    try:
        mgr = driver.DriverManager(namespace, backend,
                                   invoke_on_load=True,
                                   invoke_args=(conf,),
                                   invoke_kwds=kwargs)
        return mgr.driver
    except RuntimeError as e:
        raise exc.NotFound("Could not find backend %s: %s" % (backend, e)) 
Example #27
Source File: __init__.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def get_graph_driver():
    try:
        mgr = driver.DriverManager('vitrage.entity_graph',
                                   CONF.entity_graph.graph_driver,
                                   invoke_on_load=True)
        return mgr.driver
    except ImportError:
        return None 
Example #28
Source File: __init__.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def get_connection_from_config():
    retries = CONF.database.max_retries
    url = CONF.database.connection

    try:
        # TOTO(iafek): check why this call randomly fails
        connection_scheme = urlparse.urlparse(url).scheme
        LOG.debug('looking for %(name)r driver in %(namespace)r',
                  {'name': connection_scheme, 'namespace': _NAMESPACE})
        mgr = driver.DriverManager(_NAMESPACE, connection_scheme)

    except Exception:
        LOG.exception('Failed to get scheme %s.' % url)
        return None

    @tenacity.retry(
        wait=tenacity.wait_fixed(CONF.database.retry_interval),
        stop=tenacity.stop_after_attempt(retries),
        after=tenacity.after_log(LOG, log.WARN),
        reraise=True)
    def _get_connection():
        """Return an open connection to the database."""
        conn = mgr.driver(url)
        session = conn._engine_facade.get_session()
        session.execute('SELECT 1;')
        return conn

    return _get_connection() 
Example #29
Source File: orchestrator.py    From cloudkitty with Apache License 2.0 5 votes vote down vote up
def __init__(self, worker_id):
        self._worker_id = worker_id
        super(Orchestrator, self).__init__(self._worker_id)

        self.fetcher = driver.DriverManager(
            FETCHERS_NAMESPACE,
            CONF.fetcher.backend,
            invoke_on_load=True,
        ).driver

        self.collector = collector.get_collector()
        self.storage = storage.get_storage()
        self._state = state.StateManager()

        # RPC
        self.server = None
        self._rating_endpoint = RatingEndpoint(self)
        self._scope_endpoint = ScopeEndpoint()
        self._init_messaging()

        # DLM
        self.coord = coordination.get_coordinator(
            CONF.orchestrator.coordination_url,
            uuidutils.generate_uuid().encode('ascii'))
        self.coord.start(start_heart=True)
        self._check_state = functools.partial(
            _check_state, self, CONF.collect.period) 
Example #30
Source File: test_driver.py    From stevedore with Apache License 2.0 5 votes vote down vote up
def test_call(self):
        def invoke(ext, *args, **kwds):
            return (ext.name, args, kwds)
        em = driver.DriverManager('stevedore.test.extension', 't1')
        result = em(invoke, 'a', b='C')
        self.assertEqual(result, ('t1', ('a',), {'b': 'C'}))