Skip to content

MIME types

rigour.mime

rigour.mime handles the parsing and normalisation of internet MIME types in Python. This can be useful to normalise invalid, or misformatted MIME types emitted by remote web servers.

Usage

The simplest use is to normalise a MIME type:

from rigour.mime import normalize_mimetype

assert normalize_mimetype('TEXT/PLAIN') == 'text/plain'
assert normalize_mimetype('plain/text') == 'text/plain'
assert normalize_mimetype(None) == 'application/octet-stream'
assert normalize_mimetype('') == 'application/octet-stream'

Internally, rigour.mime uses a MIMEType object to handle parsing. It can be used to access more specific information, like human readable labels:

from rigour.mime import parse_mimetype

parsed = parse_mimetype('text/plain')
assert parsed.family == 'text'
assert parsed.subtype == 'plain'
assert parsed.label == 'Plain text'

Open issues

  • Internationalisation, i.e. make the human-readable labels available in multiple languages.
  • Expand replacements for specific MIME types.

This module is an inlined version of the pantomime library.

mimetype_extension(mime_type)

Infer a possible extension from a MIME type.

Source code in rigour/mime/filename.py
def mimetype_extension(mime_type: Optional[str]) -> Optional[str]:
    """Infer a possible extension from a MIME type."""
    mime_type = normalize_mimetype(mime_type)
    if mime_type == DEFAULT:
        return None
    extension = guess_extension(mime_type)
    return normalize_extension(extension)

normalize_extension(extension)

Normalise a file name extension.

Source code in rigour/mime/filename.py
def normalize_extension(extension: Optional[str]) -> Optional[str]:
    """Normalise a file name extension."""
    extension = decode_path(extension)
    if extension is None:
        return None
    if extension.startswith("."):
        extension = extension[1:]
    if "." in extension:
        _, extension = os.path.splitext(extension)
    extension = slugify(extension, sep="")
    if extension is None or not len(extension):
        return None
    return extension

normalize_mimetype(text, default=DEFAULT)

Normalize the spelling of a MIME type.

Source code in rigour/mime/mime.py
def normalize_mimetype(text: Optional[str], default: str = DEFAULT) -> str:
    """Normalize the spelling of a MIME type."""
    return parse_mimetype(text, default=default).normalized or default

parse_mimetype(text, default=DEFAULT)

Parse a MIME type into a structured object.

Source code in rigour/mime/mime.py
7
8
9
def parse_mimetype(text: Optional[str], default: str = DEFAULT) -> MIMEType:
    """Parse a MIME type into a structured object."""
    return MIMEType.parse(text, default=default)

useful_mimetype(text)

Check to see if the given mime type is a MIME type which is useful in terms of how to treat this file.

Source code in rigour/mime/mime.py
def useful_mimetype(text: Optional[str]) -> bool:
    """Check to see if the given mime type is a MIME type
    which is useful in terms of how to treat this file.
    """
    mimetype = normalize_mimetype(text)
    return mimetype not in [DEFAULT, PLAIN, None]