Package amplee :: Module http_helper
[hide private]
[frames] | no frames]

Module http_helper

source code

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)
Iterates through 'header_value' and check if found any match in 'within'.

Variables [hide private]
  q_separator = <_sre.SRE_Pattern object at 0xb7c008a8>

Function Details [hide private]

get_best_mimetype(header_value, within, default=None)

source code 

Iterates through 'header_value' and check if found any match
in 'within'.

When */* is part of header_value if no match has yet be found
during the process and 'default' is None,
the first element of 'within' is returned otehrwise 'default'

>>> 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)
>>> 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'
>>> a = 'application/*'
>>> get_best_mimetype(a, l) # returns None

Keyword arguments:
header_value -- a string of an header such as Accept
within -- a list of mime-types
default -- default value to return when nothing has been found


Variables Details [hide private]

q_separator

None
Value:
; *q *=