structures.py 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. from __future__ import annotations
  2. from collections.abc import MutableSet
  3. from copy import deepcopy
  4. from .. import exceptions
  5. from .._internal import _missing
  6. from .mixins import ImmutableDictMixin
  7. from .mixins import ImmutableListMixin
  8. from .mixins import ImmutableMultiDictMixin
  9. from .mixins import UpdateDictMixin
  10. def is_immutable(self):
  11. raise TypeError(f"{type(self).__name__!r} objects are immutable")
  12. def iter_multi_items(mapping):
  13. """Iterates over the items of a mapping yielding keys and values
  14. without dropping any from more complex structures.
  15. """
  16. if isinstance(mapping, MultiDict):
  17. yield from mapping.items(multi=True)
  18. elif isinstance(mapping, dict):
  19. for key, value in mapping.items():
  20. if isinstance(value, (tuple, list)):
  21. for v in value:
  22. yield key, v
  23. else:
  24. yield key, value
  25. else:
  26. yield from mapping
  27. class ImmutableList(ImmutableListMixin, list):
  28. """An immutable :class:`list`.
  29. .. versionadded:: 0.5
  30. :private:
  31. """
  32. def __repr__(self):
  33. return f"{type(self).__name__}({list.__repr__(self)})"
  34. class TypeConversionDict(dict):
  35. """Works like a regular dict but the :meth:`get` method can perform
  36. type conversions. :class:`MultiDict` and :class:`CombinedMultiDict`
  37. are subclasses of this class and provide the same feature.
  38. .. versionadded:: 0.5
  39. """
  40. def get(self, key, default=None, type=None):
  41. """Return the default value if the requested data doesn't exist.
  42. If `type` is provided and is a callable it should convert the value,
  43. return it or raise a :exc:`ValueError` if that is not possible. In
  44. this case the function will return the default as if the value was not
  45. found:
  46. >>> d = TypeConversionDict(foo='42', bar='blub')
  47. >>> d.get('foo', type=int)
  48. 42
  49. >>> d.get('bar', -1, type=int)
  50. -1
  51. :param key: The key to be looked up.
  52. :param default: The default value to be returned if the key can't
  53. be looked up. If not further specified `None` is
  54. returned.
  55. :param type: A callable that is used to cast the value in the
  56. :class:`MultiDict`. If a :exc:`ValueError` or a
  57. :exc:`TypeError` is raised by this callable the default
  58. value is returned.
  59. .. versionchanged:: 3.0.2
  60. Returns the default value on :exc:`TypeError`, too.
  61. """
  62. try:
  63. rv = self[key]
  64. except KeyError:
  65. return default
  66. if type is not None:
  67. try:
  68. rv = type(rv)
  69. except (ValueError, TypeError):
  70. rv = default
  71. return rv
  72. class ImmutableTypeConversionDict(ImmutableDictMixin, TypeConversionDict):
  73. """Works like a :class:`TypeConversionDict` but does not support
  74. modifications.
  75. .. versionadded:: 0.5
  76. """
  77. def copy(self):
  78. """Return a shallow mutable copy of this object. Keep in mind that
  79. the standard library's :func:`copy` function is a no-op for this class
  80. like for any other python immutable type (eg: :class:`tuple`).
  81. """
  82. return TypeConversionDict(self)
  83. def __copy__(self):
  84. return self
  85. class MultiDict(TypeConversionDict):
  86. """A :class:`MultiDict` is a dictionary subclass customized to deal with
  87. multiple values for the same key which is for example used by the parsing
  88. functions in the wrappers. This is necessary because some HTML form
  89. elements pass multiple values for the same key.
  90. :class:`MultiDict` implements all standard dictionary methods.
  91. Internally, it saves all values for a key as a list, but the standard dict
  92. access methods will only return the first value for a key. If you want to
  93. gain access to the other values, too, you have to use the `list` methods as
  94. explained below.
  95. Basic Usage:
  96. >>> d = MultiDict([('a', 'b'), ('a', 'c')])
  97. >>> d
  98. MultiDict([('a', 'b'), ('a', 'c')])
  99. >>> d['a']
  100. 'b'
  101. >>> d.getlist('a')
  102. ['b', 'c']
  103. >>> 'a' in d
  104. True
  105. It behaves like a normal dict thus all dict functions will only return the
  106. first value when multiple values for one key are found.
  107. From Werkzeug 0.3 onwards, the `KeyError` raised by this class is also a
  108. subclass of the :exc:`~exceptions.BadRequest` HTTP exception and will
  109. render a page for a ``400 BAD REQUEST`` if caught in a catch-all for HTTP
  110. exceptions.
  111. A :class:`MultiDict` can be constructed from an iterable of
  112. ``(key, value)`` tuples, a dict, a :class:`MultiDict` or from Werkzeug 0.2
  113. onwards some keyword parameters.
  114. :param mapping: the initial value for the :class:`MultiDict`. Either a
  115. regular dict, an iterable of ``(key, value)`` tuples
  116. or `None`.
  117. """
  118. def __init__(self, mapping=None):
  119. if isinstance(mapping, MultiDict):
  120. dict.__init__(self, ((k, vs[:]) for k, vs in mapping.lists()))
  121. elif isinstance(mapping, dict):
  122. tmp = {}
  123. for key, value in mapping.items():
  124. if isinstance(value, (tuple, list)):
  125. if len(value) == 0:
  126. continue
  127. value = list(value)
  128. else:
  129. value = [value]
  130. tmp[key] = value
  131. dict.__init__(self, tmp)
  132. else:
  133. tmp = {}
  134. for key, value in mapping or ():
  135. tmp.setdefault(key, []).append(value)
  136. dict.__init__(self, tmp)
  137. def __getstate__(self):
  138. return dict(self.lists())
  139. def __setstate__(self, value):
  140. dict.clear(self)
  141. dict.update(self, value)
  142. def __iter__(self):
  143. # Work around https://bugs.python.org/issue43246.
  144. # (`return super().__iter__()` also works here, which makes this look
  145. # even more like it should be a no-op, yet it isn't.)
  146. return dict.__iter__(self)
  147. def __getitem__(self, key):
  148. """Return the first data value for this key;
  149. raises KeyError if not found.
  150. :param key: The key to be looked up.
  151. :raise KeyError: if the key does not exist.
  152. """
  153. if key in self:
  154. lst = dict.__getitem__(self, key)
  155. if len(lst) > 0:
  156. return lst[0]
  157. raise exceptions.BadRequestKeyError(key)
  158. def __setitem__(self, key, value):
  159. """Like :meth:`add` but removes an existing key first.
  160. :param key: the key for the value.
  161. :param value: the value to set.
  162. """
  163. dict.__setitem__(self, key, [value])
  164. def add(self, key, value):
  165. """Adds a new value for the key.
  166. .. versionadded:: 0.6
  167. :param key: the key for the value.
  168. :param value: the value to add.
  169. """
  170. dict.setdefault(self, key, []).append(value)
  171. def getlist(self, key, type=None):
  172. """Return the list of items for a given key. If that key is not in the
  173. `MultiDict`, the return value will be an empty list. Just like `get`,
  174. `getlist` accepts a `type` parameter. All items will be converted
  175. with the callable defined there.
  176. :param key: The key to be looked up.
  177. :param type: A callable that is used to cast the value in the
  178. :class:`MultiDict`. If a :exc:`ValueError` is raised
  179. by this callable the value will be removed from the list.
  180. :return: a :class:`list` of all the values for the key.
  181. """
  182. try:
  183. rv = dict.__getitem__(self, key)
  184. except KeyError:
  185. return []
  186. if type is None:
  187. return list(rv)
  188. result = []
  189. for item in rv:
  190. try:
  191. result.append(type(item))
  192. except ValueError:
  193. pass
  194. return result
  195. def setlist(self, key, new_list):
  196. """Remove the old values for a key and add new ones. Note that the list
  197. you pass the values in will be shallow-copied before it is inserted in
  198. the dictionary.
  199. >>> d = MultiDict()
  200. >>> d.setlist('foo', ['1', '2'])
  201. >>> d['foo']
  202. '1'
  203. >>> d.getlist('foo')
  204. ['1', '2']
  205. :param key: The key for which the values are set.
  206. :param new_list: An iterable with the new values for the key. Old values
  207. are removed first.
  208. """
  209. dict.__setitem__(self, key, list(new_list))
  210. def setdefault(self, key, default=None):
  211. """Returns the value for the key if it is in the dict, otherwise it
  212. returns `default` and sets that value for `key`.
  213. :param key: The key to be looked up.
  214. :param default: The default value to be returned if the key is not
  215. in the dict. If not further specified it's `None`.
  216. """
  217. if key not in self:
  218. self[key] = default
  219. else:
  220. default = self[key]
  221. return default
  222. def setlistdefault(self, key, default_list=None):
  223. """Like `setdefault` but sets multiple values. The list returned
  224. is not a copy, but the list that is actually used internally. This
  225. means that you can put new values into the dict by appending items
  226. to the list:
  227. >>> d = MultiDict({"foo": 1})
  228. >>> d.setlistdefault("foo").extend([2, 3])
  229. >>> d.getlist("foo")
  230. [1, 2, 3]
  231. :param key: The key to be looked up.
  232. :param default_list: An iterable of default values. It is either copied
  233. (in case it was a list) or converted into a list
  234. before returned.
  235. :return: a :class:`list`
  236. """
  237. if key not in self:
  238. default_list = list(default_list or ())
  239. dict.__setitem__(self, key, default_list)
  240. else:
  241. default_list = dict.__getitem__(self, key)
  242. return default_list
  243. def items(self, multi=False):
  244. """Return an iterator of ``(key, value)`` pairs.
  245. :param multi: If set to `True` the iterator returned will have a pair
  246. for each value of each key. Otherwise it will only
  247. contain pairs for the first value of each key.
  248. """
  249. for key, values in dict.items(self):
  250. if multi:
  251. for value in values:
  252. yield key, value
  253. else:
  254. yield key, values[0]
  255. def lists(self):
  256. """Return a iterator of ``(key, values)`` pairs, where values is the list
  257. of all values associated with the key."""
  258. for key, values in dict.items(self):
  259. yield key, list(values)
  260. def values(self):
  261. """Returns an iterator of the first value on every key's value list."""
  262. for values in dict.values(self):
  263. yield values[0]
  264. def listvalues(self):
  265. """Return an iterator of all values associated with a key. Zipping
  266. :meth:`keys` and this is the same as calling :meth:`lists`:
  267. >>> d = MultiDict({"foo": [1, 2, 3]})
  268. >>> zip(d.keys(), d.listvalues()) == d.lists()
  269. True
  270. """
  271. return dict.values(self)
  272. def copy(self):
  273. """Return a shallow copy of this object."""
  274. return self.__class__(self)
  275. def deepcopy(self, memo=None):
  276. """Return a deep copy of this object."""
  277. return self.__class__(deepcopy(self.to_dict(flat=False), memo))
  278. def to_dict(self, flat=True):
  279. """Return the contents as regular dict. If `flat` is `True` the
  280. returned dict will only have the first item present, if `flat` is
  281. `False` all values will be returned as lists.
  282. :param flat: If set to `False` the dict returned will have lists
  283. with all the values in it. Otherwise it will only
  284. contain the first value for each key.
  285. :return: a :class:`dict`
  286. """
  287. if flat:
  288. return dict(self.items())
  289. return dict(self.lists())
  290. def update(self, mapping):
  291. """update() extends rather than replaces existing key lists:
  292. >>> a = MultiDict({'x': 1})
  293. >>> b = MultiDict({'x': 2, 'y': 3})
  294. >>> a.update(b)
  295. >>> a
  296. MultiDict([('y', 3), ('x', 1), ('x', 2)])
  297. If the value list for a key in ``other_dict`` is empty, no new values
  298. will be added to the dict and the key will not be created:
  299. >>> x = {'empty_list': []}
  300. >>> y = MultiDict()
  301. >>> y.update(x)
  302. >>> y
  303. MultiDict([])
  304. """
  305. for key, value in iter_multi_items(mapping):
  306. MultiDict.add(self, key, value)
  307. def pop(self, key, default=_missing):
  308. """Pop the first item for a list on the dict. Afterwards the
  309. key is removed from the dict, so additional values are discarded:
  310. >>> d = MultiDict({"foo": [1, 2, 3]})
  311. >>> d.pop("foo")
  312. 1
  313. >>> "foo" in d
  314. False
  315. :param key: the key to pop.
  316. :param default: if provided the value to return if the key was
  317. not in the dictionary.
  318. """
  319. try:
  320. lst = dict.pop(self, key)
  321. if len(lst) == 0:
  322. raise exceptions.BadRequestKeyError(key)
  323. return lst[0]
  324. except KeyError:
  325. if default is not _missing:
  326. return default
  327. raise exceptions.BadRequestKeyError(key) from None
  328. def popitem(self):
  329. """Pop an item from the dict."""
  330. try:
  331. item = dict.popitem(self)
  332. if len(item[1]) == 0:
  333. raise exceptions.BadRequestKeyError(item[0])
  334. return (item[0], item[1][0])
  335. except KeyError as e:
  336. raise exceptions.BadRequestKeyError(e.args[0]) from None
  337. def poplist(self, key):
  338. """Pop the list for a key from the dict. If the key is not in the dict
  339. an empty list is returned.
  340. .. versionchanged:: 0.5
  341. If the key does no longer exist a list is returned instead of
  342. raising an error.
  343. """
  344. return dict.pop(self, key, [])
  345. def popitemlist(self):
  346. """Pop a ``(key, list)`` tuple from the dict."""
  347. try:
  348. return dict.popitem(self)
  349. except KeyError as e:
  350. raise exceptions.BadRequestKeyError(e.args[0]) from None
  351. def __copy__(self):
  352. return self.copy()
  353. def __deepcopy__(self, memo):
  354. return self.deepcopy(memo=memo)
  355. def __repr__(self):
  356. return f"{type(self).__name__}({list(self.items(multi=True))!r})"
  357. class _omd_bucket:
  358. """Wraps values in the :class:`OrderedMultiDict`. This makes it
  359. possible to keep an order over multiple different keys. It requires
  360. a lot of extra memory and slows down access a lot, but makes it
  361. possible to access elements in O(1) and iterate in O(n).
  362. """
  363. __slots__ = ("prev", "key", "value", "next")
  364. def __init__(self, omd, key, value):
  365. self.prev = omd._last_bucket
  366. self.key = key
  367. self.value = value
  368. self.next = None
  369. if omd._first_bucket is None:
  370. omd._first_bucket = self
  371. if omd._last_bucket is not None:
  372. omd._last_bucket.next = self
  373. omd._last_bucket = self
  374. def unlink(self, omd):
  375. if self.prev:
  376. self.prev.next = self.next
  377. if self.next:
  378. self.next.prev = self.prev
  379. if omd._first_bucket is self:
  380. omd._first_bucket = self.next
  381. if omd._last_bucket is self:
  382. omd._last_bucket = self.prev
  383. class OrderedMultiDict(MultiDict):
  384. """Works like a regular :class:`MultiDict` but preserves the
  385. order of the fields. To convert the ordered multi dict into a
  386. list you can use the :meth:`items` method and pass it ``multi=True``.
  387. In general an :class:`OrderedMultiDict` is an order of magnitude
  388. slower than a :class:`MultiDict`.
  389. .. admonition:: note
  390. Due to a limitation in Python you cannot convert an ordered
  391. multi dict into a regular dict by using ``dict(multidict)``.
  392. Instead you have to use the :meth:`to_dict` method, otherwise
  393. the internal bucket objects are exposed.
  394. """
  395. def __init__(self, mapping=None):
  396. dict.__init__(self)
  397. self._first_bucket = self._last_bucket = None
  398. if mapping is not None:
  399. OrderedMultiDict.update(self, mapping)
  400. def __eq__(self, other):
  401. if not isinstance(other, MultiDict):
  402. return NotImplemented
  403. if isinstance(other, OrderedMultiDict):
  404. iter1 = iter(self.items(multi=True))
  405. iter2 = iter(other.items(multi=True))
  406. try:
  407. for k1, v1 in iter1:
  408. k2, v2 = next(iter2)
  409. if k1 != k2 or v1 != v2:
  410. return False
  411. except StopIteration:
  412. return False
  413. try:
  414. next(iter2)
  415. except StopIteration:
  416. return True
  417. return False
  418. if len(self) != len(other):
  419. return False
  420. for key, values in self.lists():
  421. if other.getlist(key) != values:
  422. return False
  423. return True
  424. __hash__ = None
  425. def __reduce_ex__(self, protocol):
  426. return type(self), (list(self.items(multi=True)),)
  427. def __getstate__(self):
  428. return list(self.items(multi=True))
  429. def __setstate__(self, values):
  430. dict.clear(self)
  431. for key, value in values:
  432. self.add(key, value)
  433. def __getitem__(self, key):
  434. if key in self:
  435. return dict.__getitem__(self, key)[0].value
  436. raise exceptions.BadRequestKeyError(key)
  437. def __setitem__(self, key, value):
  438. self.poplist(key)
  439. self.add(key, value)
  440. def __delitem__(self, key):
  441. self.pop(key)
  442. def keys(self):
  443. return (key for key, value in self.items())
  444. def __iter__(self):
  445. return iter(self.keys())
  446. def values(self):
  447. return (value for key, value in self.items())
  448. def items(self, multi=False):
  449. ptr = self._first_bucket
  450. if multi:
  451. while ptr is not None:
  452. yield ptr.key, ptr.value
  453. ptr = ptr.next
  454. else:
  455. returned_keys = set()
  456. while ptr is not None:
  457. if ptr.key not in returned_keys:
  458. returned_keys.add(ptr.key)
  459. yield ptr.key, ptr.value
  460. ptr = ptr.next
  461. def lists(self):
  462. returned_keys = set()
  463. ptr = self._first_bucket
  464. while ptr is not None:
  465. if ptr.key not in returned_keys:
  466. yield ptr.key, self.getlist(ptr.key)
  467. returned_keys.add(ptr.key)
  468. ptr = ptr.next
  469. def listvalues(self):
  470. for _key, values in self.lists():
  471. yield values
  472. def add(self, key, value):
  473. dict.setdefault(self, key, []).append(_omd_bucket(self, key, value))
  474. def getlist(self, key, type=None):
  475. try:
  476. rv = dict.__getitem__(self, key)
  477. except KeyError:
  478. return []
  479. if type is None:
  480. return [x.value for x in rv]
  481. result = []
  482. for item in rv:
  483. try:
  484. result.append(type(item.value))
  485. except ValueError:
  486. pass
  487. return result
  488. def setlist(self, key, new_list):
  489. self.poplist(key)
  490. for value in new_list:
  491. self.add(key, value)
  492. def setlistdefault(self, key, default_list=None):
  493. raise TypeError("setlistdefault is unsupported for ordered multi dicts")
  494. def update(self, mapping):
  495. for key, value in iter_multi_items(mapping):
  496. OrderedMultiDict.add(self, key, value)
  497. def poplist(self, key):
  498. buckets = dict.pop(self, key, ())
  499. for bucket in buckets:
  500. bucket.unlink(self)
  501. return [x.value for x in buckets]
  502. def pop(self, key, default=_missing):
  503. try:
  504. buckets = dict.pop(self, key)
  505. except KeyError:
  506. if default is not _missing:
  507. return default
  508. raise exceptions.BadRequestKeyError(key) from None
  509. for bucket in buckets:
  510. bucket.unlink(self)
  511. return buckets[0].value
  512. def popitem(self):
  513. try:
  514. key, buckets = dict.popitem(self)
  515. except KeyError as e:
  516. raise exceptions.BadRequestKeyError(e.args[0]) from None
  517. for bucket in buckets:
  518. bucket.unlink(self)
  519. return key, buckets[0].value
  520. def popitemlist(self):
  521. try:
  522. key, buckets = dict.popitem(self)
  523. except KeyError as e:
  524. raise exceptions.BadRequestKeyError(e.args[0]) from None
  525. for bucket in buckets:
  526. bucket.unlink(self)
  527. return key, [x.value for x in buckets]
  528. class CombinedMultiDict(ImmutableMultiDictMixin, MultiDict):
  529. """A read only :class:`MultiDict` that you can pass multiple :class:`MultiDict`
  530. instances as sequence and it will combine the return values of all wrapped
  531. dicts:
  532. >>> from werkzeug.datastructures import CombinedMultiDict, MultiDict
  533. >>> post = MultiDict([('foo', 'bar')])
  534. >>> get = MultiDict([('blub', 'blah')])
  535. >>> combined = CombinedMultiDict([get, post])
  536. >>> combined['foo']
  537. 'bar'
  538. >>> combined['blub']
  539. 'blah'
  540. This works for all read operations and will raise a `TypeError` for
  541. methods that usually change data which isn't possible.
  542. From Werkzeug 0.3 onwards, the `KeyError` raised by this class is also a
  543. subclass of the :exc:`~exceptions.BadRequest` HTTP exception and will
  544. render a page for a ``400 BAD REQUEST`` if caught in a catch-all for HTTP
  545. exceptions.
  546. """
  547. def __reduce_ex__(self, protocol):
  548. return type(self), (self.dicts,)
  549. def __init__(self, dicts=None):
  550. self.dicts = list(dicts) or []
  551. @classmethod
  552. def fromkeys(cls, keys, value=None):
  553. raise TypeError(f"cannot create {cls.__name__!r} instances by fromkeys")
  554. def __getitem__(self, key):
  555. for d in self.dicts:
  556. if key in d:
  557. return d[key]
  558. raise exceptions.BadRequestKeyError(key)
  559. def get(self, key, default=None, type=None):
  560. for d in self.dicts:
  561. if key in d:
  562. if type is not None:
  563. try:
  564. return type(d[key])
  565. except ValueError:
  566. continue
  567. return d[key]
  568. return default
  569. def getlist(self, key, type=None):
  570. rv = []
  571. for d in self.dicts:
  572. rv.extend(d.getlist(key, type))
  573. return rv
  574. def _keys_impl(self):
  575. """This function exists so __len__ can be implemented more efficiently,
  576. saving one list creation from an iterator.
  577. """
  578. rv = set()
  579. rv.update(*self.dicts)
  580. return rv
  581. def keys(self):
  582. return self._keys_impl()
  583. def __iter__(self):
  584. return iter(self.keys())
  585. def items(self, multi=False):
  586. found = set()
  587. for d in self.dicts:
  588. for key, value in d.items(multi):
  589. if multi:
  590. yield key, value
  591. elif key not in found:
  592. found.add(key)
  593. yield key, value
  594. def values(self):
  595. for _key, value in self.items():
  596. yield value
  597. def lists(self):
  598. rv = {}
  599. for d in self.dicts:
  600. for key, values in d.lists():
  601. rv.setdefault(key, []).extend(values)
  602. return list(rv.items())
  603. def listvalues(self):
  604. return (x[1] for x in self.lists())
  605. def copy(self):
  606. """Return a shallow mutable copy of this object.
  607. This returns a :class:`MultiDict` representing the data at the
  608. time of copying. The copy will no longer reflect changes to the
  609. wrapped dicts.
  610. .. versionchanged:: 0.15
  611. Return a mutable :class:`MultiDict`.
  612. """
  613. return MultiDict(self)
  614. def to_dict(self, flat=True):
  615. """Return the contents as regular dict. If `flat` is `True` the
  616. returned dict will only have the first item present, if `flat` is
  617. `False` all values will be returned as lists.
  618. :param flat: If set to `False` the dict returned will have lists
  619. with all the values in it. Otherwise it will only
  620. contain the first item for each key.
  621. :return: a :class:`dict`
  622. """
  623. if flat:
  624. return dict(self.items())
  625. return dict(self.lists())
  626. def __len__(self):
  627. return len(self._keys_impl())
  628. def __contains__(self, key):
  629. for d in self.dicts:
  630. if key in d:
  631. return True
  632. return False
  633. def __repr__(self):
  634. return f"{type(self).__name__}({self.dicts!r})"
  635. class ImmutableDict(ImmutableDictMixin, dict):
  636. """An immutable :class:`dict`.
  637. .. versionadded:: 0.5
  638. """
  639. def __repr__(self):
  640. return f"{type(self).__name__}({dict.__repr__(self)})"
  641. def copy(self):
  642. """Return a shallow mutable copy of this object. Keep in mind that
  643. the standard library's :func:`copy` function is a no-op for this class
  644. like for any other python immutable type (eg: :class:`tuple`).
  645. """
  646. return dict(self)
  647. def __copy__(self):
  648. return self
  649. class ImmutableMultiDict(ImmutableMultiDictMixin, MultiDict):
  650. """An immutable :class:`MultiDict`.
  651. .. versionadded:: 0.5
  652. """
  653. def copy(self):
  654. """Return a shallow mutable copy of this object. Keep in mind that
  655. the standard library's :func:`copy` function is a no-op for this class
  656. like for any other python immutable type (eg: :class:`tuple`).
  657. """
  658. return MultiDict(self)
  659. def __copy__(self):
  660. return self
  661. class ImmutableOrderedMultiDict(ImmutableMultiDictMixin, OrderedMultiDict):
  662. """An immutable :class:`OrderedMultiDict`.
  663. .. versionadded:: 0.6
  664. """
  665. def _iter_hashitems(self):
  666. return enumerate(self.items(multi=True))
  667. def copy(self):
  668. """Return a shallow mutable copy of this object. Keep in mind that
  669. the standard library's :func:`copy` function is a no-op for this class
  670. like for any other python immutable type (eg: :class:`tuple`).
  671. """
  672. return OrderedMultiDict(self)
  673. def __copy__(self):
  674. return self
  675. class CallbackDict(UpdateDictMixin, dict):
  676. """A dict that calls a function passed every time something is changed.
  677. The function is passed the dict instance.
  678. """
  679. def __init__(self, initial=None, on_update=None):
  680. dict.__init__(self, initial or ())
  681. self.on_update = on_update
  682. def __repr__(self):
  683. return f"<{type(self).__name__} {dict.__repr__(self)}>"
  684. class HeaderSet(MutableSet):
  685. """Similar to the :class:`ETags` class this implements a set-like structure.
  686. Unlike :class:`ETags` this is case insensitive and used for vary, allow, and
  687. content-language headers.
  688. If not constructed using the :func:`parse_set_header` function the
  689. instantiation works like this:
  690. >>> hs = HeaderSet(['foo', 'bar', 'baz'])
  691. >>> hs
  692. HeaderSet(['foo', 'bar', 'baz'])
  693. """
  694. def __init__(self, headers=None, on_update=None):
  695. self._headers = list(headers or ())
  696. self._set = {x.lower() for x in self._headers}
  697. self.on_update = on_update
  698. def add(self, header):
  699. """Add a new header to the set."""
  700. self.update((header,))
  701. def remove(self, header):
  702. """Remove a header from the set. This raises an :exc:`KeyError` if the
  703. header is not in the set.
  704. .. versionchanged:: 0.5
  705. In older versions a :exc:`IndexError` was raised instead of a
  706. :exc:`KeyError` if the object was missing.
  707. :param header: the header to be removed.
  708. """
  709. key = header.lower()
  710. if key not in self._set:
  711. raise KeyError(header)
  712. self._set.remove(key)
  713. for idx, key in enumerate(self._headers):
  714. if key.lower() == header:
  715. del self._headers[idx]
  716. break
  717. if self.on_update is not None:
  718. self.on_update(self)
  719. def update(self, iterable):
  720. """Add all the headers from the iterable to the set.
  721. :param iterable: updates the set with the items from the iterable.
  722. """
  723. inserted_any = False
  724. for header in iterable:
  725. key = header.lower()
  726. if key not in self._set:
  727. self._headers.append(header)
  728. self._set.add(key)
  729. inserted_any = True
  730. if inserted_any and self.on_update is not None:
  731. self.on_update(self)
  732. def discard(self, header):
  733. """Like :meth:`remove` but ignores errors.
  734. :param header: the header to be discarded.
  735. """
  736. try:
  737. self.remove(header)
  738. except KeyError:
  739. pass
  740. def find(self, header):
  741. """Return the index of the header in the set or return -1 if not found.
  742. :param header: the header to be looked up.
  743. """
  744. header = header.lower()
  745. for idx, item in enumerate(self._headers):
  746. if item.lower() == header:
  747. return idx
  748. return -1
  749. def index(self, header):
  750. """Return the index of the header in the set or raise an
  751. :exc:`IndexError`.
  752. :param header: the header to be looked up.
  753. """
  754. rv = self.find(header)
  755. if rv < 0:
  756. raise IndexError(header)
  757. return rv
  758. def clear(self):
  759. """Clear the set."""
  760. self._set.clear()
  761. del self._headers[:]
  762. if self.on_update is not None:
  763. self.on_update(self)
  764. def as_set(self, preserve_casing=False):
  765. """Return the set as real python set type. When calling this, all
  766. the items are converted to lowercase and the ordering is lost.
  767. :param preserve_casing: if set to `True` the items in the set returned
  768. will have the original case like in the
  769. :class:`HeaderSet`, otherwise they will
  770. be lowercase.
  771. """
  772. if preserve_casing:
  773. return set(self._headers)
  774. return set(self._set)
  775. def to_header(self):
  776. """Convert the header set into an HTTP header string."""
  777. return ", ".join(map(http.quote_header_value, self._headers))
  778. def __getitem__(self, idx):
  779. return self._headers[idx]
  780. def __delitem__(self, idx):
  781. rv = self._headers.pop(idx)
  782. self._set.remove(rv.lower())
  783. if self.on_update is not None:
  784. self.on_update(self)
  785. def __setitem__(self, idx, value):
  786. old = self._headers[idx]
  787. self._set.remove(old.lower())
  788. self._headers[idx] = value
  789. self._set.add(value.lower())
  790. if self.on_update is not None:
  791. self.on_update(self)
  792. def __contains__(self, header):
  793. return header.lower() in self._set
  794. def __len__(self):
  795. return len(self._set)
  796. def __iter__(self):
  797. return iter(self._headers)
  798. def __bool__(self):
  799. return bool(self._set)
  800. def __str__(self):
  801. return self.to_header()
  802. def __repr__(self):
  803. return f"{type(self).__name__}({self._headers!r})"
  804. # circular dependencies
  805. from .. import http