accept.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. from __future__ import annotations
  2. import codecs
  3. import re
  4. from .structures import ImmutableList
  5. class Accept(ImmutableList):
  6. """An :class:`Accept` object is just a list subclass for lists of
  7. ``(value, quality)`` tuples. It is automatically sorted by specificity
  8. and quality.
  9. All :class:`Accept` objects work similar to a list but provide extra
  10. functionality for working with the data. Containment checks are
  11. normalized to the rules of that header:
  12. >>> a = CharsetAccept([('ISO-8859-1', 1), ('utf-8', 0.7)])
  13. >>> a.best
  14. 'ISO-8859-1'
  15. >>> 'iso-8859-1' in a
  16. True
  17. >>> 'UTF8' in a
  18. True
  19. >>> 'utf7' in a
  20. False
  21. To get the quality for an item you can use normal item lookup:
  22. >>> print a['utf-8']
  23. 0.7
  24. >>> a['utf7']
  25. 0
  26. .. versionchanged:: 0.5
  27. :class:`Accept` objects are forced immutable now.
  28. .. versionchanged:: 1.0.0
  29. :class:`Accept` internal values are no longer ordered
  30. alphabetically for equal quality tags. Instead the initial
  31. order is preserved.
  32. """
  33. def __init__(self, values=()):
  34. if values is None:
  35. list.__init__(self)
  36. self.provided = False
  37. elif isinstance(values, Accept):
  38. self.provided = values.provided
  39. list.__init__(self, values)
  40. else:
  41. self.provided = True
  42. values = sorted(
  43. values, key=lambda x: (self._specificity(x[0]), x[1]), reverse=True
  44. )
  45. list.__init__(self, values)
  46. def _specificity(self, value):
  47. """Returns a tuple describing the value's specificity."""
  48. return (value != "*",)
  49. def _value_matches(self, value, item):
  50. """Check if a value matches a given accept item."""
  51. return item == "*" or item.lower() == value.lower()
  52. def __getitem__(self, key):
  53. """Besides index lookup (getting item n) you can also pass it a string
  54. to get the quality for the item. If the item is not in the list, the
  55. returned quality is ``0``.
  56. """
  57. if isinstance(key, str):
  58. return self.quality(key)
  59. return list.__getitem__(self, key)
  60. def quality(self, key):
  61. """Returns the quality of the key.
  62. .. versionadded:: 0.6
  63. In previous versions you had to use the item-lookup syntax
  64. (eg: ``obj[key]`` instead of ``obj.quality(key)``)
  65. """
  66. for item, quality in self:
  67. if self._value_matches(key, item):
  68. return quality
  69. return 0
  70. def __contains__(self, value):
  71. for item, _quality in self:
  72. if self._value_matches(value, item):
  73. return True
  74. return False
  75. def __repr__(self):
  76. pairs_str = ", ".join(f"({x!r}, {y})" for x, y in self)
  77. return f"{type(self).__name__}([{pairs_str}])"
  78. def index(self, key):
  79. """Get the position of an entry or raise :exc:`ValueError`.
  80. :param key: The key to be looked up.
  81. .. versionchanged:: 0.5
  82. This used to raise :exc:`IndexError`, which was inconsistent
  83. with the list API.
  84. """
  85. if isinstance(key, str):
  86. for idx, (item, _quality) in enumerate(self):
  87. if self._value_matches(key, item):
  88. return idx
  89. raise ValueError(key)
  90. return list.index(self, key)
  91. def find(self, key):
  92. """Get the position of an entry or return -1.
  93. :param key: The key to be looked up.
  94. """
  95. try:
  96. return self.index(key)
  97. except ValueError:
  98. return -1
  99. def values(self):
  100. """Iterate over all values."""
  101. for item in self:
  102. yield item[0]
  103. def to_header(self):
  104. """Convert the header set into an HTTP header string."""
  105. result = []
  106. for value, quality in self:
  107. if quality != 1:
  108. value = f"{value};q={quality}"
  109. result.append(value)
  110. return ",".join(result)
  111. def __str__(self):
  112. return self.to_header()
  113. def _best_single_match(self, match):
  114. for client_item, quality in self:
  115. if self._value_matches(match, client_item):
  116. # self is sorted by specificity descending, we can exit
  117. return client_item, quality
  118. return None
  119. def best_match(self, matches, default=None):
  120. """Returns the best match from a list of possible matches based
  121. on the specificity and quality of the client. If two items have the
  122. same quality and specificity, the one is returned that comes first.
  123. :param matches: a list of matches to check for
  124. :param default: the value that is returned if none match
  125. """
  126. result = default
  127. best_quality = -1
  128. best_specificity = (-1,)
  129. for server_item in matches:
  130. match = self._best_single_match(server_item)
  131. if not match:
  132. continue
  133. client_item, quality = match
  134. specificity = self._specificity(client_item)
  135. if quality <= 0 or quality < best_quality:
  136. continue
  137. # better quality or same quality but more specific => better match
  138. if quality > best_quality or specificity > best_specificity:
  139. result = server_item
  140. best_quality = quality
  141. best_specificity = specificity
  142. return result
  143. @property
  144. def best(self):
  145. """The best match as value."""
  146. if self:
  147. return self[0][0]
  148. _mime_split_re = re.compile(r"/|(?:\s*;\s*)")
  149. def _normalize_mime(value):
  150. return _mime_split_re.split(value.lower())
  151. class MIMEAccept(Accept):
  152. """Like :class:`Accept` but with special methods and behavior for
  153. mimetypes.
  154. """
  155. def _specificity(self, value):
  156. return tuple(x != "*" for x in _mime_split_re.split(value))
  157. def _value_matches(self, value, item):
  158. # item comes from the client, can't match if it's invalid.
  159. if "/" not in item:
  160. return False
  161. # value comes from the application, tell the developer when it
  162. # doesn't look valid.
  163. if "/" not in value:
  164. raise ValueError(f"invalid mimetype {value!r}")
  165. # Split the match value into type, subtype, and a sorted list of parameters.
  166. normalized_value = _normalize_mime(value)
  167. value_type, value_subtype = normalized_value[:2]
  168. value_params = sorted(normalized_value[2:])
  169. # "*/*" is the only valid value that can start with "*".
  170. if value_type == "*" and value_subtype != "*":
  171. raise ValueError(f"invalid mimetype {value!r}")
  172. # Split the accept item into type, subtype, and parameters.
  173. normalized_item = _normalize_mime(item)
  174. item_type, item_subtype = normalized_item[:2]
  175. item_params = sorted(normalized_item[2:])
  176. # "*/not-*" from the client is invalid, can't match.
  177. if item_type == "*" and item_subtype != "*":
  178. return False
  179. return (
  180. (item_type == "*" and item_subtype == "*")
  181. or (value_type == "*" and value_subtype == "*")
  182. ) or (
  183. item_type == value_type
  184. and (
  185. item_subtype == "*"
  186. or value_subtype == "*"
  187. or (item_subtype == value_subtype and item_params == value_params)
  188. )
  189. )
  190. @property
  191. def accept_html(self):
  192. """True if this object accepts HTML."""
  193. return (
  194. "text/html" in self or "application/xhtml+xml" in self or self.accept_xhtml
  195. )
  196. @property
  197. def accept_xhtml(self):
  198. """True if this object accepts XHTML."""
  199. return "application/xhtml+xml" in self or "application/xml" in self
  200. @property
  201. def accept_json(self):
  202. """True if this object accepts JSON."""
  203. return "application/json" in self
  204. _locale_delim_re = re.compile(r"[_-]")
  205. def _normalize_lang(value):
  206. """Process a language tag for matching."""
  207. return _locale_delim_re.split(value.lower())
  208. class LanguageAccept(Accept):
  209. """Like :class:`Accept` but with normalization for language tags."""
  210. def _value_matches(self, value, item):
  211. return item == "*" or _normalize_lang(value) == _normalize_lang(item)
  212. def best_match(self, matches, default=None):
  213. """Given a list of supported values, finds the best match from
  214. the list of accepted values.
  215. Language tags are normalized for the purpose of matching, but
  216. are returned unchanged.
  217. If no exact match is found, this will fall back to matching
  218. the first subtag (primary language only), first with the
  219. accepted values then with the match values. This partial is not
  220. applied to any other language subtags.
  221. The default is returned if no exact or fallback match is found.
  222. :param matches: A list of supported languages to find a match.
  223. :param default: The value that is returned if none match.
  224. """
  225. # Look for an exact match first. If a client accepts "en-US",
  226. # "en-US" is a valid match at this point.
  227. result = super().best_match(matches)
  228. if result is not None:
  229. return result
  230. # Fall back to accepting primary tags. If a client accepts
  231. # "en-US", "en" is a valid match at this point. Need to use
  232. # re.split to account for 2 or 3 letter codes.
  233. fallback = Accept(
  234. [(_locale_delim_re.split(item[0], 1)[0], item[1]) for item in self]
  235. )
  236. result = fallback.best_match(matches)
  237. if result is not None:
  238. return result
  239. # Fall back to matching primary tags. If the client accepts
  240. # "en", "en-US" is a valid match at this point.
  241. fallback_matches = [_locale_delim_re.split(item, 1)[0] for item in matches]
  242. result = super().best_match(fallback_matches)
  243. # Return a value from the original match list. Find the first
  244. # original value that starts with the matched primary tag.
  245. if result is not None:
  246. return next(item for item in matches if item.startswith(result))
  247. return default
  248. class CharsetAccept(Accept):
  249. """Like :class:`Accept` but with normalization for charsets."""
  250. def _value_matches(self, value, item):
  251. def _normalize(name):
  252. try:
  253. return codecs.lookup(name).name
  254. except LookupError:
  255. return name.lower()
  256. return item == "*" or _normalize(value) == _normalize(item)