888 return 1./score, value |
888 return 1./score, value |
889 |
889 |
890 def _parse_accept_header(raw_header, value_parser=None, value_sort_key=None): |
890 def _parse_accept_header(raw_header, value_parser=None, value_sort_key=None): |
891 """returns an ordered list accepted types |
891 """returns an ordered list accepted types |
892 |
892 |
893 returned value is a list of 2-tuple (value, score), ordered |
893 :param value_parser: a function to parse a raw accept chunk. If None |
894 by score. Exact type of `value` will depend on what `value_parser` |
894 is provided, the function defaults to identity. If a function is provided, |
895 will reutrn. if `value_parser` is None, then the raw value, as found |
895 it must accept 2 parameters ``value`` and ``other_params``. ``value`` is |
896 in the http header, is used. |
896 the value found before the first ';', `other_params` is a dictionary |
|
897 built from all other chunks after this first ';' |
|
898 |
|
899 :param value_sort_key: a key function to sort values found in the accept |
|
900 header. This function will be passed a 3-tuple |
|
901 (raw_value, parsed_value, score). If None is provided, the default |
|
902 sort_key is 1./score |
|
903 |
|
904 :return: a list of 3-tuple (raw_value, parsed_value, score), |
|
905 ordered by score. ``parsed_value`` will be the return value of |
|
906 ``value_parser(raw_value)`` |
897 """ |
907 """ |
898 if value_sort_key is None: |
908 if value_sort_key is None: |
899 value_sort_key = lambda infos: 1./infos[-1] |
909 value_sort_key = lambda infos: 1./infos[-1] |
900 values = [] |
910 values = [] |
901 for info in raw_header.split(','): |
911 for info in raw_header.split(','): |
926 (type, subtype, type_params) corresponding to the mimetype definition |
936 (type, subtype, type_params) corresponding to the mimetype definition |
927 e.g. : for 'text/*', `mimetypeinfo` will be ('text', '*', {}), for |
937 e.g. : for 'text/*', `mimetypeinfo` will be ('text', '*', {}), for |
928 'text/html;level=1', `mimetypeinfo` will be ('text', '*', {'level': '1'}) |
938 'text/html;level=1', `mimetypeinfo` will be ('text', '*', {'level': '1'}) |
929 """ |
939 """ |
930 try: |
940 try: |
931 media_type, media_subtype = value.strip().split('/') |
941 media_type, media_subtype = value.strip().split('/', 1) |
932 except ValueError: # safety belt : '/' should always be present |
942 except ValueError: # safety belt : '/' should always be present |
933 media_type = value.strip() |
943 media_type = value.strip() |
934 media_subtype = '*' |
944 media_subtype = '*' |
935 return (media_type, media_subtype, other_params) |
945 return (media_type, media_subtype, other_params) |
936 |
946 |