Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: appengine/chromium_build_logs/third_party/googleapiclient/mimeparse.py

Issue 1260293009: make version of ts_mon compatible with appengine (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: clean up code Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright (C) 2007 Joe Gregorio 1 # Copyright 2014 Joe Gregorio
2 # 2 #
3 # Licensed under the MIT License 3 # Licensed under the MIT License
4 4
5 """MIME-Type Parser 5 """MIME-Type Parser
6 6
7 This module provides basic functions for handling mime-types. It can handle 7 This module provides basic functions for handling mime-types. It can handle
8 matching mime-types against a list of media-ranges. See section 14.1 of the 8 matching mime-types against a list of media-ranges. See section 14.1 of the
9 HTTP specification [RFC 2616] for a complete explanation. 9 HTTP specification [RFC 2616] for a complete explanation.
10 10
11 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1 11 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
12 12
13 Contents: 13 Contents:
14 - parse_mime_type(): Parses a mime-type into its component parts. 14 - parse_mime_type(): Parses a mime-type into its component parts.
15 - parse_media_range(): Media-ranges are mime-types with wild-cards and a 'q' 15 - parse_media_range(): Media-ranges are mime-types with wild-cards and a 'q'
16 quality parameter. 16 quality parameter.
17 - quality(): Determines the quality ('q') of a mime-type when 17 - quality(): Determines the quality ('q') of a mime-type when
18 compared against a list of media-ranges. 18 compared against a list of media-ranges.
19 - quality_parsed(): Just like quality() except the second parameter must be 19 - quality_parsed(): Just like quality() except the second parameter must be
20 pre-parsed. 20 pre-parsed.
21 - best_match(): Choose the mime-type with the highest quality ('q') 21 - best_match(): Choose the mime-type with the highest quality ('q')
22 from a list of candidates. 22 from a list of candidates.
23 """ 23 """
24 from __future__ import absolute_import
25 from functools import reduce
26 import six
24 27
25 __version__ = '0.1.3' 28 __version__ = '0.1.3'
26 __author__ = 'Joe Gregorio' 29 __author__ = 'Joe Gregorio'
27 __email__ = 'joe@bitworking.org' 30 __email__ = 'joe@bitworking.org'
28 __license__ = 'MIT License' 31 __license__ = 'MIT License'
29 __credits__ = '' 32 __credits__ = ''
30 33
31 34
32 def parse_mime_type(mime_type): 35 def parse_mime_type(mime_type):
33 """Parses a mime-type into its component parts. 36 """Parses a mime-type into its component parts.
(...skipping 27 matching lines...) Expand all
61 range. For example, the media range 'application/*;q=0.5' would get parsed 64 range. For example, the media range 'application/*;q=0.5' would get parsed
62 into: 65 into:
63 66
64 ('application', '*', {'q', '0.5'}) 67 ('application', '*', {'q', '0.5'})
65 68
66 In addition this function also guarantees that there is a value for 'q' 69 In addition this function also guarantees that there is a value for 'q'
67 in the params dictionary, filling it in with a proper default if 70 in the params dictionary, filling it in with a proper default if
68 necessary. 71 necessary.
69 """ 72 """
70 (type, subtype, params) = parse_mime_type(range) 73 (type, subtype, params) = parse_mime_type(range)
71 if not params.has_key('q') or not params['q'] or \ 74 if 'q' not in params or not params['q'] or \
72 not float(params['q']) or float(params['q']) > 1\ 75 not float(params['q']) or float(params['q']) > 1\
73 or float(params['q']) < 0: 76 or float(params['q']) < 0:
74 params['q'] = '1' 77 params['q'] = '1'
75 78
76 return (type, subtype, params) 79 return (type, subtype, params)
77 80
78 81
79 def fitness_and_quality_parsed(mime_type, parsed_ranges): 82 def fitness_and_quality_parsed(mime_type, parsed_ranges):
80 """Find the best match for a mime-type amongst parsed media-ranges. 83 """Find the best match for a mime-type amongst parsed media-ranges.
81 84
82 Find the best match for a given mime-type against a list of media_ranges 85 Find the best match for a given mime-type against a list of media_ranges
83 that have already been parsed by parse_media_range(). Returns a tuple of 86 that have already been parsed by parse_media_range(). Returns a tuple of
84 the fitness value and the value of the 'q' quality parameter of the best 87 the fitness value and the value of the 'q' quality parameter of the best
85 match, or (-1, 0) if no match was found. Just as for quality_parsed(), 88 match, or (-1, 0) if no match was found. Just as for quality_parsed(),
86 'parsed_ranges' must be a list of parsed media ranges. 89 'parsed_ranges' must be a list of parsed media ranges.
87 """ 90 """
88 best_fitness = -1 91 best_fitness = -1
89 best_fit_q = 0 92 best_fit_q = 0
90 (target_type, target_subtype, target_params) =\ 93 (target_type, target_subtype, target_params) =\
91 parse_media_range(mime_type) 94 parse_media_range(mime_type)
92 for (type, subtype, params) in parsed_ranges: 95 for (type, subtype, params) in parsed_ranges:
93 type_match = (type == target_type or\ 96 type_match = (type == target_type or\
94 type == '*' or\ 97 type == '*' or\
95 target_type == '*') 98 target_type == '*')
96 subtype_match = (subtype == target_subtype or\ 99 subtype_match = (subtype == target_subtype or\
97 subtype == '*' or\ 100 subtype == '*' or\
98 target_subtype == '*') 101 target_subtype == '*')
99 if type_match and subtype_match: 102 if type_match and subtype_match:
100 param_matches = reduce(lambda x, y: x + y, [1 for (key, value) in \ 103 param_matches = reduce(lambda x, y: x + y, [1 for (key, value) in \
101 target_params.iteritems() if key != 'q' and \ 104 six.iteritems(target_params) if key != 'q' and \
102 params.has_key(key) and value == params[key]], 0) 105 key in params and value == params[key]], 0)
103 fitness = (type == target_type) and 100 or 0 106 fitness = (type == target_type) and 100 or 0
104 fitness += (subtype == target_subtype) and 10 or 0 107 fitness += (subtype == target_subtype) and 10 or 0
105 fitness += param_matches 108 fitness += param_matches
106 if fitness > best_fitness: 109 if fitness > best_fitness:
107 best_fitness = fitness 110 best_fitness = fitness
108 best_fit_q = params['q'] 111 best_fit_q = params['q']
109 112
110 return best_fitness, float(best_fit_q) 113 return best_fitness, float(best_fit_q)
111 114
112 115
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 pos += 1 166 pos += 1
164 weighted_matches.sort() 167 weighted_matches.sort()
165 168
166 return weighted_matches[-1][0][1] and weighted_matches[-1][2] or '' 169 return weighted_matches[-1][0][1] and weighted_matches[-1][2] or ''
167 170
168 171
169 def _filter_blank(i): 172 def _filter_blank(i):
170 for s in i: 173 for s in i:
171 if s.strip(): 174 if s.strip():
172 yield s 175 yield s
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698