Python wrapper for libmagic
Project description
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
Release history Release notifications | RSS feed
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 hashes)
Built Distributions
Close
Hashes for cmagic-0.4.2-cp38-cp38-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bad4131cb2fbada3394387ffb69aa0af55e48a2bc57ebca5e0a8f53ff8b7a0af |
|
MD5 | 26ed672125c8dec4f3e269d858c3f6ce |
|
BLAKE2b-256 | 543ba5d04c04d0bcef2065ac8cab6ee3655652e60c2820280d5658bb3389eae7 |
Close
Hashes for cmagic-0.4.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b92b26a0fc1987f45bbe21231302c52788ee36eaedb03654edb7bfb7d2eacae |
|
MD5 | 6f37fbdde749cf19772750d09e2146fa |
|
BLAKE2b-256 | 4b59f6ffe9ca07890f30be0815b87b839661e97928a906a0f1ee24da2d276c3a |
Close
Hashes for cmagic-0.4.2-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 493b176836dfc38fc840844b4f88bedd58fdbf20c342619ca59a1bb566147bf5 |
|
MD5 | 6d16014b4e4c2355994649ad30e96689 |
|
BLAKE2b-256 | 1df837c573998de0c01b4d3cf96bccb02596f0aeb9f42d708fa3b705217d19e8 |
Close
Hashes for cmagic-0.4.2-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55d37e1c947fffe0bb1de293a37a18868993b4a901cfeb8bcd645da9e7150e2f |
|
MD5 | 025044fffdd0144be39f865ed84255af |
|
BLAKE2b-256 | 9bf9f5591b9d0dbf8dbb2d31e1576217269b57520bcc877c5eeb005382a30c5e |
Close
Hashes for cmagic-0.4.2-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0284d3bd27bcab605d9ad24b92112b3d7d32320326ac216108a39c003c2b39c5 |
|
MD5 | ab146844954f4e167f8ed7545f1c3dce |
|
BLAKE2b-256 | 32fd92a0a29294bd9d620e3095c56b935a46bd8aaa3e5ff9ff00fb43af255d1a |
Close
Hashes for cmagic-0.4.2-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51836eabc0244e20c504a3a8b96fe9aaa8b39c39d9ab714f5e99ab10427295c1 |
|
MD5 | fda63e655f0e8a905d5ce1df9f1f7229 |
|
BLAKE2b-256 | 5be79e87186d44494d0dd65bf152485eb1f673c1603df9a5d111a075dd8cf6ff |
Close
Hashes for cmagic-0.4.2-cp35-cp35m-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 073eee8efd9f75f4e6f0411be4546deb8abd85f8cbe9943df3dd232150c95e88 |
|
MD5 | bcbda8d74b29e91f0f20d164031c45aa |
|
BLAKE2b-256 | 5fc12a4991b5ee3d8ff410ee7de3f774274ff14420256f9eea6e28a2d71accc3 |
Close
Hashes for cmagic-0.4.2-cp35-cp35m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6eb2bc32dfbe5b1278ec9ece48ff7f375896c49e61932dcb1eec9d50980af420 |
|
MD5 | 52768f6a817655c4325b1623edd8d791 |
|
BLAKE2b-256 | 262f309f54db3d6f37670ef941a00187bc317dad1bf7a147fb5303c822061237 |