Example #1
0
def import_class(import_str):
    """Returns a class from a string including module and class."""
    mod_str, _sep, class_str = import_str.rpartition('.')
    try:
        __import__(mod_str)
        return getattr(sys.modules[mod_str], class_str)
    except (ImportError, ValueError, AttributeError), exc:
        LOG.debug(_('Inner Exception: %s'), exc)
        raise exception.ClassNotFound(class_name=class_str, exception=exc)
Example #2
0
def get_filter_classes(filter_class_names):
    """Get filter classes from class names."""
    classes = []
    for cls_name in filter_class_names:
        obj = utils.import_class(cls_name)
        if _is_filter_class(obj):
            classes.append(obj)
        elif type(obj) is types.FunctionType:
            # Get list of classes from a function
            classes.extend(obj())
        else:
            raise exception.ClassNotFound(class_name=cls_name,
                    exception='Not a valid scheduler filter')
    return classes
Example #3
0
 def get_matching_classes(self, loadable_class_names):
     """Get loadable classes from a list of names.  Each name can be
     a full module path or the full path to a method that returns
     classes to use.  The latter behavior is useful to specify a method
     that returns a list of classes to use in a default case.
     """
     classes = []
     for cls_name in loadable_class_names:
         obj = importutils.import_class(cls_name)
         if self._is_correct_class(obj):
             classes.append(obj)
         elif inspect.isfunction(obj):
             # Get list of classes from a function
             for cls in obj():
                 classes.append(cls)
         else:
             error_str = 'Not a class of the correct type'
             raise exception.ClassNotFound(class_name=cls_name,
                                           exception=error_str)
     return classes