cmagic
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
export CFLAGS="-I$(brew --prefix libmagic)/include" LDFLAGS="-L$(brew --prefix libmagic)/lib"
python3 -m pip install cmagic
Metadata
Release files for cmagic 1.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cmagic-1.0.3.tar.gz | 12.5 kB | Without provenance | Details for cmagic-1.0.3.tar.gz |
Release files / cmagic-1.0.3.tar.gz
| Download URL | cmagic-1.0.3.tar.gz |
|---|---|
| Size | 12.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
acd50451ffcba1b484defb9d6041ac11c3c4c002e7ec82d3d179446186393f5b
|
|
BLAKE2b-256 checksum How to use checksums |
62a57efff6bc4c6832adbfea118c059d3069bcf2e27741eef49635d70ba090d3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.9.6 readme-renderer/32.0 requests/2.28.1 requests-toolbelt/0.9.1 urllib3/1.26.12 tqdm/4.62.3 importlib-metadata/5.2.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
|
Security for cmagic 1.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.