Ejemplo n.º 1
0
def get_package(pkg_or_dotted_name):
    """Get a package denoted by the given argument.

    If the given argument is a string, we try to load the module
    denoting that module and return it.

    Otherwise, the argument is believed to be a package and is
    returned as-is.
    """
    pkg = pkg_or_dotted_name
    if isinstance(pkg, string_types):
        pkg = resolve(pkg)
    elif isinstance(pkg, bytes):
        pkg = resolve(pkg.decode('utf-8'))
    return pkg
Ejemplo n.º 2
0
def _default(mro, get_default):
    """Apply default rule to list of classes in mro.
    """
    error = None
    for base in mro:
        module_of_base = scan.resolve(base.__module__)
        try:
            if util.is_baseclass(base):
                break
            result = get_default(base, module_of_base)
        except UnknownError, e:
            # store error if this is the first UnknownError we ran into
            if error is None:
                error = e
            result = UNKNOWN
        if result is not UNKNOWN:
            return result
Ejemplo n.º 3
0
def _default(mro, get_default):
    """Apply default rule to list of classes in mro.
    """
    error = None
    for base in mro:
        module_of_base = scan.resolve(base.__module__)
        try:
            if util.is_baseclass(base):
                break
            result = get_default(base, module_of_base)
        except UnknownError as e:
            # store error if this is the first UnknownError we ran into
            if error is None:
                error = e
            result = UNKNOWN
        if result is not UNKNOWN:
            return result
    # if we haven't found a result, raise the first error we had as
    # a GrokError
    if error is not None:
        raise GrokError(compat3.str(error), error.component)
    return UNKNOWN
Ejemplo n.º 4
0
def _default(mro, get_default):
    """Apply default rule to list of classes in mro.
    """
    error = None
    for base in mro:
        module_of_base = scan.resolve(base.__module__)
        try:
            if util.is_baseclass(base):
                break
            result = get_default(base, module_of_base)
        except UnknownError as e:
            # store error if this is the first UnknownError we ran into
            if error is None:
                error = e
            result = UNKNOWN
        if result is not UNKNOWN:
            return result
    # if we haven't found a result, raise the first error we had as
    # a GrokError
    if error is not None:
        raise GrokError(compat3.str(error), error.component)
    return UNKNOWN
Ejemplo n.º 5
0
    def get(self, directive, component, get_default):
        # look up class-level directive on this class or its bases
        # we don't need to loop through the __mro__ here as Python will
        # do it for us
        result = directive.store.get(directive, component, _USE_DEFAULT)
        if result is not _USE_DEFAULT:
            return result

        # we may be really dealing with an instance or a module here
        if not util.isclass(component):
            return get_default(component, component)

        # now we need to loop through the mro, potentially twice
        mro = inspect.getmro(component)
        # look up module-level directive for this class or its bases
        for base in mro:
            module_of_base = scan.resolve(base.__module__)
            result = directive.store.get(directive, module_of_base,
                                         _USE_DEFAULT)
            if result is not _USE_DEFAULT:
                return result
        # look up default rule for this class or its bases
        return _default(mro, get_default)