zope.contenttype

Latest release Supported Python versions https://github.com/zopefoundation/zope.contenttype/actions/workflows/tests.yml/badge.svg https://coveralls.io/repos/github/zopefoundation/zope.contenttype/badge.svg?branch=master Documentation Status

A utility module for content-type (MIME type) handling.

Functions include:

  • Guessing a content type given a name and (optional) body data.
  • Guessing a content type given some text.
  • Parsing MIME types.

Documentation is hosted at https://zopecontenttype.readthedocs.io/en/latest/

API

There are two major features provided by this package: determining content types, and parsing text representations of content types into structured data.

Note

When this module is imported, it uses add_files() to extend the standard mimetypes database with a set of common office and multimedia types and filename extensions.

Tip

When used as the main module (e.g., python -m zope.contenttype) this module will print out the mimetypes database.

Determining Content Types

zope.contenttype.guess_content_type(name='', body='', default=None)[source]

Given a named piece of content, try to guess its content type.

The implementation relies on the mimetypes standard Python module, the text_type() function also defined in this module, and a simple heuristic for detecting binary data.

Returns:

A tuple of (type, encoding) like mimetypes.guess_type().

Parameters:
  • name (str) – The file name for the content. This is given priority when guessing the type.
  • body (bytes) – The binary data of the content.
  • default (str) – If given, and no content type can be guessed, this will be returned as the type portion.
zope.contenttype.text_type(s)[source]

Given an unnamed piece of data, try to guess its content type.

Detects HTML, XML, and plain text.

Returns:A MIME type string such as ‘text/html’.
Return type:str
Parameters:s (bytes) – The binary data to examine.

Utility Functions

zope.contenttype.add_files(filenames)[source]

Add the names of MIME type map files to the standard mimetypes module.

MIME type map files are used for detecting the MIME type of some content based on the content’s filename extension.

The files should be formatted similarly to the ‘mime.types’ file included in this package. Each line specifies a MIME type and the file extensions that imply that MIME type. Here are some sample lines:

text/css                        css
text/plain                      bat c h pl ksh
text/x-vcard                    vcf

Parsing Text Representations

MIME Content-Type parsing helper functions.

This supports parsing RFC 1341 Content-Type values, including quoted-string values as defined in RFC 822.

zope.contenttype.parse.parse(string)[source]

Parse the given string as a MIME type.

This uses parseOrdered() and can raise the same exceptions it does.

Returns:A tuple (major, minor, params) where major and minor are the two parts of the type, and params is a dictionary containing any parameters by name.
Parameters:string (str) – The string to parse.
zope.contenttype.parse.parseOrdered(string)[source]

Parse the given string as a MIME type.

Returns:A tuple (major, minor, params) where major and minor are the two parts of the type, and params is a sequence of the parameters in order.
Raises:ValueError – If the string is malformed.
Parameters:string (str) – The string to parse.
zope.contenttype.parse.join(spec)[source]

Given a three-part tuple as produced by parse() or parseOrdered(), return the string representation.

Returns:The string representation. For example, given ('text', 'plain', [('encoding','utf-8')]), this will produce 'text/plain;encoding=utf-8'.
Return type:str

Change History

5.1 (2023-09-21)

  • Add some more MIME types and extensions.

5.0 (2023-03-27)

  • Add support for Python 3.11.
  • Drop support for Python 2.7, 3.5, 3.6.
  • Drop support for deprecated python setup.py test.

4.6 (2022-09-07)

  • Add support for Python 3.9, 3.10.
  • Drop support for Python 3.4.

4.5.0 (2019-12-19)

  • Fix tests on Python 3.8 (#7).
  • Add support for Python 3.8.

4.4 (2018-10-05)

  • Add support for Python 3.7.

4.3.0 (2017-08-10)

4.2.0 (2016-08-26)

  • Add support for Python 3.5.
  • Drop support for Python 2.6.

4.1.0 (2014-12-26)

  • Add support for Python 3.4 and PyPy3.
  • Add support for testing on Travis.

4.0.1 (2013-02-20)

  • Change the file contents argument of guess_content_type from string to bytes. This change has no effect on Python 2.

4.0.0 (2013-02-11)

  • Add some tests for better coverage.
  • Add tox.ini and manifest.
  • Add support for Python 3.3 and PyPy.
  • Drop support for Python 2.4 and 2.5.

3.5.5 (2011-07-27)

  • Properly restore the HTML snippet detection, by looking at the entire string and not just its start.

3.5.4 (2011-07-26)

  • Restore detection of HTML snippets from 3.4 series.

3.5.3 (2011-03-18)

  • Add new mime types for web fonts, cache manifest and new media formats.

3.5.2 (2011-02-11)

  • LP #717289: add video/x-m4v mimetype for the .m4v extension.

3.5.1 (2010-03-23)

  • LP #242321: fix IndexError raised when testing strings consisting solely of leading whitespace.

3.5.0 (2009-10-22)

  • Move the implementation of zope.publisher.contenttype to zope.contenttype.parse, moved tests along.

3.4.3 (2009-12-28)

  • Update mime-type for .js to be application/javascript.

3.4.1 (2009-02-04)

3.4.0 (2007-09-13)

  • First stable release as an independent package.

Development

zope.contenttype is hosted at GitHub:

Project URLs

Indices and tables