Beispiel #1
0
def backend_from_fname(name):
    """Determine backend module object from a file name."""
    ext = splitext(name)[1]

    try:
        mime = EXTS_TO_MIMETYPES[ext]

    except KeyError:
        try:
            f = open(name, 'rb')

        except IOError as e:
            # The file may not exist, we are being asked to determine it's type
            # from it's name. Other errors are unexpected.
            if e.errno != errno.ENOENT:
                raise

            # We will have to fall back upon the default backend.
            msg = "No handler for %r, defaulting to %r" % (ext, DEFAULT_MIME)
            if 'FULLTEXT_TESTING' in os.environ:
                warn(msg)
            else:
                LOGGER.debug(msg)

            mod_name = MIMETYPE_TO_BACKENDS[DEFAULT_MIME]

        else:
            with f:
                return backend_from_fobj(f)

    else:
        mod_name = MIMETYPE_TO_BACKENDS[mime]

    mod = import_mod(mod_name)
    return mod
Beispiel #2
0
def register_backend(mimetype, module, extensions=None):
    """Register a backend.
    `mimetype`: a mimetype string (e.g. 'text/plain')
    `module`: an import string (e.g. path.to.my.module)
    `extensions`: a list of extensions (e.g. ['txt', 'text'])
    """
    if mimetype in MIMETYPE_TO_BACKENDS:
        warn("overwriting %r mimetype which was already set" % mimetype)
    MIMETYPE_TO_BACKENDS[mimetype] = module
    if extensions is None:
        try:
            ext = _MIMETYPES_TO_EXT[mimetype]
        except KeyError:
            raise KeyError("mimetypes module has no extension associated "
                           "with %r mimetype; use 'extensions' arg yourself" %
                           mimetype)
        assert ext, ext
        EXTS_TO_MIMETYPES[ext] = mimetype
    else:
        if not isinstance(extensions, (list, tuple, set, frozenset)):
            raise TypeError("invalid extensions type (got %r)" % extensions)
        for ext in set(extensions):
            ext = ext if ext.startswith('.') else '.' + ext
            assert ext, ext
            EXTS_TO_MIMETYPES[ext] = mimetype
Beispiel #3
0
def backend_from_fname(name):
    """Determine backend module object from a file name."""
    ext = splitext(name)[1]

    try:
        mime = EXTS_TO_MIMETYPES[ext]

    except KeyError:
        try:
            f = open(name, 'rb')

        except IOError as e:
            # The file may not exist, we are being asked to determine it's type
            # from it's name. Other errors are unexpected.
            if e.errno != errno.ENOENT:
                raise

            # We will have to fall back upon the default backend.
            msg = "No handler for %r, defaulting to %r" % (ext, DEFAULT_MIME)
            if 'FULLTEXT_TESTING' in os.environ:
                warn(msg)
            else:
                LOGGER.debug(msg)

            mod_name = MIMETYPE_TO_BACKENDS[DEFAULT_MIME]

        else:
            with f:
                return backend_from_fobj(f)

    else:
        mod_name = MIMETYPE_TO_BACKENDS[mime]

    mod = import_mod(mod_name)
    return mod
Beispiel #4
0
def register_backend(mimetype, module, extensions=None):
    """Register a backend.
    `mimetype`: a mimetype string (e.g. 'text/plain')
    `module`: an import string (e.g. path.to.my.module)
    `extensions`: a list of extensions (e.g. ['txt', 'text'])
    """
    if mimetype in MIMETYPE_TO_BACKENDS:
        warn("overwriting %r mimetype which was already set" % mimetype)
    MIMETYPE_TO_BACKENDS[mimetype] = module
    if extensions is None:
        try:
            ext = _MIMETYPES_TO_EXT[mimetype]
        except KeyError:
            raise KeyError(
                "mimetypes module has no extension associated "
                "with %r mimetype; use 'extensions' arg yourself" % mimetype)
        assert ext, ext
        EXTS_TO_MIMETYPES[ext] = mimetype
    else:
        if not isinstance(extensions, (list, tuple, set, frozenset)):
            raise TypeError("invalid extensions type (got %r)" % extensions)
        for ext in set(extensions):
            ext = ext if ext.startswith('.') else '.' + ext
            assert ext, ext
            EXTS_TO_MIMETYPES[ext] = mimetype
Beispiel #5
0
def backend_from_fobj(f):
    """Determine backend module object from a file object."""
    if magic is None:
        warn("magic lib is not installed; assuming mime type %r" %
             (DEFAULT_MIME))
        return backend_from_mime(DEFAULT_MIME)
    else:
        offset = f.tell()
        try:
            f.seek(0)
            chunk = f.read(MAGIC_BUFFER_SIZE)
            mime = magic.from_buffer(chunk, mime=True)
            return backend_from_mime(mime)
        finally:
            f.seek(offset)
Beispiel #6
0
def backend_from_mime(mime):
    """Determine backend module object from a mime string."""
    try:
        mod_name = MIMETYPE_TO_BACKENDS[mime]

    except KeyError:
        msg = "No handler for %r, defaulting to %r" % (mime, DEFAULT_MIME)
        if 'FULLTEXT_TESTING' in os.environ:
            warn(msg)
        else:
            LOGGER.debug(msg)

        mod_name = MIMETYPE_TO_BACKENDS[DEFAULT_MIME]
    mod = import_mod(mod_name)
    return mod
Beispiel #7
0
def backend_from_fobj(f):
    """Determine backend module object from a file object."""
    if magic is None:
        warn("magic lib is not installed; assuming mime type %r" % (
            DEFAULT_MIME))
        return backend_from_mime(DEFAULT_MIME)
    else:
        offset = f.tell()
        try:
            f.seek(0)
            chunk = f.read(MAGIC_BUFFER_SIZE)
            mime = magic.from_buffer(chunk, mime=True)
            return backend_from_mime(mime)
        finally:
            f.seek(offset)
Beispiel #8
0
def backend_from_mime(mime):
    """Determine backend module object from a mime string."""
    try:
        mod_name = MIMETYPE_TO_BACKENDS[mime]

    except KeyError:
        msg = "No handler for %r, defaulting to %r" % (mime, DEFAULT_MIME)
        if 'FULLTEXT_TESTING' in os.environ:
            warn(msg)
        else:
            LOGGER.debug(msg)

        mod_name = MIMETYPE_TO_BACKENDS[DEFAULT_MIME]
    mod = import_mod(mod_name)
    return mod
Beispiel #9
0
def backend_inst_from_mod(mod, encoding, encoding_errors, kwargs):
    """Given a mod and a set of opts return an instantiated
    Backend class.
    """
    kw = dict(encoding=encoding, encoding_errors=encoding_errors,
              kwargs=kwargs)
    try:
        klass = getattr(mod, "Backend")
    except AttributeError:
        raise AttributeError("%r mod does not define any backend class" % mod)
    inst = klass(**kw)
    try:
        inst.check(title=False)
    except Exception as err:
        bin_mod = "fulltext.backends.__bin"
        warn("can't use %r due to %r; use %r backend instead" % (
             mod, str(err), bin_mod))
        inst = import_mod(bin_mod).Backend(**kw)
        inst.check(title=False)
    LOGGER.debug("using %r" % inst)
    return inst
Beispiel #10
0
def backend_inst_from_mod(mod, encoding, encoding_errors, kwargs):
    """Given a mod and a set of opts return an instantiated
    Backend class.
    """
    kw = dict(encoding=encoding,
              encoding_errors=encoding_errors,
              kwargs=kwargs)
    try:
        klass = getattr(mod, "Backend")
    except AttributeError:
        raise AttributeError("%r mod does not define any backend class" % mod)
    inst = klass(**kw)
    try:
        inst.check(title=False)
    except Exception as err:
        bin_mod = "fulltext.backends.__bin"
        warn("can't use %r due to %r; use %r backend instead" %
             (mod, str(err), bin_mod))
        inst = import_mod(bin_mod).Backend(**kw)
        inst.check(title=False)
    LOGGER.debug("using %r" % inst)
    return inst