Skip to main content

Type-safe MIME type enumeration for Python

Project description

mime-enum

Release Build status codecov License

A type-safe Python library for working with MIME types and file extensions.

The mime-enum package provides a comprehensive enumeration of MIME types with their associated file extensions. It offers a clean, type-safe API for parsing MIME type strings, looking up MIME types by file extension, and working with file paths.

Installation

Install using pip:

pip install mime-enum

Or using uv:

uv add mime-enum

Quick Start

The mime-enum library provides three key capabilities: type-safe MIME type access, flexible string parsing, and file extension lookups.

Type-Safe MIME Types

Access MIME types as strongly-typed enum values with full IDE support:

from mime_enum import MimeType

# Enum values work as strings with autocompletion and type checking
json_mime = MimeType.APPLICATION_JSON
print(json_mime)  # "application/json"
print(json_mime.extensions)  # ("json",)

Convenient Aliases

For commonly used MIME types with verbose names, convenient aliases are provided:

# Microsoft Office formats - use short aliases instead of verbose names
docx = MimeType.APPLICATION_DOCX  # vs APPLICATION_VND_OPENXMLFORMATS_OFFICEDOCUMENT_WORDPROCESSINGML_DOCUMENT
xlsx = MimeType.APPLICATION_XLSX  # vs APPLICATION_VND_OPENXMLFORMATS_OFFICEDOCUMENT_SPREADSHEETML_SHEET
pptx = MimeType.APPLICATION_PPTX  # vs APPLICATION_VND_OPENXMLFORMATS_OFFICEDOCUMENT_PRESENTATIONML_PRESENTATION

# String representation shows the full MIME type
print(docx)  # "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
print(xlsx)  # "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
print(pptx)  # "application/vnd.openxmlformats-officedocument.presentationml.presentation"

# All aliases point to the same enum instances
assert MimeType.APPLICATION_DOCX is MimeType.APPLICATION_VND_OPENXMLFORMATS_OFFICEDOCUMENT_WORDPROCESSINGML_DOCUMENT

# Available aliases: DOCX, XLSX, PPTX, DOTX, XLTX, POTX, PPSX, SLDX

Flexible String Parsing

Parse real-world MIME type strings with automatic parameter stripping and alias normalization:

from mime_enum import parse, try_parse

# Strips parameters automatically
mime_type = parse("application/json; charset=utf-8")
print(mime_type)  # MimeType.APPLICATION_JSON

# Normalizes common aliases to canonical forms
canonical = parse("text/json")  # → MimeType.APPLICATION_JSON
canonical = parse("application/javascript")  # → MimeType.TEXT_JAVASCRIPT

# Safe parsing returns None instead of raising exceptions
unknown = try_parse("application/unknown")
print(unknown)  # None

File Extension Lookups

Detect MIME types from file extensions and paths:

from mime_enum import from_extension, from_path

# Look up by extension (with or without dot, case-insensitive)
pdf_mime = from_extension(".pdf")  # MimeType.APPLICATION_PDF
json_mime = from_extension("JSON")  # MimeType.APPLICATION_JSON

# Detect from complete file paths
mime_type = from_path("/path/to/document.pdf")  # MimeType.APPLICATION_PDF

Note: These functions only examine file extensions, not actual file content. For content-based detection, consider python-magic or filetype packages.

For detailed usage examples, see the Usage Guide.

For complete API documentation, see the API Reference.

Acknowledgments

This project uses the mimeData.json file from mimetype-io by Patrick McCallum.

Project details


Download files

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

Source Distribution

mime_enum-0.0.2.tar.gz (125.4 kB view details)

Uploaded Source

Built Distribution

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

mime_enum-0.0.2-py3-none-any.whl (31.1 kB view details)

Uploaded Python 3

File details

Details for the file mime_enum-0.0.2.tar.gz.

File metadata

  • Download URL: mime_enum-0.0.2.tar.gz
  • Upload date:
  • Size: 125.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.14

File hashes

Hashes for mime_enum-0.0.2.tar.gz
Algorithm Hash digest
SHA256 0109b24e96c1a0bde7fc08d2d69a85c09a7d32ed82ea79a4d95cb2445a09c373
MD5 38f6ffbe4a43650af8b2d4edc6b5d4f3
BLAKE2b-256 35139284452b7fff8bd399038d8e0b572786038b6ca8ac529b9d4ead5982c3b1

See more details on using hashes here.

File details

Details for the file mime_enum-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: mime_enum-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 31.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.14

File hashes

Hashes for mime_enum-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9ac60a0ba4604904c202595c90fb732f6cc82bba4cccde33100bb1e48d87edd5
MD5 278b6fb53c1750a770c439275e4b0cef
BLAKE2b-256 7bb7d8885a0018c3a91a7791cef91d09fea78ecbfae0a508c11ce949f7a7cd2b

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