media1488%python.module
fault.internet

Mime Type structures and parsers for content types and media ranges.

Interfaces here work exclusively with character-strings; wire data must be decoded.

References

Properties

types

A mapping of type names and file extensions to MIME type strings.

filename_extensions

A mapping of filename extensions to type name.

iana_registered_types

The IRI of the set of media types registered with IANA.

Functions

type_from_string

Construct a Type instance from a MIME type string.

Code:

from fault.internet import media
mt = media.type_from_string("text/xml")

Equivalent to Type.from_string, but cached.

range_from_string

Construct a Range instance from a Media Range string like an Accept header.

Equivalent to Range.from_string, but cached.

operator
import

functools0
import

typing0
import

tools0
import

iana_registered_types0
data

iana_registered_types = 'https://www.iana.org/assignments/media-types/media-types.xml'

types0
data

types = {
	'data': 'application/octet-stream', # browsers interpret this as a file download
	'fail': 'application/failure+xml',

	'python-pickle': 'application/x-python-object+pickle',
	'python-marshal': 'application/x-python-object+marshal',
	'python-xml': 'application/x-python-object+xml', # fault.xml format
	# 'structures': 'application/x-conceptual+xml',

	'text': 'text/plain',
	'txt': 'text/plain',
	'rtf': 'application/rtf',

	'cache': 'text/cache-manifest',
	'html': 'text/html',
	'htm': 'text/html',
	'css': 'text/css',

	'pdf': 'application/pdf',
	'postscript': 'application/postscript',

	'javascript': 'text/plain;pl=javascript',
	'py': 'text/plain;pl=python',

	# Recommended types for .js files.
	'json': 'application/json',
	'js': 'application/javascript',

	'xml': 'text/xml',
	'sgml': 'text/sgml',

	'rdf': 'application/rdf+xml',
	'rss': 'application/rss+xml',
	'atom': 'application/atom+xml',
	'xslt': 'application/xslt+xml',
	'xsl': 'application/xslt+xml',

	'zip': 'application/zip',
	'gzip': 'application/gzip',
	'gz': 'application/gzip',
	'bzip2': 'application/x-bzip2',
	'tar': 'application/x-tar',
	'xz': 'application/x-xz',
	'rar': 'application/x-rar-compressed',
	'sit': 'application/x-stuffit',
	'z': 'application/x-compress',

	'tgz': 'application/x-tar+gzip',
	'txz': 'application/x-tar+x-xz',
	'torrent': 'application/x-bittorrent',

	# images
	'svg': 'image/svg+xml',
	'png': 'image/png',
	'gif': 'image/gif',
	'tiff': 'image/tiff',
	'tif': 'image/tiff',
	'jpeg': 'image/jpeg',
	'jpg': 'image/jpeg',

	# video
	'mpg': 'video/mpeg',
	'mpeg': 'video/mpeg',
	'mp2': 'video/mpeg',
	'mov': 'video/quicktime',
	'mp4': 'video/mp4',
	'webm': 'video/webm',
	'ogv': 'video/ogg',
	'avi': 'video/avi',

	# audio
	'mp3': 'audio/mpeg',
	'mid': 'audio/midi',
	'wav': 'audio/x-wav',
	'aif': 'audio/x-aiff',
	'aiff': 'audio/x-aiff',

	'ogg': 'audio/ogg',
	'opus': 'audio/ogg',
	'oga': 'audio/ogg',
	'ogx': 'application/ogg',
	'spx': 'audio/ogg',

	# microsoft
	'xls': 'application/vnd.ms-excel',
	'doc': 'application/msword',
	'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
	'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
	'pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
	'ppsx': 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
	'potx': 'application/vnd.openxmlformats-officedocument.presentationml.template',
}

@functools.lru_cache(32)

file_type183%
function

file_type(filename)

Identify the MIME type from a filename using common file extensions.

Unidentified extensions will likely return application/octets-stream.

Type0
class

An IANA Media Type; the Content-Type, Subtype, and options triple describing the type of data.

A container interface (in) is provided in order to identify if a given type is considered to be within another:

The from_string classmethod is the common constructor.

Type__slots__0
data

__slots__ = ()

Type__str__0
method

__str__(self)

Type__bytes__0
method

__bytes__(self)

Typecotype0
property

Content Type; usually one of:

  • application

  • text

  • image

  • video

  • model

The initial part of a MIME (media) type.

Typesubtype0
property

Subtype; the specific form of the cotype.

Typeparameters0
property

Parameters such as charset for encoding designation.

Typepattern0
property
bool

Whether the Type is a pattern and can match multiple types.

Typepush30%
method

push(self, subtype)

Return a new Type with the given subtype appended to the instance's subtype.

Typepop60%
method

pop(self)

Return a new Type with the last '+'-delimited subtype removed. (inverse of push).

Typefrom_bytes0
classmethod

from_bytes(Class, string, **parameters)

Split on the ';' and '/' separators and build an instance.

Additional parameters or overrides may be given using keywords.

Typefrom_string0
classmethod

from_string(Class, string, **parameters)

Split on the ';' and '/' separators and build an instance.

Additional parameters or overrides may be given using keywords.

Type__contains__0
method

__contains__(self, mtype)

Range0
class

Media Range class for supporting Accept headers.

Ranges are a mapping of content-types to an ordered sequence of subtype sets. The ordering of the subtype sets indicates the relative quality.

Querying the range for a set of types will return the types with the highest precedence for each content type.

None is used to represent any types, *.

Range__slots__0
data

__slots__ = ()
	@staticmethod

Rangesplit0
staticmethod

split(media_range)

Construct triples describing the media_range.

Rangefrom_string0
classmethod

from_string(Class, string)

Instantiate the Range from a string.

Rangefrom_bytes0
classmethod

from_bytes(Class, data)

Instantiate the Range from a bytes object; decoded and passed to from_string.

Rangequality40%
method

quality(self, mimetype)

Search for a single MIME type in the range. Return identified quality of the type.

The quality of a particular type is useful in cases where the server wants to give some precedence to another type. Given that the client accepts XML, but it is a lower quality than HTML, the server may want to send XML anyways.

Rangequery0
method

query(self, *available)

Given a sequence of mime types, return the best match according to the qualities recorded in the range.

any_type0
data

any_type = Type(('*', '*', frozenset()))

any_range0
data

any_range = Range([(100, any_type)])

type_from_bytes0
data

type_from_bytes = functools.lru_cache(32)(Type.from_bytes)

type_from_string0
data

type_from_string = functools.lru_cache(32)(Type.from_string)

range_from_string0
data

range_from_string = functools.lru_cache(32)(Range.from_string)