Skip to main content

Python wrapper for libmagic

Project description

https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg Github Actions Latest Version https://img.shields.io/pypi/wheel/cmagic.svg https://img.shields.io/pypi/pyversions/cmagic.svg https://img.shields.io/pypi/l/cmagic.svg

Python wrapper for libmagic.

Usage

# MacOS
$ export MAGIC=/usr/local/Cellar/libmagic/5.39/share/misc/magic.mgc

# Ubuntu
$ export MAGIC=/usr/lib/file/magic.mgc
import cmagic

# Database path from MAGIC environment variable
m = cmagic.Magic()

if m.check():
   print("Database is ok")

m.load()

m.guess_file("/etc/hosts")
# 'ASCII text'
m.guess_bytes("hello world")
# 'ASCII text'

# Setting flags
m.set_flags(mime_type=True)


m = cmagic.Magic(
   # Setting flags here
   mime_type=True
)

# Trying to find database on the standard paths
m.load(cmagic.find_db())

m.guess_file("/etc/hosts")
# 'text/plain'

m.guess_bytes("hello world")
# 'text/plain'

asyncio example

import asyncio
import cmagic
from threading import local


magic_tls = local()


def get_instance():
    global magic_tls

    if not hasattr(magic_tls, "instance"):
        m = cmagic.Magic(mime_type=True)
        m.load(cmagic.find_db())
        magic_tls.instance = m

    return magic_tls.instance


async def guess_file(fname):
    loop = asyncio.get_event_loop()

    def run():
        m = get_instance()
        return m.guess_file(fname)

    return await loop.run_in_executor(None, run)


async def guess_bytes(payload):
    loop = asyncio.get_event_loop()

    def run():
        m = get_instance()
        return m.guess_bytes(payload)

    return await loop.run_in_executor(None, run)


if __name__ == "__main__":
    print(asyncio.run(guess_file("/etc/hosts")))
    # text/plain
    print(asyncio.run(guess_bytes(b"\0\0\0\0\0\0\0")))
    # application/octet-stream

Installation

Ubuntu/Debian

apt-get install -y libmagic1 libmagic-mgc   # when using manilinux wheel
apt-get install -y libmagic-dev             # for building from sources
python3 -m pip install cmagic

Centos

yum install -y file-libs            # when using manilinux wheel
yum install -y file-devel           # for building from sources
python3 -m pip install cmagic

MacOS

brew install libmagic
python3 -m pip install cmagic

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

cmagic-0.4.2.tar.gz (8.8 kB view details)

Uploaded Source

Built Distributions

cmagic-0.4.2-cp38-cp38-manylinux2014_x86_64.whl (138.4 kB view details)

Uploaded CPython 3.8

cmagic-0.4.2-cp38-cp38-macosx_10_9_x86_64.whl (362.8 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

cmagic-0.4.2-cp37-cp37m-manylinux2014_x86_64.whl (139.0 kB view details)

Uploaded CPython 3.7m

cmagic-0.4.2-cp37-cp37m-macosx_10_9_x86_64.whl (362.7 kB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

cmagic-0.4.2-cp36-cp36m-manylinux2014_x86_64.whl (138.0 kB view details)

Uploaded CPython 3.6m

cmagic-0.4.2-cp36-cp36m-macosx_10_9_x86_64.whl (362.7 kB view details)

Uploaded CPython 3.6m macOS 10.9+ x86-64

cmagic-0.4.2-cp35-cp35m-manylinux2014_x86_64.whl (137.8 kB view details)

Uploaded CPython 3.5m

cmagic-0.4.2-cp35-cp35m-macosx_10_6_intel.whl (365.4 kB view details)

Uploaded CPython 3.5m macOS 10.6+ intel

File details

Details for the file cmagic-0.4.2.tar.gz.

File metadata

  • Download URL: cmagic-0.4.2.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for cmagic-0.4.2.tar.gz
Algorithm Hash digest
SHA256 dc2a5bd7969ddc6660ca58e0347fd7382b7b5a77e12560e8ae898ff2f50986dd
MD5 48f1d136813ba4858de6a63cbb953538
BLAKE2b-256 ac10c82b01d888fe9bdc5b098aabd955e634231e576182264584f9af63f8e0f9

See more details on using hashes here.

File details

Details for the file cmagic-0.4.2-cp38-cp38-manylinux2014_x86_64.whl.

File metadata

  • Download URL: cmagic-0.4.2-cp38-cp38-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 138.4 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for cmagic-0.4.2-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bad4131cb2fbada3394387ffb69aa0af55e48a2bc57ebca5e0a8f53ff8b7a0af
MD5 26ed672125c8dec4f3e269d858c3f6ce
BLAKE2b-256 543ba5d04c04d0bcef2065ac8cab6ee3655652e60c2820280d5658bb3389eae7

See more details on using hashes here.

File details

Details for the file cmagic-0.4.2-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: cmagic-0.4.2-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 362.8 kB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for cmagic-0.4.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7b92b26a0fc1987f45bbe21231302c52788ee36eaedb03654edb7bfb7d2eacae
MD5 6f37fbdde749cf19772750d09e2146fa
BLAKE2b-256 4b59f6ffe9ca07890f30be0815b87b839661e97928a906a0f1ee24da2d276c3a

See more details on using hashes here.

File details

Details for the file cmagic-0.4.2-cp37-cp37m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: cmagic-0.4.2-cp37-cp37m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 139.0 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for cmagic-0.4.2-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 493b176836dfc38fc840844b4f88bedd58fdbf20c342619ca59a1bb566147bf5
MD5 6d16014b4e4c2355994649ad30e96689
BLAKE2b-256 1df837c573998de0c01b4d3cf96bccb02596f0aeb9f42d708fa3b705217d19e8

See more details on using hashes here.

File details

Details for the file cmagic-0.4.2-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: cmagic-0.4.2-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 362.7 kB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for cmagic-0.4.2-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 55d37e1c947fffe0bb1de293a37a18868993b4a901cfeb8bcd645da9e7150e2f
MD5 025044fffdd0144be39f865ed84255af
BLAKE2b-256 9bf9f5591b9d0dbf8dbb2d31e1576217269b57520bcc877c5eeb005382a30c5e

See more details on using hashes here.

File details

Details for the file cmagic-0.4.2-cp36-cp36m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: cmagic-0.4.2-cp36-cp36m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 138.0 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for cmagic-0.4.2-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0284d3bd27bcab605d9ad24b92112b3d7d32320326ac216108a39c003c2b39c5
MD5 ab146844954f4e167f8ed7545f1c3dce
BLAKE2b-256 32fd92a0a29294bd9d620e3095c56b935a46bd8aaa3e5ff9ff00fb43af255d1a

See more details on using hashes here.

File details

Details for the file cmagic-0.4.2-cp36-cp36m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: cmagic-0.4.2-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 362.7 kB
  • Tags: CPython 3.6m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for cmagic-0.4.2-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 51836eabc0244e20c504a3a8b96fe9aaa8b39c39d9ab714f5e99ab10427295c1
MD5 fda63e655f0e8a905d5ce1df9f1f7229
BLAKE2b-256 5be79e87186d44494d0dd65bf152485eb1f673c1603df9a5d111a075dd8cf6ff

See more details on using hashes here.

File details

Details for the file cmagic-0.4.2-cp35-cp35m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: cmagic-0.4.2-cp35-cp35m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 137.8 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for cmagic-0.4.2-cp35-cp35m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 073eee8efd9f75f4e6f0411be4546deb8abd85f8cbe9943df3dd232150c95e88
MD5 bcbda8d74b29e91f0f20d164031c45aa
BLAKE2b-256 5fc12a4991b5ee3d8ff410ee7de3f774274ff14420256f9eea6e28a2d71accc3

See more details on using hashes here.

File details

Details for the file cmagic-0.4.2-cp35-cp35m-macosx_10_6_intel.whl.

File metadata

  • Download URL: cmagic-0.4.2-cp35-cp35m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 365.4 kB
  • Tags: CPython 3.5m, macOS 10.6+ intel
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for cmagic-0.4.2-cp35-cp35m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 6eb2bc32dfbe5b1278ec9ece48ff7f375896c49e61932dcb1eec9d50980af420
MD5 52768f6a817655c4325b1623edd8d791
BLAKE2b-256 262f309f54db3d6f37670ef941a00187bc317dad1bf7a147fb5303c822061237

See more details on using hashes here.

Supported by

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