File: //proc/self/root/opt/alt/python27/lib/python2.7/site-packages/paste/util/mimeparse.pyc
�
b�Nc @ sI d Z d � Z d � Z d � Z d � Z d � Z d � Z d � Z d S( s� MIME-Type Parser
This module provides basic functions for handling mime-types. It can handle
matching mime-types against a list of media-ranges. See section 14.1 of
the HTTP specification [RFC 2616] for a complete explanation.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
Based on mimeparse 0.1.2 by Joe Gregorio:
http://code.google.com/p/mimeparse/
Contents:
- parse_mime_type(): Parses a mime-type into its component parts.
- parse_media_range(): Media-ranges are mime-types with wild-cards and a 'q' quality parameter.
- quality(): Determines the quality ('q') of a mime-type when compared against a list of media-ranges.
- quality_parsed(): Just like quality() except the second parameter must be pre-parsed.
- best_match(): Choose the mime-type with the highest quality ('q') from a list of candidates.
- desired_matches(): Filter against a list of desired mime-types in the order the server prefers.
c C s | j d � } | d | d } } y | j d d � \ } } Wn* t k
rl | j � p_ d d } } n% X| j � p| d } | j � p� d } i } xo | D]g } | j d d � } t | � d k r� | d j � | d j � } } | r| r| | | <qq� q� W| | | f S( s3 Carves up a mime-type and returns a tuple of the
(type, subtype, params) where 'params' is a dictionary
of all the parameters for the media range.
For example, the media range 'application/xhtml;q=0.5' would
get parsed into:
('application', 'xhtml', {'q', '0.5'})
t ;i i t /t *t =i ( t splitt
ValueErrort stript len( t mime_typet typet plistt subtypet paramst paramt keyt value( ( sE /opt/alt/python27/lib/python2.7/site-packages/paste/util/mimeparse.pyt parse_mime_type s
!c C sy t | � \ } } } y3 d t | d � k o9 d k n sG t � n Wn! t t f k
rk d | d <n X| | | f S( s� Carves up a media range and returns a tuple of the
(type, subtype, params) where 'params' is a dictionary
of all the parameters for the media range.
For example, the media range 'application/*;q=0.5' would
get parsed into:
('application', '*', {'q', '0.5'})
In addition this function also guarantees that there
is a value for 'q' in the params dictionary, filling it
in with a proper default if necessary.
i t qi t 1( R t floatR t KeyError( t rangeR R R ( ( sE /opt/alt/python27/lib/python2.7/site-packages/paste/util/mimeparse.pyt parse_media_range3 s
&
c C s: d \ } } t | � \ } } } x| D]� \ } } } | | k s[ | d k s[ | d k r( | | k s | d k s | d k r( d }
| | k r� |
d 7}
n | | k r� |
d 7}
n xJ | D]B } | d k r� | | k r� | | | | k r |
d 7}
q q� q� W|
| k r&|
} | d } q&q( q( W| t | � f S( s� Find the best match for a given mime-type against
a list of media_ranges that have already been
parsed by parse_media_range(). Returns a tuple of
the fitness value and the value of the 'q' quality
parameter of the best match, or (-1, 0) if no match
was found. Just as for quality_parsed(), 'parsed_ranges'
must be a list of parsed media ranges.i����i R id i
R i ( i����i ( R R ( R t
parsed_rangest best_fitnesst
best_fit_qt target_typet target_subtypet
target_paramsR R R t fitnessR ( ( sE /opt/alt/python27/lib/python2.7/site-packages/paste/util/mimeparse.pyt fitness_and_quality_parsedH s(
c C s t | | � d S( sS Find the best match for a given mime-type against
a list of media_ranges that have already been
parsed by parse_media_range(). Returns the
'q' quality parameter of the best match, 0 if no
match was found. This function behaves the same as quality()
except that 'parsed_ranges' must be a list of
parsed media ranges.i ( R ( R R ( ( sE /opt/alt/python27/lib/python2.7/site-packages/paste/util/mimeparse.pyt quality_parsede s c C s% t t | j d � � } t | | � S( s� Returns the quality 'q' of a mime-type when compared
against the media-ranges in ranges. For example:
>>> quality('text/html','text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5')
0.7
t ,( t mapR R R ( R t rangesR ( ( sE /opt/alt/python27/lib/python2.7/site-packages/paste/util/mimeparse.pyt qualityo s c C s~ | s
d St t | j d � � } t g t | � D]"