Python bindings for pure-magic crate, a pure Rust re-implementation of libmagic
Actively maintained. Last release 13 days ago, commits in 11 of the last 26 weeks. A safe choice.
$
pip install pure-magic-rsREADME as published on PyPI with version 0.6.2.
Pure Rust Python Bindings for File Type Detection
pure-magic-rs is a Python module that provides bindings to the pure-magic crate, allowing you to detect file types, MIME types, and other metadata using a pure Rust alternative to the C libmagic library. This module uses an embedded magic database, so everything works out-of-the-box with no need of a compiled database or magic rule files.
Features
- Pure Rust implementation: No C dependencies, easily cross-compilable for great compatibility
- Embedded magic database: No database file or magic rules needed
- Detect file types from buffers or files
- Retrieve MIME types, creator codes, and file extensions
- Convert results to Python dictionaries for easy integration
- Supports both "first match", "best match" and "all matches" detection strategies
Installation
pip install pure-magic-rs
Usage
Initializing the Database
from pure_magic_rs import MagicDb
# Initialize the Magic database (uses embedded database)
db = MagicDb()
Detecting File Types from Buffers
# Detect the first match for a buffer (e.g., PNG data)
png_data = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00'
result = db.first_magic_buffer(png_data, None)
print(f"Detected: {result.message} (MIME: {result.mime_type})")
# Detect the best match for a buffer
result = db.best_magic_buffer(png_data, None)
print(f"Best match: {result.message}")