Package amplee :: Package utils :: Module mediatype
[hide private]
[frames] | no frames]

Module mediatype

source code

Helper to find the best matching media-types.

Synopsis
--------

The Atom Publishing Protocol is very sensitive to the media-type
of resources it treats. Doing a simple string comparison is
certainly not enough in this case since media-types sent by
user-agents can be of different forms. This module provides only
one method called `get_best_mimetype` which does all the hard work
of finding the best matching media-type given a seed and a list
of acceptable media-types.



Classes [hide private]
  HeaderElement
An element (with parameters) from an HTTP header's element list.
  AcceptElement
An element (with parameters) from an Accept-* header's element list.
Functions [hide private]
 
get_best_mimetype(header_value, within, default=None, check_params=False, return_full=False)
Iterates through 'header_value' and checks if it finds any match in 'within'.
source code
Variables [hide private]
  __doc__ = """Helper to find the best matching media-typ...
  q_separator = re.compile(r'; *q *=')
Function Details [hide private]

get_best_mimetype(header_value, within, default=None, check_params=False, return_full=False)

source code 

Iterates through 'header_value' and checks if it finds any match in 'within'.

When */* is part of header_value and no candidate was found this function returns the first media-type of 'within'

Consider the following examples:
>>> from amplee.http_helper import get_best_mimetype
>>> l = ['application/rdf+xml', 'application/atom+xml']
>>> a = 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
>>> get_best_mimetype(a, l)
>>> 'application/rdf+xml'
>>> get_best_mimetype(a, l, 'application/atom+xml')
'application/atom+xml'
>>> a = 'text/xml,application/xml,application/atom+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
>>> get_best_mimetype(a, l)
'application/atom+xml'
>>> l = ['application/rdf+xml', 'entry']
>>> get_best_mimetype(a, l)
'application/atom+xml'
>>> a = 'text/xml,application/xml,text/html;q=0.9,text/plain;q=0.8,image/png'
>>> get_best_mimetype(a, l) # returns None
>>> a = 'application/xml;q=0.9,application/rdf+xml;q=0.8,application/atom+xml'
>>> l = ['application/rdf+xml', 'entry']
>>> get_best_mimetype(a, l)
'application/atom+xml'
>>> a = 'application/xml;q=0.9,application/rdf+xml;q=0.8,application/atom+xml;q=0.1'
>>> get_best_mimetype(a, l)
'application/rdf+xml'
>>> a = 'application/xml,application/rdf+xml;q=0.8,application/atom+xml'
>>> get_best_mimetype(a, l)
'application/atom+xml'
>>> l = ['application/rdf+xml', 'application/atom+xml', 'application/xhtml+xml'] 
>>> a = 'application/*'
>>> get_best_mimetype(a, l)
>>> 'application/xhtml+xml'
>>> l = [u'application/atom+xml;type=entry', u'application/x-www-form-urlencoded']
>>> a = u'application/atom+xml;type=entry;some=yu'
>>> get_best_mimetype(a, l, check_params=True)
>>> a = u'application/atom+xml;type=entry'
>>> get_best_mimetype(a, l, check_params=True)
u'application/atom+xml'
>>> get_best_mimetype(a, l, check_params=True, return_full=True)
u'application/atom+xml;type=entry'
>>> a = u'application/atom+xml;type=entry;some=yu'
>>> get_best_mimetype(a, l, check_params=['type'])
u'application/atom+xml'
>>> get_best_mimetype(a, l, check_params=['type'], return_full=True)
u'aPPlication/atom+xml; some=yu;type=entry'
>>> get_best_mimetype(a, l)
u'application/atom+xml'

The ``header_value`` is a string respecting the HTTP Accept header format as defined in section 14.1 of RFC 2616.

The ``within`` argument is a list of acceptable media-type strings.

The ``default`` value is returned when no match was found.

The ``check_params``, if provided, may be a list of keys (string) that should be matched between ``header_value`` and headers ``within`` or it can be a boolean. If ``True`` then every parameters will be tested, if ``False`` (default) the test won't occur. Setting it to ``True`` ensures that if a match is found it will be exactly the one wanted but this is a more restrictive matching scheme.

If ``return_full`` is ``True`` it returns the media-type along with its parameters. Otherwise it returns only the ``media-type``.

Variables Details [hide private]

__doc__

Value:
"""Helper to find the best matching media-types.

Synopsis
--------

The Atom Publishing Protocol is very sensitive to the media-type
of resources it treats. Doing a simple string comparison is
certainly not enough in this case since media-types sent by
...