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

Uploaded Source

File details

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

File metadata

  • Download URL: cmagic-1.0.1.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-1.0.1.tar.gz
Algorithm Hash digest
SHA256 bf5920ba0e4e2ed500de08c66de04f4010e993f6d89c8cf0ddf71f10958be1f4
MD5 7b31972d47976c1c9bd2fa32fdb803b4
BLAKE2b-256 74807716643d5d01c20d525b6cc9ae85af17a3e051357820f5e2e6451e7c443f

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