Joe Gregorio | b9e63ff | 2011-06-30 12:38:10 -0400 | [diff] [blame] | 1 | |
| 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 3 | <html><head><title>Python: module apiclient.mimeparse</title> |
| 4 | </head><body bgcolor="#f0f0f8"> |
| 5 | |
| 6 | <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading"> |
| 7 | <tr bgcolor="#7799ee"> |
| 8 | <td valign=bottom> <br> |
| 9 | <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 |
| 10 | ><td align=right valign=bottom |
Joe Gregorio | a845167 | 2011-09-15 09:53:21 -0400 | [diff] [blame^] | 11 | ><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> |
Joe Gregorio | b9e63ff | 2011-06-30 12:38:10 -0400 | [diff] [blame] | 12 | <p><tt>MIME-Type Parser<br> |
| 13 | <br> |
| 14 | This module provides basic functions for handling mime-types. It can handle<br> |
| 15 | matching mime-types against a list of media-ranges. See section 14.1 of the<br> |
| 16 | HTTP specification [<a href="http://www.rfc-editor.org/rfc/rfc2616.txt">RFC 2616</a>] for a complete explanation.<br> |
| 17 | <br> |
| 18 | <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> |
| 19 | <br> |
| 20 | Contents:<br> |
| 21 | - <a href="#-parse_mime_type">parse_mime_type</a>(): Parses a mime-type into its component parts.<br> |
| 22 | - <a href="#-parse_media_range">parse_media_range</a>(): Media-ranges are mime-types with wild-cards and a 'q'<br> |
| 23 | quality parameter.<br> |
| 24 | - <a href="#-quality">quality</a>(): Determines the quality ('q') of a mime-type when<br> |
| 25 | compared against a list of media-ranges.<br> |
| 26 | - <a href="#-quality_parsed">quality_parsed</a>(): Just like <a href="#-quality">quality</a>() except the second parameter must be<br> |
| 27 | pre-parsed.<br> |
| 28 | - <a href="#-best_match">best_match</a>(): Choose the mime-type with the highest quality ('q')<br> |
| 29 | from a list of candidates.</tt></p> |
| 30 | <p> |
| 31 | <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 32 | <tr bgcolor="#eeaa77"> |
| 33 | <td colspan=3 valign=bottom> <br> |
| 34 | <font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr> |
| 35 | |
| 36 | <tr><td bgcolor="#eeaa77"><tt> </tt></td><td> </td> |
| 37 | <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> |
| 38 | <br> |
| 39 | Takes a list of supported mime-types and finds the best match for all the<br> |
| 40 | media-ranges listed in header. The value of header must be a string that<br> |
| 41 | conforms to the format of the HTTP Accept: header. The value of 'supported'<br> |
| 42 | is a list of mime-types. The list of supported mime-types should be sorted<br> |
| 43 | in order of increasing desirability, in case of a situation where there is<br> |
| 44 | a tie.<br> |
| 45 | <br> |
| 46 | >>> <a href="#-best_match">best_match</a>(['application/xbel+xml', 'text/xml'],<br> |
| 47 | 'text/*;q=0.5,*/*; q=0.1')<br> |
| 48 | 'text/xml'</tt></dd></dl> |
| 49 | <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> |
| 50 | <br> |
| 51 | Find the best match for a given mime-type against a list of media_ranges<br> |
| 52 | that have already been parsed by <a href="#-parse_media_range">parse_media_range</a>(). Returns a tuple of<br> |
| 53 | the fitness value and the value of the 'q' quality parameter of the best<br> |
| 54 | match, or (-1, 0) if no match was found. Just as for <a href="#-quality_parsed">quality_parsed</a>(),<br> |
| 55 | 'parsed_ranges' must be a list of parsed media ranges.</tt></dd></dl> |
| 56 | <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> |
| 57 | <br> |
| 58 | Carves up a media range and returns a tuple of the (type, subtype,<br> |
| 59 | params) where 'params' is a dictionary of all the parameters for the media<br> |
| 60 | range. For example, the media range 'application/*;q=0.5' would get parsed<br> |
| 61 | into:<br> |
| 62 | <br> |
| 63 | ('application', '*', {'q', '0.5'})<br> |
| 64 | <br> |
| 65 | In addition this function also guarantees that there is a value for 'q'<br> |
| 66 | in the params dictionary, filling it in with a proper default if<br> |
| 67 | necessary.</tt></dd></dl> |
| 68 | <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> |
| 69 | <br> |
| 70 | Carves up a mime-type and returns a tuple of the (type, subtype, params)<br> |
| 71 | where 'params' is a dictionary of all the parameters for the media range.<br> |
| 72 | For example, the media range 'application/xhtml;q=0.5' would get parsed<br> |
| 73 | into:<br> |
| 74 | <br> |
| 75 | ('application', 'xhtml', {'q', '0.5'})</tt></dd></dl> |
| 76 | <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> |
| 77 | <br> |
| 78 | Returns the quality 'q' of a mime-type when compared against the<br> |
| 79 | media-ranges in ranges. For example:<br> |
| 80 | <br> |
| 81 | >>> <a href="#-quality">quality</a>('text/html','text/*;q=0.3, text/html;q=0.7,<br> |
| 82 | text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5')<br> |
| 83 | 0.7</tt></dd></dl> |
| 84 | <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> |
| 85 | <br> |
| 86 | Find the best match for a given mime-type against a list of media_ranges<br> |
| 87 | that have already been parsed by <a href="#-parse_media_range">parse_media_range</a>(). Returns the 'q'<br> |
| 88 | quality parameter of the best match, 0 if no match was found. This function<br> |
| 89 | bahaves the same as <a href="#-quality">quality</a>() except that 'parsed_ranges' must be a list of<br> |
| 90 | parsed media ranges.</tt></dd></dl> |
| 91 | </td></tr></table><p> |
| 92 | <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 93 | <tr bgcolor="#55aa55"> |
| 94 | <td colspan=3 valign=bottom> <br> |
| 95 | <font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr> |
| 96 | |
| 97 | <tr><td bgcolor="#55aa55"><tt> </tt></td><td> </td> |
| 98 | <td width="100%"><strong>__author__</strong> = 'Joe Gregorio'<br> |
| 99 | <strong>__credits__</strong> = ''<br> |
| 100 | <strong>__email__</strong> = 'joe@bitworking.org'<br> |
| 101 | <strong>__license__</strong> = 'MIT License'<br> |
| 102 | <strong>__version__</strong> = '0.1.3'</td></tr></table><p> |
| 103 | <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 104 | <tr bgcolor="#7799ee"> |
| 105 | <td colspan=3 valign=bottom> <br> |
| 106 | <font color="#ffffff" face="helvetica, arial"><big><strong>Author</strong></big></font></td></tr> |
| 107 | |
| 108 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 109 | <td width="100%">Joe Gregorio</td></tr></table><p> |
| 110 | <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> |
| 111 | <tr bgcolor="#7799ee"> |
| 112 | <td colspan=3 valign=bottom> <br> |
| 113 | <font color="#ffffff" face="helvetica, arial"><big><strong>Credits</strong></big></font></td></tr> |
| 114 | |
| 115 | <tr><td bgcolor="#7799ee"><tt> </tt></td><td> </td> |
| 116 | <td width="100%"></td></tr></table> |
| 117 | </body></html> |