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

mime_parser-1.1.0-py3-none-any.whl (56.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mime_parser-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 56.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.11

File hashes

Hashes for mime_parser-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b409a78ad85fd784d7bfa1f84336853be3e69b16b5ab7d333f31417b0f3627dd
MD5 3a6553ea2f501e4271d0e7ec6f43a7c2
BLAKE2b-256 e2506adbc09328738f48b0e7413cbfca27e08b681ce4067f3afd405c0ab5bded

See more details on using hashes here.

Supported by

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