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.1.tar.gz
(8.7 kB
view hashes)
Built Distributions
Close
Hashes for cmagic-0.4.1-cp38-cp38-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 628657294694c91ff6e96f288f776852015c3ecc00f6e0557b860bb73c16eeb3 |
|
MD5 | 53083a76a1c22161463b3136f1e60f26 |
|
BLAKE2b-256 | 1eefed35d4d71dc5f92c45bb176d6f888b5b960b2afaf989730e7d35480ddd64 |
Close
Hashes for cmagic-0.4.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dee26274b50f87214931f8f5fe387bb47a9f26609acbd7f769480ecd20d4aba4 |
|
MD5 | 08755828e520886adc3bda6e8cf13002 |
|
BLAKE2b-256 | fc362cfb90e901e8af250f1a96b16f5f163ed538bad988b13a1967fe883728e8 |
Close
Hashes for cmagic-0.4.1-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 097238e32493d55953814f27265c08851ee3baa4dc30e6530d981fccee4e4590 |
|
MD5 | 470e587015ae9b2c318892c545b97fab |
|
BLAKE2b-256 | 7e1ac0955a1d5e84192d08d457a48f743f76e84083a61e581be158f3d349c552 |
Close
Hashes for cmagic-0.4.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c766e8e5afb51fa0140821e5e1d5fd828bd7819e2c2856ba6fc97dc875e42c81 |
|
MD5 | f43f75f73c0f98ab46919dbe17c4d7c9 |
|
BLAKE2b-256 | 6b0a48796b38328a121460abca3a1645f3f1b249c905e1bfcf3fddde3dbb30e9 |
Close
Hashes for cmagic-0.4.1-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 05a72bf96343270ec0f5d081e61eb7bba4b3d8d6858003483d1aa4fc2b6a0871 |
|
MD5 | 150d25271d8fd2644689a61998230105 |
|
BLAKE2b-256 | 99328d0b3b24575e96861ad2477839144e4e8ea83cb7572c4749fc4b8b85d917 |
Close
Hashes for cmagic-0.4.1-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4dab5a039610333d9092459b93c7c8c51b6111d4e8413f712d45ad37afbb38bb |
|
MD5 | d5af2603720c6efecaa501716ae9a6c4 |
|
BLAKE2b-256 | c2d1c0c94207b891b06ccedd0853144bea0ee6b939de8f47ad855b7beabd5e91 |
Close
Hashes for cmagic-0.4.1-cp35-cp35m-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 878b910420c7a2a5fce994e83771800864a3979d0cb94bda7108ce57f7d5e7b2 |
|
MD5 | d6968cecc0dbef84f8e2ffa495ca90cd |
|
BLAKE2b-256 | fd7242fc7fa37526837f81663487fa8ccbf18be95ef3ab91b9ca8d2d9c36e590 |
Close
Hashes for cmagic-0.4.1-cp35-cp35m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7efac5b65870f629acfec3a5368e86b21dea39eaa20b7a0151a6155ee9b3e9f |
|
MD5 | a8c6b04e9e972a12f0aeab19ce6bda9b |
|
BLAKE2b-256 | 81c1865be787390863a933282f683c541d106740027cf4d3a8c36f6bf63d17dc |