def __init__(self, conf, strategy=None): conf.register_opts(self.opts) strategy = conf.notifier_strategy try: self.strategy = utils.import_class(_STRATEGIES[strategy])(conf) except (KeyError, ImportError): raise exception.InvalidNotifierStrategy(strategy=strategy)
def test_import_class_or_object(self): # Test that import_class raises a descriptive error when the # class to import could not be found. self.assertRaises(exception.ImportFailure, utils.import_class, 'nomodule') self.assertRaises(exception.ImportFailure, utils.import_class, 'mymodule.nonexistingclass') self.assertRaises(exception.ImportFailure, utils.import_class, 'sys.nonexistingclass') self.assertRaises(exception.ImportFailure, utils.import_object, 'os.path.NONEXISTINGOBJECT') store_class = utils.import_class('glance.store.s3.Store') self.assertTrue(store_class.__name__ == 'Store') # Try importing an object by supplying a class and # verify the object's class name is the same as that supplied ex_obj = utils.import_object('glance.common.exception.GlanceException') self.assertTrue(ex_obj.__class__.__name__ == 'GlanceException') # Try importing a module itself module_obj = utils.import_object('glance.registry') self.assertEqual('glance.registry', module_obj.__package__)
def __init__(self, conf, strategy=None): conf.register_opts(self.opts) strategy = conf.notifier_strategy try: self.strategy = utils.import_class(_STRATEGIES[strategy])(conf) except KeyError, ImportError: raise exception.InvalidNotifierStrategy(strategy=strategy)
def init_driver(self): """ Create the driver for the cache """ driver_name = self.options.get("image_cache_driver", "sqlite") driver_module = __name__ + ".drivers." + driver_name + ".Driver" try: self.driver_class = utils.import_class(driver_module) logger.info(_("Image cache loaded driver '%s'.") % driver_name) except exception.ImportFailure, import_err: logger.warn( _("Image cache driver " "'%(driver_name)s' failed to load. " "Got error: '%(import_err)s.") % locals() ) driver_module = __name__ + ".drivers.sqlite.Driver" logger.info(_("Defaulting to SQLite driver.")) self.driver_class = utils.import_class(driver_module)
def init_driver(self): """ Create the driver for the cache """ driver_name = self.conf.image_cache_driver driver_module = (__name__ + '.drivers.' + driver_name + '.Driver') try: self.driver_class = utils.import_class(driver_module) logger.info(_("Image cache loaded driver '%s'.") % driver_name) except exception.ImportFailure, import_err: logger.warn( _("Image cache driver " "'%(driver_name)s' failed to load. " "Got error: '%(import_err)s.") % locals()) driver_module = __name__ + '.drivers.sqlite.Driver' logger.info(_("Defaulting to SQLite driver.")) self.driver_class = utils.import_class(driver_module)
def get_store_location_class(self): """ Returns the store location class that is used by this store. """ if not self.store_location_class: class_name = "%s.StoreLocation" % (self.__module__) logger.debug("Late loading location class %s", class_name) self.store_location_class = utils.import_class(class_name) return self.store_location_class
def _get_store_class(store_entry): store_cls = None try: logger.debug("Attempting to import store %s", store_entry) store_cls = utils.import_class(store_entry) except exception.NotFound: raise BackendException('Unable to load store. ' 'Could not find a class named %s.' % store_entry) return store_cls
def __init__(self, app, conf, **local_conf): self.conf = conf self.conf.register_opts(self.opts) # Determine the context class to use self.ctxcls = RequestContext if 'context_class' in local_conf: self.ctxcls = utils.import_class(local_conf['context_class']) super(ContextMiddleware, self).__init__(app)
def register_store(store_module, schemes): """ Registers a store module and a set of schemes for which a particular URI request should be routed. :param store_module: String representing the store module :param schemes: List of strings representing schemes for which this store should be used in routing """ try: utils.import_class(store_module + '.Store') except exception.NotFound: raise BackendException('Unable to register store. Could not find ' 'a class named Store in module %s.' % store_module) REGISTERED_STORE_MODULES.append(store_module) scheme_map = {} for scheme in schemes: scheme_map[scheme] = store_module location.register_scheme_map(scheme_map)
def make_context(self, *args, **kwargs): """ Create a context with the given arguments. """ # Determine the context class to use ctxcls = RequestContext if 'context_class' in self.options: ctxcls = utils.import_class(self.options['context_class']) return ctxcls(*args, **kwargs)
def create_stores(options): """ Construct the store objects with supplied configuration options """ for store_module in REGISTERED_STORE_MODULES: try: store_class = utils.import_class(store_module + '.Store') except exception.NotFound: raise BackendException('Unable to create store. Could not find ' 'a class named Store in module %s.' % store_module) STORES[store_module] = store_class(options)
def _get_store_location(self): """ We find the store module and then grab an instance of the store's StoreLocation class which handles store-specific location information """ try: cls = utils.import_class('glance.store.%s.StoreLocation' % SCHEME_TO_STORE_MAP[self.store_name]) return cls(self.store_specs) except exception.NotFound: logger.error("Unable to find StoreLocation class in store %s", self.store_name) return None
def _get_store_location(self): """ We find the store module and then grab an instance of the store's StoreLocation class which handles store-specific location information """ try: cls = utils.import_class('%s.StoreLocation' % SCHEME_TO_STORE_MAP[self.store_name]) return cls(self.store_specs) except exception.NotFound: msg = _("Unable to find StoreLocation class in store " "%s") % self.store_name logger.error(msg) return None
def make_context(self, *args, **kwargs): """ Create a context with the given arguments. """ # Determine the context class to use ctxcls = RequestContext if 'context_class' in self.options: ctxcls = utils.import_class(self.options['context_class']) # Determine whether to use tenant or owner owner_is_tenant = config.get_option(self.options, 'owner_is_tenant', type='bool', default=True) kwargs.setdefault('owner_is_tenant', owner_is_tenant) return ctxcls(*args, **kwargs)
def configure_driver(self): """ Configure the driver for the cache and, if it fails to configure, fall back to using the SQLite driver which has no odd dependencies """ try: self.driver = self.driver_class(self.conf) self.driver.configure() except exception.BadDriverConfiguration, config_err: driver_module = self.driver_class.__module__ logger.warn(_("Image cache driver " "'%(driver_module)s' failed to configure. " "Got error: '%(config_err)s") % locals()) logger.info(_("Defaulting to SQLite driver.")) default_module = __name__ + '.drivers.sqlite.Driver' self.driver_class = utils.import_class(default_module) self.driver = self.driver_class(self.conf) self.driver.configure()
def _import_factory(self, local_conf): """Import an app/filter class. Lookup the KEY from the PasteDeploy local conf and import the class named there. This class can then be used as an app or filter factory. Note we support the <module>:<class> format. Note also that if you do e.g. key = value then ConfigParser returns a value with a leading newline, so we strip() the value before using it. """ class_name = local_conf[self.KEY].replace(':', '.').strip() return utils.import_class(class_name)
def _import_factory(self, local_conf): """Import an app/filter class. Lookup the KEY from the PasteDeploy local conf and import the class named therein. This class can then be used as an app or filter factory. Note we support the <module>:<class> format. Note also that if you do e.g. key = value then ConfigParser returns a value with a leading newline, so we strip() the value before using it. """ class_name = local_conf[self.KEY].replace(':', '.').strip() return utils.import_class(class_name)