Package archipelagentvirtualmachinestorage :: Module magic
[hide private]
[frames] | no frames]

Module magic

source code

magic is a wrapper around the libmagic file identification library.

See README for more information.

Usage:

>>> import magic
>>> magic.from_file("testdata/test.pdf")
'PDF document, version 1.2'
>>> magic.from_file("testdata/test.pdf", mime=True)
'application/pdf'
>>> magic.from_buffer(open("testdata/test.pdf").read(1024))
'PDF document, version 1.2'
>>>
Classes [hide private]
  MagicException
  Magic
Magic is a wrapper around the libmagic C library.
Functions [hide private]
 
_get_magic_type(mime) source code
 
from_file(filename, mime=False)
" Accepts a filename and returns the detected filetype.
source code
 
from_buffer(buffer, mime=False)
Accepts a binary string and returns the detected filetype.
source code
 
errorcheck_null(result, func, args) source code
 
errorcheck_negative_one(result, func, args) source code
 
coerce_filename(filename) source code
 
magic_file(cookie, filename) source code
 
magic_buffer(cookie, buf) source code
 
magic_load(cookie, filename) source code
Variables [hide private]
  instances = threading.local()
  dll = ctypes.util.find_library('magic') or ctypes.util.find_li...
  libmagic = ctypes.CDLL(dll)
  platform_to_lib = {'darwin': ['/opt/local/lib/libmagic.dylib',...
  magic_t = ctypes.c_void_p
  magic_open = libmagic.magic_open
  magic_close = libmagic.magic_close
  magic_error = libmagic.magic_error
  magic_errno = libmagic.magic_errno
  _magic_file = libmagic.magic_file
  _magic_buffer = libmagic.magic_buffer
  _magic_load = libmagic.magic_load
  magic_setflags = libmagic.magic_setflags
  magic_check = libmagic.magic_check
  magic_compile = libmagic.magic_compile
  MAGIC_NONE = 0x000000
  MAGIC_DEBUG = 0x000001
  MAGIC_SYMLINK = 0x000002
  MAGIC_COMPRESS = 0x000004
  MAGIC_DEVICES = 0x000008
  MAGIC_MIME = 0x000010
  MAGIC_MIME_ENCODING = 0x000400
  MAGIC_CONTINUE = 0x000020
  MAGIC_CHECK = 0x000040
  MAGIC_PRESERVE_ATIME = 0x000080
  MAGIC_RAW = 0x000100
  MAGIC_ERROR = 0x000200
  MAGIC_NO_CHECK_COMPRESS = 0x001000
  MAGIC_NO_CHECK_TAR = 0x002000
  MAGIC_NO_CHECK_SOFT = 0x004000
  MAGIC_NO_CHECK_APPTYPE = 0x008000
  MAGIC_NO_CHECK_ELF = 0x010000
  MAGIC_NO_CHECK_ASCII = 0x020000
  MAGIC_NO_CHECK_TROFF = 0x040000
  MAGIC_NO_CHECK_FORTRAN = 0x080000
  MAGIC_NO_CHECK_TOKENS = 0x100000
Function Details [hide private]

from_file(filename, mime=False)

source code 

" Accepts a filename and returns the detected filetype. Return value is the mimetype if mime=True, otherwise a human readable name.

>>> magic.from_file("testdata/test.pdf", mime=True)
'application/pdf'

from_buffer(buffer, mime=False)

source code 

Accepts a binary string and returns the detected filetype. Return value is the mimetype if mime=True, otherwise a human readable name.

>>> magic.from_buffer(open("testdata/test.pdf").read(1024))
'PDF document, version 1.2'

Variables Details [hide private]

dll

Value:
ctypes.util.find_library('magic') or ctypes.util.find_library('magic1'\
)

platform_to_lib

Value:
{'darwin': ['/opt/local/lib/libmagic.dylib', '/usr/local/lib/libmagic.\
dylib']+ glob.glob('/usr/local/Cellar/libmagic/*/lib/libmagic.dylib'),\
 'win32': ['magic1.dll']}