Skip to main content

Pure python implementation of magic file detection

Project description

puremagic is a pure python module that will identify a file based off it’s magic numbers.

CoverageStatus License PyPi

It is designed to be minimalistic and inherently cross platform compatible. It is also designed to be a stand in for python-magic, it incorporates the functions from_file(filename[, mime]) and from_string(string[, mime]) however the magic_file() and magic_string() are more powerful and will also display confidence and duplicate matches.

It does NOT try to match files off non-magic string. In other words it will not search for a string within a certain window of bytes like others might.

Advantages over using a wrapper for ‘file’ or ‘libmagic’:

  • Faster

  • Lightweight

  • Cross platform compatible

  • No dependencies

Disadvantages:

  • Does not have as many file types

  • No multilingual comments

  • Duplications due to small or reused magic numbers

(Help fix the first two disadvantages by contributing!)

Compatibility

  • Python 3.5+

  • Pypy

Using travis-ci to run continuous integration tests on listed platforms.

Install

In either a virtualenv or globally, simply run:

$ python setup.py install

Usage

“from_file” will return the most likely file extension. “magic_file” will give you every possible result it finds, as well as the confidence.

import puremagic

filename = "test/resources/images/test.gif"

ext = puremagic.from_file(filename)
# '.gif'

puremagic.magic_file(filename)
# [['.gif', 'image/gif', 'Graphics interchange format file (GIF87a)', 0.7],
#  ['.gif', '', 'GIF file', 0.5]]

With “magic_file” it gives each match, highest confidence first:

  • possible extension(s)

  • mime type

  • description

  • confidence (All headers have to perfectly match to make the list, however this orders it by longest header, therefore most precise, first)

Script

Usage

$ python -m puremagic [options] filename <filename2>...

Examples

$ python -m puremagic test/resources/images/test.gif
'test/resources/images/test.gif' : .gif

$ python -m puremagic -m test/resources/images/test.gif test/resources/audio/test.mp3
'test/resources/images/test.gif' : image/gif
'test/resources/audio/test.mp3' : audio/mpeg

FAQ

The file type is actually X but it’s showing up as Y with higher confidence?

This can happen when the file’s signature happens to match a subset of a file standard. The subset signature will be longer, therefore report with greater confidence, because it will have both the base file type signature plus the additional subset one.

You don’t have sliding offsets that could better detect plenty of common formats, why’s that?

Design choice, so it will be a lot faster and more accurate. Without more intelligent or deeper identification past a sliding offset I don’t feel comfortable including it as part of a ‘magic number’ library.

Your version isn’t as complete as I want it to be, where else should I look?

Look into python modules that wrap around libmagic or use something like Apache Tika.

Acknowledgements

Gary C. Kessler

For use of his File Signature Tables, available at: http://www.garykessler.net/library/file_sigs.html

Freedesktop.org

For use of their shared-mime-info file (even if they do use XML, blea), available at: https://cgit.freedesktop.org/xdg/shared-mime-info/

License

MIT Licenced, see LICENSE, Copyright (c) 2013-2020 Chris Griffith

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

puremagic-1.9.tar.gz (35.4 kB view details)

Uploaded Source

Built Distribution

puremagic-1.9-py3-none-any.whl (31.2 kB view details)

Uploaded Python 3

File details

Details for the file puremagic-1.9.tar.gz.

File metadata

  • Download URL: puremagic-1.9.tar.gz
  • Upload date:
  • Size: 35.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for puremagic-1.9.tar.gz
Algorithm Hash digest
SHA256 c11a24464b40b39fea89ccf3027854e2d64f78fa57d22f10f4c1c4c55d8e0855
MD5 58042e97efea2610ed3fd3d4d365d3d6
BLAKE2b-256 2df4be270bce48ee4cdeeddd80477338a14cfe8eba577a1ff1306e8e67345e2c

See more details on using hashes here.

File details

Details for the file puremagic-1.9-py3-none-any.whl.

File metadata

  • Download URL: puremagic-1.9-py3-none-any.whl
  • Upload date:
  • Size: 31.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for puremagic-1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 563d16453ce9ebfdd919f0dc3908ae5d681345e81fcc6e7e13d9a27a0bb9532e
MD5 fe2d3121e181614db640fd748a48f27a
BLAKE2b-256 bbf8e44acc4960629fb05f4d1318fb3b6b737a372fbdcf62496b047503a097ea

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page