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('tank.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('tank.common.exception.TankException') self.assertTrue(ex_obj.__class__.__name__ == 'TankException') # Try importing a module itself module_obj = utils.import_object('tank.registry') self.assertEqual('tank.registry', module_obj.__package__)
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("tank.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("tank.common.exception.TankException") self.assertTrue(ex_obj.__class__.__name__ == "TankException") # Try importing a module itself module_obj = utils.import_object("tank.registry") self.assertEqual("tank.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.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 __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 create_stores(conf): """ 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(conf)
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 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)