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

Uploaded Source

Built Distributions

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

Uploaded CPython 3.8

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

Uploaded CPython 3.7m

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

Uploaded CPython 3.6m

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

Uploaded CPython 3.5m

cmagic-0.4.1-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.1.tar.gz.

File metadata

  • Download URL: cmagic-0.4.1.tar.gz
  • Upload date:
  • Size: 8.7 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.1.tar.gz
Algorithm Hash digest
SHA256 aa07a9e0df4870195e261ed00755bef382e42721ce1b9e26bbe9ac8f8b25fded
MD5 54ce4e53ab3514f783e83301823d074a
BLAKE2b-256 af9ccb755c6ff2c1f5b0d1a65b2b505f30edb0efdb441f459c40b4177810456b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cmagic-0.4.1-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.1-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 628657294694c91ff6e96f288f776852015c3ecc00f6e0557b860bb73c16eeb3
MD5 53083a76a1c22161463b3136f1e60f26
BLAKE2b-256 1eefed35d4d71dc5f92c45bb176d6f888b5b960b2afaf989730e7d35480ddd64

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cmagic-0.4.1-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.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 dee26274b50f87214931f8f5fe387bb47a9f26609acbd7f769480ecd20d4aba4
MD5 08755828e520886adc3bda6e8cf13002
BLAKE2b-256 fc362cfb90e901e8af250f1a96b16f5f163ed538bad988b13a1967fe883728e8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cmagic-0.4.1-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.1-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 097238e32493d55953814f27265c08851ee3baa4dc30e6530d981fccee4e4590
MD5 470e587015ae9b2c318892c545b97fab
BLAKE2b-256 7e1ac0955a1d5e84192d08d457a48f743f76e84083a61e581be158f3d349c552

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cmagic-0.4.1-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.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c766e8e5afb51fa0140821e5e1d5fd828bd7819e2c2856ba6fc97dc875e42c81
MD5 f43f75f73c0f98ab46919dbe17c4d7c9
BLAKE2b-256 6b0a48796b38328a121460abca3a1645f3f1b249c905e1bfcf3fddde3dbb30e9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cmagic-0.4.1-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.1-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 05a72bf96343270ec0f5d081e61eb7bba4b3d8d6858003483d1aa4fc2b6a0871
MD5 150d25271d8fd2644689a61998230105
BLAKE2b-256 99328d0b3b24575e96861ad2477839144e4e8ea83cb7572c4749fc4b8b85d917

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cmagic-0.4.1-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.1-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 4dab5a039610333d9092459b93c7c8c51b6111d4e8413f712d45ad37afbb38bb
MD5 d5af2603720c6efecaa501716ae9a6c4
BLAKE2b-256 c2d1c0c94207b891b06ccedd0853144bea0ee6b939de8f47ad855b7beabd5e91

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cmagic-0.4.1-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.1-cp35-cp35m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 878b910420c7a2a5fce994e83771800864a3979d0cb94bda7108ce57f7d5e7b2
MD5 d6968cecc0dbef84f8e2ffa495ca90cd
BLAKE2b-256 fd7242fc7fa37526837f81663487fa8ccbf18be95ef3ab91b9ca8d2d9c36e590

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cmagic-0.4.1-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.1-cp35-cp35m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 f7efac5b65870f629acfec3a5368e86b21dea39eaa20b7a0151a6155ee9b3e9f
MD5 a8c6b04e9e972a12f0aeab19ce6bda9b
BLAKE2b-256 81c1865be787390863a933282f683c541d106740027cf4d3a8c36f6bf63d17dc

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