diff options
Diffstat (limited to 'contrib/file/python')
-rw-r--r-- | contrib/file/python/README | 13 | ||||
-rw-r--r-- | contrib/file/python/magic.py | 90 | ||||
-rw-r--r-- | contrib/file/python/setup.py | 32 |
3 files changed, 40 insertions, 95 deletions
diff --git a/contrib/file/python/README b/contrib/file/python/README index e69de29..8b9a2a7 100644 --- a/contrib/file/python/README +++ b/contrib/file/python/README @@ -0,0 +1,13 @@ +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/contrib/file/python/magic.py b/contrib/file/python/magic.py index c48f7d5..a17e8da 100644 --- a/contrib/file/python/magic.py +++ b/contrib/file/python/magic.py @@ -1,13 +1,10 @@ -# coding: utf-8 - +#!/usr/bin/env python ''' Python bindings for libmagic ''' import ctypes -from collections import namedtuple - from ctypes import * from ctypes.util import find_library @@ -35,7 +32,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 # MIME_TYPE + MIME_ENCODING +MAGIC_MIME = MIME = 1040 MAGIC_APPLE = APPLE = 2048 MAGIC_NO_CHECK_COMPRESS = NO_CHECK_COMPRESS = 4096 @@ -50,8 +47,6 @@ 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 @@ -123,18 +118,14 @@ 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. """ - if isinstance(filename, bytes): - bi = filename - else: - try: # keep Python 2 compatibility + try: # attempt python3 approach first + if isinstance(filename, bytes): + bi = filename + else: bi = bytes(filename, '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') + return str(_file(self._magic_t, bi), 'utf-8') + except: + return _file(self._magic_t, filename.encode('utf-8')) def descriptor(self, fd): """ @@ -148,22 +139,20 @@ 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. """ - r = _buffer(self._magic_t, buf, len(buf)) - if isinstance(r, str): - return r - else: - return str(r).encode('utf-8') + 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)) def error(self): """ Returns a textual explanation of the last error or None if there was no error. """ - e = _error(self._magic_t) - if isinstance(e, str): - return e - else: - return str(e).encode('utf-8') + try: # attempt python3 approach first + return str(_error(self._magic_t), 'utf-8') + except: + return _error(self._magic_t) def setflags(self, flags): """ @@ -230,48 +219,3 @@ 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/contrib/file/python/setup.py b/contrib/file/python/setup.py index 24ae182..2c3b527 100644 --- a/contrib/file/python/setup.py +++ b/contrib/file/python/setup.py @@ -1,22 +1,10 @@ -# 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', - ]) +# 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']) |