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
Built Distributions
File details
Details for the file cmagic-1.0.0.tar.gz
.
File metadata
- Download URL: cmagic-1.0.0.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e9f6230d5f304aaed3d1835e43a60d8a64d9fe5009ac1f9278769ec97a9599df |
|
MD5 | b4ea46bf60bdb739bd0f65b684be5ff9 |
|
BLAKE2b-256 | 5a81efa8137035bbd7feeaa25e4320cb2f5d0760d71d92b7dc7c8fe2cefa43ac |
Provenance
File details
Details for the file cmagic-1.0.0-cp38-cp38-manylinux2014_x86_64.whl
.
File metadata
- Download URL: cmagic-1.0.0-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.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0758f53235a7955862d96beca07f08f34c40ce65910c06ceab109f880689fdfe |
|
MD5 | 0f40b9cfe6b33800ae0d41214810475d |
|
BLAKE2b-256 | f80a671df42bf6af12bf49ec5ef0c5c8b20d0ff8d4cfe83dd890f13be1f79c3e |
Provenance
File details
Details for the file cmagic-1.0.0-cp38-cp38-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: cmagic-1.0.0-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.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72a910a3f863aeed8227228494d4d91494eba9f9b9176d07ebbd5d347b3f8c67 |
|
MD5 | cea08abaa3c1038d2d64f698dc23fb79 |
|
BLAKE2b-256 | 2d1fd3b4d4a069f5d6059aacdaa4c73f9d2d7c80ed75ccbc1498e75683309960 |
Provenance
File details
Details for the file cmagic-1.0.0-cp37-cp37m-manylinux2014_x86_64.whl
.
File metadata
- Download URL: cmagic-1.0.0-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.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9bce2857570dad6b0a1d65e9c2de6eb4655e1a1ad6d60e0fa1399ec8595401f |
|
MD5 | f830b391318a684e9db8ee2fc4636305 |
|
BLAKE2b-256 | bdfcb41c11a37b216f2fc8aab58fa82264caf50196b9e4df34c7fb5ca0a3eb5f |
Provenance
File details
Details for the file cmagic-1.0.0-cp37-cp37m-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: cmagic-1.0.0-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.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8236a577996f52137c85d9e7219ed389e8481aabf83633b93a22e622edfbd9d |
|
MD5 | 1c49b85b1b73991adf466a157be1364b |
|
BLAKE2b-256 | 46196e55266429620fb81a1749b052dc1cc727b08923ceb4ca96d4659fadbacf |
Provenance
File details
Details for the file cmagic-1.0.0-cp36-cp36m-manylinux2014_x86_64.whl
.
File metadata
- Download URL: cmagic-1.0.0-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.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 941dded7a3fe50dcfb8e8d68da5101aa5eb082085362d58fab4196a6809c8ade |
|
MD5 | b11240e37eb6e645a4c8a7a07302bde2 |
|
BLAKE2b-256 | 2080cf913b138bc5fb23bc5e17a40738a86ae0c718799e913d4d25f746e2827a |
Provenance
File details
Details for the file cmagic-1.0.0-cp36-cp36m-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: cmagic-1.0.0-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.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55fa79463bc53e096ef7d53ab8cf7b98045a0ce1cf914677b780320f5589d559 |
|
MD5 | 429f4422fff677b29c47b215a18632f4 |
|
BLAKE2b-256 | 52762b798ceb6f7285a7c7eb6276fdea5beaab2dc6391483610d23136e38a8cc |
Provenance
File details
Details for the file cmagic-1.0.0-cp35-cp35m-manylinux2014_x86_64.whl
.
File metadata
- Download URL: cmagic-1.0.0-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.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e09f3de135a7cc1e8fb773a1a2fc70e9772ca6125c798fab203ff962ca0c3e6 |
|
MD5 | c5f39a666227f6f74a0e82c416b56d81 |
|
BLAKE2b-256 | 80b25215952186a3fbcd83f57201f976fd58277d8d9515022e5db1dc2c3282bb |
Provenance
File details
Details for the file cmagic-1.0.0-cp35-cp35m-macosx_10_6_intel.whl
.
File metadata
- Download URL: cmagic-1.0.0-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.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff5a29414759f212c8e734a90730f3903710f411da9c45bb8d5260246b736812 |
|
MD5 | 84286c70837652c5200aa74f6630bf41 |
|
BLAKE2b-256 | 8e2409dccb6aa65ba7e2f6d6d4b4fe7cda99e8c04643bea37865c5bd3e7a56c5 |