def _derive_endpoints(tasks): """Derive endpoints from list of strings, classes or packages.""" derived_tasks = set() for item in tasks: module = None if isinstance(item, six.string_types): try: pkg, cls = item.split(":") except ValueError: module = importutils.import_module(item) else: obj = importutils.import_class("%s.%s" % (pkg, cls)) if not reflection.is_subclass(obj, t_task.BaseTask): raise TypeError("Item %s is not a BaseTask subclass" % item) derived_tasks.add(obj) elif isinstance(item, types.ModuleType): module = item elif reflection.is_subclass(item, t_task.BaseTask): derived_tasks.add(item) else: raise TypeError("Item %s unexpected type: %s" % (item, type(item))) # derive tasks if module is not None: for name, obj in inspect.getmembers(module): if reflection.is_subclass(obj, t_task.BaseTask): derived_tasks.add(obj) return [endpoint.Endpoint(task) for task in derived_tasks]
def _derive_endpoints(tasks): """Derive endpoints from list of strings, classes or packages.""" derived_tasks = set() for item in tasks: module = None if isinstance(item, six.string_types): try: pkg, cls = item.split(':') except ValueError: module = importutils.import_module(item) else: obj = importutils.import_class('%s.%s' % (pkg, cls)) if not reflection.is_subclass(obj, t_task.BaseTask): raise TypeError("Item %s is not a BaseTask subclass" % item) derived_tasks.add(obj) elif isinstance(item, types.ModuleType): module = item elif reflection.is_subclass(item, t_task.BaseTask): derived_tasks.add(item) else: raise TypeError("Item %s unexpected type: %s" % (item, type(item))) # derive tasks if module is not None: for name, obj in inspect.getmembers(module): if reflection.is_subclass(obj, t_task.BaseTask): derived_tasks.add(obj) return [endpoint.Endpoint(task) for task in derived_tasks]
def fetch(backend): """Fetch a backend impl. for a given backend type.""" with _BACKEND_MAPPING_LOCK: if backend not in _BACKEND_MAPPING: raise exc.NotFound("Unknown backend %s requested" % (backend)) mod = _BACKEND_MAPPING.get(backend, backend) with _BACKEND_LOCK: if mod in _BACKENDS: return _BACKENDS[mod] backend_mod = importutils.import_module(mod) backend_impl = backend_mod.get_backend() _BACKENDS[mod] = backend_impl return backend_impl
def __get_backend(self): """Get the actual backend. May be a module or an instance of a class. Doesn't matter to us. We do this synchronized as it's possible multiple greenthreads started very quickly trying to do DB calls and eventlet can switch threads before self.__backend gets assigned. """ if self.__backend: # Another thread assigned it return self.__backend backend_name = CONF.database.backend self.__use_tpool = CONF.database.use_tpool if self.__use_tpool: from eventlet import tpool self.__tpool = tpool # Import the untranslated name if we don't have a # mapping. backend_path = self.__backend_mapping.get(backend_name, backend_name) backend_mod = importutils.import_module(backend_path) self.__backend = backend_mod.get_backend() return self.__backend
def find_subclasses(locations, base_cls, exclude_hidden=True): """Finds subclass types in the given locations. This will examines the given locations for types which are subclasses of the base class type provided and returns the found subclasses (or fails with exceptions if this introspection can not be accomplished). If a string is provided as one of the locations it will be imported and examined if it is a subclass of the base class. If a module is given, all of its members will be examined for attributes which are subclasses of the base class. If a type itself is given it will be examined for being a subclass of the base class. """ derived = set() for item in locations: module = None if isinstance(item, six.string_types): try: pkg, cls = item.split(':') except ValueError: module = importutils.import_module(item) else: obj = importutils.import_class('%s.%s' % (pkg, cls)) if not is_subclass(obj, base_cls): raise TypeError("Item %s is not a %s subclass" % (item, base_cls)) derived.add(obj) elif isinstance(item, types.ModuleType): module = item elif is_subclass(item, base_cls): derived.add(item) else: raise TypeError("Item %s unexpected type: %s" % (item, type(item))) # If it's a module derive objects from it if we can. if module is not None: for (_name, obj) in _get_members(module, exclude_hidden): if is_subclass(obj, base_cls): derived.add(obj) return derived
def __init__(self, db_driver=None): if not db_driver: db_driver = CONF.db_driver self.db = importutils.import_module(db_driver) # pylint: disable=C0103