diff options
| author | Xin LI <delphij@FreeBSD.org> | 2016-04-18 04:43:03 +0000 | 
|---|---|---|
| committer | Xin LI <delphij@FreeBSD.org> | 2016-04-18 04:43:03 +0000 | 
| commit | 196a380938cafa8bcc6ffcfd739c093a8673c712 (patch) | |
| tree | 8777b975692116ac553664f46dd5e5c190ba5332 /python | |
| parent | 1e0fd9ee49c0f1156dc8531655f1613adeb134c0 (diff) | |
Notes
Diffstat (limited to 'python')
| -rw-r--r-- | python/README | 13 | ||||
| -rw-r--r-- | python/magic.py | 90 | ||||
| -rw-r--r-- | python/setup.py | 32 | 
3 files changed, 95 insertions, 40 deletions
| diff --git a/python/README b/python/README index 8b9a2a7e4074..e69de29bb2d1 100644 --- a/python/README +++ b/python/README @@ -1,13 +0,0 @@ -This directory contains Python bindings to allow you to access the -libmagic api. At the moment their status is "experimental". - -You can install the modules either with: - -$ python setup.py build -$ python setup.py install - -or, if you have easy_install: - -$ easy_install . - -magic-python should work now! diff --git a/python/magic.py b/python/magic.py index a17e8dad169a..c48f7d5a3012 100644 --- a/python/magic.py +++ b/python/magic.py @@ -1,10 +1,13 @@ -#!/usr/bin/env python +# coding: utf-8 +  '''  Python bindings for libmagic  '''  import ctypes +from collections import namedtuple +  from ctypes import *  from ctypes.util import find_library @@ -32,7 +35,7 @@ MAGIC_PRESERVE_ATIME = PRESERVE_ATIME = 128  MAGIC_RAW = RAW = 256  MAGIC_ERROR = ERROR = 512  MAGIC_MIME_ENCODING = MIME_ENCODING = 1024 -MAGIC_MIME = MIME = 1040 +MAGIC_MIME = MIME = 1040  # MIME_TYPE + MIME_ENCODING  MAGIC_APPLE = APPLE = 2048  MAGIC_NO_CHECK_COMPRESS = NO_CHECK_COMPRESS = 4096 @@ -47,6 +50,8 @@ MAGIC_NO_CHECK_ENCODING = NO_CHECK_ENCODING = 2097152  MAGIC_NO_CHECK_BUILTIN = NO_CHECK_BUILTIN = 4173824 +FileMagic = namedtuple('FileMagic', ('mime_type', 'encoding', 'name')) +  class magic_set(Structure):      pass @@ -118,14 +123,18 @@ class Magic(object):          as a filename or None if an error occurred and the MAGIC_ERROR flag          is set.  A call to errno() will return the numeric error code.          """ -        try:  # attempt python3 approach first -            if isinstance(filename, bytes): -                bi = filename -            else: +        if isinstance(filename, bytes): +            bi = filename +        else: +            try:  # keep Python 2 compatibility                  bi = bytes(filename, 'utf-8') -            return str(_file(self._magic_t, bi), 'utf-8') -        except: -            return _file(self._magic_t, filename.encode('utf-8')) +            except TypeError: +                bi = bytes(filename) +        r = _file(self._magic_t, bi) +        if isinstance(r, str): +            return r +        else: +            return str(r).encode('utf-8')      def descriptor(self, fd):          """ @@ -139,20 +148,22 @@ class Magic(object):          as a buffer or None if an error occurred and the MAGIC_ERROR flag          is set. A call to errno() will return the numeric error code.          """ -        try:  # attempt python3 approach first -            return str(_buffer(self._magic_t, buf, len(buf)), 'utf-8') -        except: -            return _buffer(self._magic_t, buf, len(buf)) +        r = _buffer(self._magic_t, buf, len(buf)) +        if isinstance(r, str): +            return r +        else: +            return str(r).encode('utf-8')      def error(self):          """          Returns a textual explanation of the last error or None          if there was no error.          """ -        try:  # attempt python3 approach first -            return str(_error(self._magic_t), 'utf-8') -        except: -            return _error(self._magic_t) +        e = _error(self._magic_t) +        if isinstance(e, str): +            return e +        else: +            return str(e).encode('utf-8')      def setflags(self, flags):          """ @@ -219,3 +230,48 @@ def open(flags):      Flags argument as for setflags.      """      return Magic(_open(flags)) + + +# Objects used by `detect_from_` functions +mime_magic = Magic(_open(MAGIC_MIME)) +mime_magic.load() +none_magic = Magic(_open(MAGIC_NONE)) +none_magic.load() + + +def _create_filemagic(mime_detected, type_detected): +    mime_type, mime_encoding = mime_detected.split('; ') + +    return FileMagic(name=type_detected, mime_type=mime_type, +                     encoding=mime_encoding.replace('charset=', '')) + + +def detect_from_filename(filename): +    '''Detect mime type, encoding and file type from a filename + +    Returns a `FileMagic` namedtuple. +    ''' + +    return _create_filemagic(mime_magic.file(filename), +                             none_magic.file(filename)) + + +def detect_from_fobj(fobj): +    '''Detect mime type, encoding and file type from file-like object + +    Returns a `FileMagic` namedtuple. +    ''' + +    file_descriptor = fobj.fileno() +    return _create_filemagic(mime_magic.descriptor(file_descriptor), +                             none_magic.descriptor(file_descriptor)) + + +def detect_from_content(byte_content): +    '''Detect mime type, encoding and file type from bytes + +    Returns a `FileMagic` namedtuple. +    ''' + +    return _create_filemagic(mime_magic.buffer(byte_content), +                             none_magic.buffer(byte_content)) diff --git a/python/setup.py b/python/setup.py index 2c3b527bb890..24ae182fff6b 100644 --- a/python/setup.py +++ b/python/setup.py @@ -1,10 +1,22 @@ -# Python distutils build script for magic extension -from distutils.core import setup - -setup(name = 'Magic file extensions', -    version = '0.2', -    author = 'Reuben Thomas', -    author_email = 'rrt@sc3d.org', -    license = 'BSD', -    description = 'libmagic Python bindings', -    py_modules = ['magic']) +# coding: utf-8 + +from __future__ import unicode_literals + +from setuptools import setup + + +setup(name='file-magic', +      version='0.3.0', +      author='Reuben Thomas, Álvaro Justen', +      author_email='rrt@sc3d.org, alvarojusten@gmail.com', +      url='https://github.com/file/file', +      license='BSD', +      description='(official) libmagic Python bindings', +      py_modules=['magic'], +      test_suite='tests', +      classifiers = [ +          'Intended Audience :: Developers', +          'License :: OSI Approved :: BSD License', +          'Natural Language :: English', +          'Topic :: Software Development :: Libraries :: Python Modules', +      ]) | 
