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.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distributions

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

Uploaded CPython 3.8

cmagic-0.4.0-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.0-cp37-cp37m-manylinux2014_x86_64.whl (139.0 kB view details)

Uploaded CPython 3.7m

cmagic-0.4.0-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.0-cp36-cp36m-manylinux2014_x86_64.whl (138.0 kB view details)

Uploaded CPython 3.6m

cmagic-0.4.0-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.0-cp35-cp35m-manylinux2014_x86_64.whl (137.8 kB view details)

Uploaded CPython 3.5m

cmagic-0.4.0-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.0.tar.gz.

File metadata

  • Download URL: cmagic-0.4.0.tar.gz
  • Upload date:
  • Size: 8.8 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.45.0 CPython/3.8.3

File hashes

Hashes for cmagic-0.4.0.tar.gz
Algorithm Hash digest
SHA256 32b26cf1ef19c5ef3abdfbc23049e4ab64e82605c0ad2e9beb7a0170da4e7266
MD5 d8656b2296bb513c3cc595d68336cdf2
BLAKE2b-256 ad1ab247f85272cb84537eefc7b8c6108e474b0179f7cecd752e74a4953c4ae7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cmagic-0.4.0-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.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.3

File hashes

Hashes for cmagic-0.4.0-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ad71dc93a6bbe8131db789d1a765e280071a3854d8bf223af9c474133b4c35f5
MD5 168c2556869c131d648805cc7a04e8c6
BLAKE2b-256 dc43045ca67d3761019188240aa45eafc88ca966d7673361ef5c19ed353e3085

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cmagic-0.4.0-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.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.3

File hashes

Hashes for cmagic-0.4.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 55307da00a95a2044b61db0fb2b2e35a337e9d3cae5456f346edddd030179a79
MD5 73e89e47e53e873fc592b4892d688417
BLAKE2b-256 92ef2a96d7802b0c1c680de92e51c05f140ff3cdd6cb3c863a03dcb10b0d9d0e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cmagic-0.4.0-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.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.3

File hashes

Hashes for cmagic-0.4.0-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 713d4bf75e30682bdd47203e930a6b1dacbdcbf4c997f0b2b3657784f753f52d
MD5 e4026a8147b92e8beb5160bf2e9c5dde
BLAKE2b-256 f8aecc67aee9f673eb3b09703def951773463523ccb0f66f1be9a9bdd5f22208

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cmagic-0.4.0-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.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.3

File hashes

Hashes for cmagic-0.4.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 91ec5972af284764804d9510033cbe46041e5e227f9cb038ff079f39fe4f1d3f
MD5 fb4fc729a45b4e2283a0ef9c63716546
BLAKE2b-256 996ed24e55aa5fb352ace08b1ed08dcea86f5826a86c78231189474221141289

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cmagic-0.4.0-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.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.3

File hashes

Hashes for cmagic-0.4.0-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f82008d16ee87086876caee387e038c50b089086ac7f639758569719a3c81746
MD5 e0d7f9a100d7043d1796e7495f957342
BLAKE2b-256 6d6e7f36168c8d7ee9996967192811920bb24d1c9ed43b9ca9833265f2c15408

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cmagic-0.4.0-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.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.3

File hashes

Hashes for cmagic-0.4.0-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c81f173c20073389b0207ef820368271c7dc44c48d6237fba6076874746a41cf
MD5 63c8f90623645d4ed74fef08421e9280
BLAKE2b-256 74a99afdca498c85cdc1517f05db5523be5ead21de3154d616d80016e9102fa4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cmagic-0.4.0-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.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.3

File hashes

Hashes for cmagic-0.4.0-cp35-cp35m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 470609f139760ac00009c3588163fba4661a02a857378e0ffc912a554264f305
MD5 56fa458350beff46a0053d986a447c34
BLAKE2b-256 617c7ae2c4107f395e4621d49c31f3f44542adfd73409bd0244c97f7a28373cb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cmagic-0.4.0-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.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.3

File hashes

Hashes for cmagic-0.4.0-cp35-cp35m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 09c500f092b33fe0f1f490c8fc310590b044a06b36a0f6a6266775a655bc315e
MD5 c56272344733b1f1ba9317c9e1f524b2
BLAKE2b-256 96f96ec9c5dc7677e26481e64bd31a7521ade3a0f0671696de90950d3cab53f8

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