Metadata-Version: 2.4
Name: pimht
Version: 0.6.0
Summary: Parser for mhtml files
Home-page: https://github.com/pilate/pimht
License: MIT License
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: chardet
Provides-Extra: speedups
Requires-Dist: faust-cchardet; extra == "speedups"
Requires-Dist: pybase64; extra == "speedups"
Dynamic: license-file

# pimht
Python mhtml parser

# Installation
```
$ pip install pimht
```

# Example

```python
import pimht

mhtml = pimht.from_filename("test.mhtml")
for part in mhtml:
    print(part.content_type, len(part.raw))
```

# Modifying

```python
import pimht

mhtml = pimht.from_filename("test.mhtml")
for part in mhtml.parts:
    if part.is_text:
        part.text = part.text.replace("Hello", "Goodbye")

with open("modified.mhtml", "wb") as f:
    f.write(mhtml.to_bytes())
```

# API

Parsing (all return an `MHTML` object):
- `pimht.from_filename(path)`
- `pimht.from_bytes(data)`
- `pimht.from_string(text)`
- `pimht.from_fileobj(fp)` — text or binary mode

`MHTML`:
- `headers` — top-level headers as a dict
- `parts` — list of `MHTMLPart` (iterating the `MHTML` object yields the same parts)
- `to_bytes()` — serialize the archive, including any modifications

`MHTMLPart`:
- `headers` — part headers as a dict
- `content_type` — mime type, e.g. `"text/html"`
- `is_text` — whether `content_type` is `text/*`
- `charset` — charset from the Content-Type header, or `None`
- `raw` — decoded content as `bytes`; assignable
- `text` — decoded content as `str` (text parts only); assignable

Assigning to `raw` or `text` re-encodes the content so `to_bytes()`
reflects the change.

# Performance
The `chardet` module, used by default, is slow. Performance can be improved by also installing `faust-cchardet` and `pybase64` with:
```
$ pip install pimht[speedups]
```

This is aimed specifically at parsing Google Chrome generated snapshots as fast as possible, but feel free to report issues with MHTML files from other sources.
