Python oslo_utils.importutils.try_import() Examples

The following are 6 code examples of oslo_utils.importutils.try_import(). 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 oslo_utils.importutils , or try the search function .
Example #1
Source File: fixtures.py    From designate with Apache License 2.0 6 votes vote down vote up
def stop(self):
        LOG.info('Stopping service %s (%s)', self.svc_name, id(self.svc))

        try:
            self.svc.stop()

        except Exception:
            LOG.error('Failed to stop service %s (%s)',
                      self.svc_name, id(self.svc))
            raise

        finally:
            # Always try reset the service's RPCAPI
            mod = importutils.try_import('designate.%s.rpcapi' % self.svc_name)
            if hasattr(mod, 'reset'):
                LOG.info('Resetting service %s RPCAPI', self.svc_name)
                mod.reset() 
Example #2
Source File: actions.py    From mistral-extra with Apache License 2.0 5 votes vote down vote up
def _try_import(module_name):
    try:
        return importutils.try_import(module_name)
    except Exception as e:
        msg = 'Unable to load module "%s". %s' % (module_name, str(e))
        LOG.error(msg)
        return None 
Example #3
Source File: service.py    From cyborg with Apache License 2.0 5 votes vote down vote up
def __init__(self, manager_module, manager_class, topic, host=None):
        super(RPCService, self).__init__()
        self.topic = topic
        self.host = host or CONF.host
        manager_module = importutils.try_import(manager_module)
        manager_class = getattr(manager_module, manager_class)
        self.manager = manager_class(self.topic, self.host)
        self.rpcserver = None 
Example #4
Source File: test_importutils.py    From oslo.utils with Apache License 2.0 5 votes vote down vote up
def test_try_import(self):
        dt = importutils.try_import('datetime')
        self.assertEqual(sys.modules['datetime'], dt) 
Example #5
Source File: test_importutils.py    From oslo.utils with Apache License 2.0 5 votes vote down vote up
def test_try_import_returns_default(self):
        foo = importutils.try_import('foo.bar')
        self.assertIsNone(foo) 
Example #6
Source File: messaging.py    From osprofiler with Apache License 2.0 4 votes vote down vote up
def __init__(self, connection_str, project=None, service=None, host=None,
                 context=None, conf=None, transport_url=None,
                 idle_timeout=1, **kwargs):
        """Driver that uses messaging as transport for notifications

        :param connection_str: OSProfiler driver connection string,
               equals to messaging://
        :param project: project name that will be included into notification
        :param service: service name that will be included into notification
        :param host: host name that will be included into notification
        :param context: oslo.messaging context
        :param conf: oslo.config CONF object
        :param transport_url: oslo.messaging transport, e.g.
               rabbit://rabbit:password@devstack:5672/
        :param idle_timeout: how long to wait for new notifications after
               the last one seen in the trace; this parameter is useful to
               collect full trace of asynchronous commands, e.g. when user
               runs `osprofiler` right after `openstack server create`
        :param kwargs: black hole for any other parameters
        """

        self.oslo_messaging = importutils.try_import("oslo_messaging")
        if not self.oslo_messaging:
            raise ValueError("Oslo.messaging library is required for "
                             "messaging driver")

        super(Messaging, self).__init__(connection_str, project=project,
                                        service=service, host=host)

        self.context = context

        if not conf:
            oslo_config = importutils.try_import("oslo_config")
            if not oslo_config:
                raise ValueError("Oslo.config library is required for "
                                 "messaging driver")
            conf = oslo_config.cfg.CONF

        transport_kwargs = {}
        if transport_url:
            transport_kwargs["url"] = transport_url

        self.transport = self.oslo_messaging.get_notification_transport(
            conf, **transport_kwargs)
        self.client = self.oslo_messaging.Notifier(
            self.transport, publisher_id=self.host, driver="messaging",
            topics=["profiler"], retry=0)

        self.idle_timeout = idle_timeout