| |
| <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| <html><head><title>Python: module apiclient.mimeparse</title> |
| </head><body bgcolor="#f0f0f8"> |
| |
| <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading"> |
| <tr bgcolor="#7799ee"> |
| <td valign=bottom> <br> |
| <font color="#ffffff" face="helvetica, arial"> <br><big><big><strong><a href="apiclient.html"><font color="#ffffff">apiclient</font></a>.mimeparse</strong></big></big> (version 0.1.3)</font></td |
| ><td align=right valign=bottom |
| ><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/usr/local/google/home/jcgregorio/projects/apiclient/apiclient/mimeparse.py">/usr/local/google/home/jcgregorio/projects/apiclient/apiclient/mimeparse.py</a></font></td></tr></table> |
| <p><tt>MIME-Type Parser<br> |
| <br> |
| This module provides basic functions for handling mime-types. It can handle<br> |
| matching mime-types against a list of media-ranges. See section 14.1 of the<br> |
| HTTP specification [<a href="http://www.rfc-editor.org/rfc/rfc2616.txt">RFC 2616</a>] for a complete explanation.<br> |
| <br> |
| <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1">http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1</a><br> |
| <br> |
| Contents:<br> |
| - <a href="#-parse_mime_type">parse_mime_type</a>(): Parses a mime-type into its component parts.<br> |
| - <a href="#-parse_media_range">parse_media_range</a>(): Media-ranges are mime-types with wild-cards and a 'q'<br> |
| quality parameter.<br> |
| - <a href="#-quality">quality</a>(): Determines the quality ('q') of a mime-type when<br> |
| compared against a list of media-ranges.<br> |
| - <a href="#-quality_parsed">quality_parsed</a>(): Just like <a href="#-quality">quality</a>() except the second parameter must be<br> |
| pre-parsed.<br> |
| - <a href="#-best_match">best_match</a>(): Choose the mime-type with the highest quality ('q')<br> |
| from a list of candidates.</tt></p> |
| <p> |
| <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| <tr bgcolor="#eeaa77"> |
| <td colspan=3 valign=bottom> <br> |
| <font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr> |
| |
| <tr><td bgcolor="#eeaa77"><tt> </tt></td><td> </td> |
| <td width="100%"><dl><dt><a name="-best_match"><strong>best_match</strong></a>(supported, header)</dt><dd><tt>Return mime-type with the highest quality ('q') from list of candidates.<br> |
| <br> |
| Takes a list of supported mime-types and finds the best match for all the<br> |
| media-ranges listed in header. The value of header must be a string that<br> |
| conforms to the format of the HTTP Accept: header. The value of 'supported'<br> |
| is a list of mime-types. The list of supported mime-types should be sorted<br> |
| in order of increasing desirability, in case of a situation where there is<br> |
| a tie.<br> |
| <br> |
| >>> <a href="#-best_match">best_match</a>(['application/xbel+xml', 'text/xml'],<br> |
| 'text/*;q=0.5,*/*; q=0.1')<br> |
| 'text/xml'</tt></dd></dl> |
| <dl><dt><a name="-fitness_and_quality_parsed"><strong>fitness_and_quality_parsed</strong></a>(mime_type, parsed_ranges)</dt><dd><tt>Find the best match for a mime-type amongst parsed media-ranges.<br> |
| <br> |
| Find the best match for a given mime-type against a list of media_ranges<br> |
| that have already been parsed by <a href="#-parse_media_range">parse_media_range</a>(). Returns a tuple of<br> |
| the fitness value and the value of the 'q' quality parameter of the best<br> |
| match, or (-1, 0) if no match was found. Just as for <a href="#-quality_parsed">quality_parsed</a>(),<br> |
| 'parsed_ranges' must be a list of parsed media ranges.</tt></dd></dl> |
| <dl><dt><a name="-parse_media_range"><strong>parse_media_range</strong></a>(range)</dt><dd><tt>Parse a media-range into its component parts.<br> |
| <br> |
| Carves up a media range and returns a tuple of the (type, subtype,<br> |
| params) where 'params' is a dictionary of all the parameters for the media<br> |
| range. For example, the media range 'application/*;q=0.5' would get parsed<br> |
| into:<br> |
| <br> |
| ('application', '*', {'q', '0.5'})<br> |
| <br> |
| In addition this function also guarantees that there is a value for 'q'<br> |
| in the params dictionary, filling it in with a proper default if<br> |
| necessary.</tt></dd></dl> |
| <dl><dt><a name="-parse_mime_type"><strong>parse_mime_type</strong></a>(mime_type)</dt><dd><tt>Parses a mime-type into its component parts.<br> |
| <br> |
| Carves up a mime-type and returns a tuple of the (type, subtype, params)<br> |
| where 'params' is a dictionary of all the parameters for the media range.<br> |
| For example, the media range 'application/xhtml;q=0.5' would get parsed<br> |
| into:<br> |
| <br> |
| ('application', 'xhtml', {'q', '0.5'})</tt></dd></dl> |
| <dl><dt><a name="-quality"><strong>quality</strong></a>(mime_type, ranges)</dt><dd><tt>Return the quality ('q') of a mime-type against a list of media-ranges.<br> |
| <br> |
| Returns the quality 'q' of a mime-type when compared against the<br> |
| media-ranges in ranges. For example:<br> |
| <br> |
| >>> <a href="#-quality">quality</a>('text/html','text/*;q=0.3, text/html;q=0.7,<br> |
| text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5')<br> |
| 0.7</tt></dd></dl> |
| <dl><dt><a name="-quality_parsed"><strong>quality_parsed</strong></a>(mime_type, parsed_ranges)</dt><dd><tt>Find the best match for a mime-type amongst parsed media-ranges.<br> |
| <br> |
| Find the best match for a given mime-type against a list of media_ranges<br> |
| that have already been parsed by <a href="#-parse_media_range">parse_media_range</a>(). Returns the 'q'<br> |
| quality parameter of the best match, 0 if no match was found. This function<br> |
| bahaves the same as <a href="#-quality">quality</a>() except that 'parsed_ranges' must be a list of<br> |
| parsed media ranges.</tt></dd></dl> |
| </td></tr></table><p> |
| <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| <tr bgcolor="#55aa55"> |
| <td colspan=3 valign=bottom> <br> |
| <font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr> |
| |
| <tr><td bgcolor="#55aa55"><tt> </tt></td><td> </td> |
| <td width="100%"><strong>__author__</strong> = 'Joe Gregorio'<br> |
| <strong>__credits__</strong> = ''<br> |
| <strong>__email__</strong> = 'joe@bitworking.org'<br> |
| <strong>__license__</strong> = 'MIT License'<br> |
| <strong>__version__</strong> = '0.1.3'</td></tr></table><p> |
| <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| <tr bgcolor="#7799ee"> |
| <td colspan=3 valign=bottom> <br> |
| <font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr> |
| |
| <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| <td width="100%">Joe Gregorio</td></tr></table><p> |
| <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| <tr bgcolor="#7799ee"> |
| <td colspan=3 valign=bottom> <br> |
| <font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr> |
| |
| <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| <td width="100%"></td></tr></table> |
| </body></html> |