gws.lib.mime

MIME types.

Defines constants for the MIME types used in the application (PNG, JSON, GML3 and so on) and functions to normalize MIME types and to map them to and from file extensions.

All MIME types in the application go through get. It accepts a MIME type in any spelling (case, spaces, quoted parameter values), a content type with parameters, a known alias (e.g. application/vnd.ogc.gml or image/png8) or a file extension, and returns the normalized MIME type with its parameters: lower case, "; " between parameters, known types and aliases mapped to the constants of this module (application/gml+xml; version=3). get_base returns it without parameters. Lookups that are not covered by the built-in tables fall back to the standard mimetypes module; unknown MIME types give an empty string.

Example:

gws.lib.mime.get('image/jpg')  # 'image/jpeg'
gws.lib.mime.get('png')  # 'image/png'
gws.lib.mime.get('image/png8')  # 'image/png; mode=8bit'
gws.lib.mime.get_base('image/png8')  # 'image/png'
gws.lib.mime.for_path('/data/report.pdf')  # 'application/pdf'
gws.lib.mime.extension_for(gws.lib.mime.JPEG)  # 'jpeg'

Source code: gws.lib.mime

Package Contents

gws.lib.mime.BIN = 'application/octet-stream'
gws.lib.mime.CSS = 'text/css'
gws.lib.mime.CSV = 'text/csv'
gws.lib.mime.DOC = 'application/msword'
gws.lib.mime.DOCX = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
gws.lib.mime.extension_for(mime: str, default: str = 'bin') → str

Return the file extension for a MIME type.

Parameters:
  • mime – Normalized MIME type.

  • default – Extension to return if the MIME type is unknown.

Returns:

The extension, without a dot.

gws.lib.mime.for_path(path: str) → str

Return the MIME type for a file path, based on its extension.

Parameters:

path – File path or name.

Returns:

The MIME type, or BIN if it is unknown.

gws.lib.mime.FORM = 'application/x-www-form-urlencoded'
gws.lib.mime.GEOJSON = 'application/geo+json'
gws.lib.mime.get(s: str) → str

Return the normalized MIME type, keeping its parameters.

Known types and aliases are mapped to the constants of this module (image/png8 to PNG8). Other parameters are kept: text/html;Charset=utf-8 gives text/html; charset=utf-8.

Parameters:

s – MIME type in any spelling, content type, alias or file extension.

Returns:

The normalized MIME type, or an empty string if it is unknown.

gws.lib.mime.get_base(s: str) → str

Return the normalized MIME type without parameters.

Parameters are removed before and after the lookup, so that unknown parameters do not make a known type unknown.

Example:

get_base('image/png8')  # 'image/png'
get_base('application/vnd.ogc.gml')  # 'application/gml+xml'
Parameters:

s – MIME type in any spelling, content type, alias or file extension.

Returns:

The normalized MIME type without parameters, or an empty string if it is unknown.

gws.lib.mime.GIF = 'image/gif'
gws.lib.mime.GML = 'application/gml+xml'
gws.lib.mime.GML2 = 'application/gml+xml; version=2'
gws.lib.mime.GML3 = 'application/gml+xml; version=3'
gws.lib.mime.GML32 = 'application/gml+xml; version=3.2'
gws.lib.mime.GPKG = 'application/geopackage+sqlite3'
gws.lib.mime.GZIP = 'application/gzip'
gws.lib.mime.HTML = 'text/html'
gws.lib.mime.JPEG = 'image/jpeg'
gws.lib.mime.JPEG2000 = 'image/jp2'
gws.lib.mime.JS = 'text/javascript'
gws.lib.mime.JSON = 'application/json'
gws.lib.mime.KML = 'application/vnd.google-earth.kml+xml'
gws.lib.mime.KMZ = 'application/vnd.google-earth.kmz'
gws.lib.mime.MSGPACK = 'application/msgpack'
gws.lib.mime.MULTIPART = 'multipart/form-data'
gws.lib.mime.MVT = 'application/vnd.mapbox-vector-tile'
gws.lib.mime.PDF = 'application/pdf'
gws.lib.mime.PNG = 'image/png'
gws.lib.mime.PNG24 = 'image/png; mode=32bit'
gws.lib.mime.PNG8 = 'image/png; mode=8bit'
gws.lib.mime.PPT = 'application/vnd.ms-powerpoint'
gws.lib.mime.PPTX = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
gws.lib.mime.SVG = 'image/svg+xml'
gws.lib.mime.TIFF = 'image/tiff'
gws.lib.mime.TTF = 'font/ttf'
gws.lib.mime.TXT = 'text/plain'
gws.lib.mime.WEBP = 'image/webp'
gws.lib.mime.WOFF2 = 'font/woff2'
gws.lib.mime.XLS = 'application/vnd.ms-excel'
gws.lib.mime.XLSX = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
gws.lib.mime.XML = 'text/xml'
gws.lib.mime.ZIP = 'application/zip'