Skip to main content

Encodes and decodes MIME data

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mime_parser-1.2.1-py3-none-any.whl (56.8 kB view details)

Uploaded Python 3

File details

Details for the file mime_parser-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: mime_parser-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 56.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for mime_parser-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3be9292bfcd89c741cb2a0b40d7e2db790f730f0c1899b217788252bcec61751
MD5 355c8164b254865ba386fb4c2e0e4ee6
BLAKE2b-256 8898438d8ee2476f67fb7a83df6a00ded5c341a2040b20729bb2341ac991d26a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page