Metadata-Version: 2.4
Name: mimey
Version: 0.3.0
Classifier: Programming Language :: Rust
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Summary: A fast and efficient MIME type and file extension detector implemented in Rust, exposed as a Python package.
Keywords: MIME,file type detection,Rust,Python
Home-Page: https://github.com/4thel00z/mimey
License-Expression: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM

# mimey

## Motivation

A fast mime parser written in Rust and exposed as python package.

## Installation

```
uv add mimey
```

or

```
pip install mimey
```

## Usage


### Detect a type
```python
>>> import mimey
>>> mimey.detect_type(b"\x89PNG\r\n\x1a\n")
'.png'
```

### Detect the mimetype

```python
>>> import mimey
>>> mimey.detect_mime(b"\x89PNG\r\n\x1a\n")
'image/png'
```

## Registering your own types

Registered types are checked before the built-in table, in registration order,
so they can teach `mimey` a format it doesn't know or override a built-in verdict.

### By magic bytes

```python
import mimey

mimey.register("application/x-nes-rom", ".nes", magic=b"NES\x1a")

rom = open("game.nes", "rb").read()
mimey.detect_mime(rom)  # 'application/x-nes-rom'
```

Pass `offset` when the signature does not start at byte 0:

```python
mimey.register("application/x-offset", ".off", magic=b"HERE", offset=4)
```

Matching stays in Rust, so a registered type costs a few nanoseconds — a
signature that hits is actually faster than built-in detection, because it
skips the detection tree.

### By callable

For anything a fixed signature cannot express:

```python
mimey.register("application/x-even", ".even", matcher=lambda data: len(data) % 2 == 0)
```

The callable receives the same `bytes` you passed in and its exceptions
propagate to the caller. Every detection re-enters Python once per registered
callable (~40ns each), so prefer `magic` when a signature is enough.

### Inspecting and clearing

```python
mimey.registered()          # [('application/x-nes-rom', '.nes')]
mimey.clear_registrations()
```

