Skip to main content

mime-parser

PyPI PyPI - Python Version GitHub

Encodes and decodes MIME data

Features

  • Supported in Python 3.8 and later.
  • MIME text parsing
  • MIME based encode and decode
  • Plugin support
  • IANA media types
  • No dependencies

List of pre-implemented codecs

  • application/json
  • application/gzip

Installation

pip install mime-parser

If you want to add orjson support:

pip install mime-parser[full]

Usage

MIME text parsing

from mime_parser import parse_mime

mime = parse_mime("image/jpeg;q=0.8")

print(mime.family)  # image
print(mime.subtype)  # jpeg
print(mime.parameter)  # q=0.8
print(mime.original)  # image/jpeg;q=0.8
print(mime.parameter_tuple)  # ('q', '0.8')

MIME based encode and decode

from mime_parser import decode, encode

original_data = b"..."
encoded_data = encode("application/gzip", original_data)

decoded_data = decode("application/gzip", encoded_data)
assert original_data == decoded_data

Register MIME codec

How to register pyyaml:

import yaml
from mime_parser import decode, register

def yaml_encoder(data):
    return yaml.dump(data).encode("utf-8")

def yaml_decoder(data):
    return yaml.full_load(data)

register("application/yaml", encoder=yaml_encoder, decoder=yaml_decoder)

test_yaml = """
test:
  value: 0
"""

decoded_yaml = decode("application/yaml", test_yaml.encode(encoding="utf-8"))
print(decoded_yaml)

Plugin support

  • Your package name must start with mime-parser-.
  • Add the __mime_parser__ attribute to your package's root __init__.py file.
    • __mime_parser__ must be Iterable.
    • The Element of __mime_parser__ must contain:
      • mime string
      • encoder(Any) -> bytes callable
      • decoder(bytes) -> Any callable
  • The plugin must be installed with pip or included in sys.path

Examples of plugins that support pyyaml:

import yaml

def yaml_encoder(data):
    return yaml.dump(data).encode("utf-8")

def yaml_decoder(data):
    return yaml.full_load(data)

__mime_parser__ = [
    {
        "mime": "application/yaml",
        "encoder": yaml_encoder,
        "decoder": yaml_decoder,
    },
]

Command line usage

Encoding:

echo "..." | python -m mime_parser encode application/gzip > data

Decoding:

cat data | python -m mime_parser decode application/gzip > original

Prints a list of installed codecs:

python -m mime_parser list

Prints the MIME types registered with IANA:

python -m mime_parser iana

More options:

python -m mime_parser --help

Environment variables

  • MIME_PARSER_PLUGIN_PREFIX: Package name prefix to search for plugins.
  • MIME_PARSER_PLUGIN_DENIES: Deny list of plugins separated by :.
  • MIME_PARSER_PLUGIN_ALLOWS: Allow list of plugins separated by :.
  • MIME_PARSER_DISABLE_ORJSON_INSTALL: without using orjson, Use the default json library.

License

See the LICENSE file for details. In summary, mime-parser is licensed under the MIT license.

Metadata

Release files for mime-parser 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for mime-parser 1.2.1
File Interpreter ABI Platform
mime_parser-1.2.1-py3-none-any.whl Python 3 none any Details

Release files / mime_parser-1.2.1-py3-none-any.whl

Download URL mime_parser-1.2.1-py3-none-any.whl
Size 56.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3be9292bfcd89c741cb2a0b40d7e2db790f730f0c1899b217788252bcec61751
BLAKE2b-256 checksum
How to use checksums
8898438d8ee2476f67fb7a83df6a00ded5c341a2040b20729bb2341ac991d26a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Security for mime-parser 1.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page