_multidict_py.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. import sys
  2. from array import array
  3. from collections import abc
  4. from ._abc import MultiMapping, MutableMultiMapping
  5. _marker = object()
  6. class istr(str):
  7. """Case insensitive str."""
  8. __is_istr__ = True
  9. upstr = istr # for relaxing backward compatibility problems
  10. def getversion(md):
  11. if not isinstance(md, _Base):
  12. raise TypeError("Parameter should be multidict or proxy")
  13. return md._impl._version
  14. _version = array("Q", [0])
  15. class _Impl:
  16. __slots__ = ("_items", "_version")
  17. def __init__(self):
  18. self._items = []
  19. self.incr_version()
  20. def incr_version(self):
  21. global _version
  22. v = _version
  23. v[0] += 1
  24. self._version = v[0]
  25. if sys.implementation.name != "pypy":
  26. def __sizeof__(self):
  27. return object.__sizeof__(self) + sys.getsizeof(self._items)
  28. class _Base:
  29. def _title(self, key):
  30. return key
  31. def getall(self, key, default=_marker):
  32. """Return a list of all values matching the key."""
  33. identity = self._title(key)
  34. res = [v for i, k, v in self._impl._items if i == identity]
  35. if res:
  36. return res
  37. if not res and default is not _marker:
  38. return default
  39. raise KeyError("Key not found: %r" % key)
  40. def getone(self, key, default=_marker):
  41. """Get first value matching the key."""
  42. identity = self._title(key)
  43. for i, k, v in self._impl._items:
  44. if i == identity:
  45. return v
  46. if default is not _marker:
  47. return default
  48. raise KeyError("Key not found: %r" % key)
  49. # Mapping interface #
  50. def __getitem__(self, key):
  51. return self.getone(key)
  52. def get(self, key, default=None):
  53. """Get first value matching the key.
  54. The method is alias for .getone().
  55. """
  56. return self.getone(key, default)
  57. def __iter__(self):
  58. return iter(self.keys())
  59. def __len__(self):
  60. return len(self._impl._items)
  61. def keys(self):
  62. """Return a new view of the dictionary's keys."""
  63. return _KeysView(self._impl)
  64. def items(self):
  65. """Return a new view of the dictionary's items *(key, value) pairs)."""
  66. return _ItemsView(self._impl)
  67. def values(self):
  68. """Return a new view of the dictionary's values."""
  69. return _ValuesView(self._impl)
  70. def __eq__(self, other):
  71. if not isinstance(other, abc.Mapping):
  72. return NotImplemented
  73. if isinstance(other, _Base):
  74. lft = self._impl._items
  75. rht = other._impl._items
  76. if len(lft) != len(rht):
  77. return False
  78. for (i1, k2, v1), (i2, k2, v2) in zip(lft, rht):
  79. if i1 != i2 or v1 != v2:
  80. return False
  81. return True
  82. if len(self._impl._items) != len(other):
  83. return False
  84. for k, v in self.items():
  85. nv = other.get(k, _marker)
  86. if v != nv:
  87. return False
  88. return True
  89. def __contains__(self, key):
  90. identity = self._title(key)
  91. for i, k, v in self._impl._items:
  92. if i == identity:
  93. return True
  94. return False
  95. def __repr__(self):
  96. body = ", ".join("'{}': {!r}".format(k, v) for k, v in self.items())
  97. return "<{}({})>".format(self.__class__.__name__, body)
  98. class MultiDictProxy(_Base, MultiMapping):
  99. """Read-only proxy for MultiDict instance."""
  100. def __init__(self, arg):
  101. if not isinstance(arg, (MultiDict, MultiDictProxy)):
  102. raise TypeError(
  103. "ctor requires MultiDict or MultiDictProxy instance"
  104. ", not {}".format(type(arg))
  105. )
  106. self._impl = arg._impl
  107. def __reduce__(self):
  108. raise TypeError("can't pickle {} objects".format(self.__class__.__name__))
  109. def copy(self):
  110. """Return a copy of itself."""
  111. return MultiDict(self.items())
  112. class CIMultiDictProxy(MultiDictProxy):
  113. """Read-only proxy for CIMultiDict instance."""
  114. def __init__(self, arg):
  115. if not isinstance(arg, (CIMultiDict, CIMultiDictProxy)):
  116. raise TypeError(
  117. "ctor requires CIMultiDict or CIMultiDictProxy instance"
  118. ", not {}".format(type(arg))
  119. )
  120. self._impl = arg._impl
  121. def _title(self, key):
  122. return key.title()
  123. def copy(self):
  124. """Return a copy of itself."""
  125. return CIMultiDict(self.items())
  126. class MultiDict(_Base, MutableMultiMapping):
  127. """Dictionary with the support for duplicate keys."""
  128. def __init__(self, *args, **kwargs):
  129. self._impl = _Impl()
  130. self._extend(args, kwargs, self.__class__.__name__, self._extend_items)
  131. if sys.implementation.name != "pypy":
  132. def __sizeof__(self):
  133. return object.__sizeof__(self) + sys.getsizeof(self._impl)
  134. def __reduce__(self):
  135. return (self.__class__, (list(self.items()),))
  136. def _title(self, key):
  137. return key
  138. def _key(self, key):
  139. if isinstance(key, str):
  140. return key
  141. else:
  142. raise TypeError(
  143. "MultiDict keys should be either str " "or subclasses of str"
  144. )
  145. def add(self, key, value):
  146. identity = self._title(key)
  147. self._impl._items.append((identity, self._key(key), value))
  148. self._impl.incr_version()
  149. def copy(self):
  150. """Return a copy of itself."""
  151. cls = self.__class__
  152. return cls(self.items())
  153. __copy__ = copy
  154. def extend(self, *args, **kwargs):
  155. """Extend current MultiDict with more values.
  156. This method must be used instead of update.
  157. """
  158. self._extend(args, kwargs, "extend", self._extend_items)
  159. def _extend(self, args, kwargs, name, method):
  160. if len(args) > 1:
  161. raise TypeError(
  162. "{} takes at most 1 positional argument"
  163. " ({} given)".format(name, len(args))
  164. )
  165. if args:
  166. arg = args[0]
  167. if isinstance(args[0], (MultiDict, MultiDictProxy)) and not kwargs:
  168. items = arg._impl._items
  169. else:
  170. if hasattr(arg, "items"):
  171. arg = arg.items()
  172. if kwargs:
  173. arg = list(arg)
  174. arg.extend(list(kwargs.items()))
  175. items = []
  176. for item in arg:
  177. if not len(item) == 2:
  178. raise TypeError(
  179. "{} takes either dict or list of (key, value) "
  180. "tuples".format(name)
  181. )
  182. items.append((self._title(item[0]), self._key(item[0]), item[1]))
  183. method(items)
  184. else:
  185. method(
  186. [
  187. (self._title(key), self._key(key), value)
  188. for key, value in kwargs.items()
  189. ]
  190. )
  191. def _extend_items(self, items):
  192. for identity, key, value in items:
  193. self.add(key, value)
  194. def clear(self):
  195. """Remove all items from MultiDict."""
  196. self._impl._items.clear()
  197. self._impl.incr_version()
  198. # Mapping interface #
  199. def __setitem__(self, key, value):
  200. self._replace(key, value)
  201. def __delitem__(self, key):
  202. identity = self._title(key)
  203. items = self._impl._items
  204. found = False
  205. for i in range(len(items) - 1, -1, -1):
  206. if items[i][0] == identity:
  207. del items[i]
  208. found = True
  209. if not found:
  210. raise KeyError(key)
  211. else:
  212. self._impl.incr_version()
  213. def setdefault(self, key, default=None):
  214. """Return value for key, set value to default if key is not present."""
  215. identity = self._title(key)
  216. for i, k, v in self._impl._items:
  217. if i == identity:
  218. return v
  219. self.add(key, default)
  220. return default
  221. def popone(self, key, default=_marker):
  222. """Remove specified key and return the corresponding value.
  223. If key is not found, d is returned if given, otherwise
  224. KeyError is raised.
  225. """
  226. identity = self._title(key)
  227. for i in range(len(self._impl._items)):
  228. if self._impl._items[i][0] == identity:
  229. value = self._impl._items[i][2]
  230. del self._impl._items[i]
  231. self._impl.incr_version()
  232. return value
  233. if default is _marker:
  234. raise KeyError(key)
  235. else:
  236. return default
  237. pop = popone # type: ignore
  238. def popall(self, key, default=_marker):
  239. """Remove all occurrences of key and return the list of corresponding
  240. values.
  241. If key is not found, default is returned if given, otherwise
  242. KeyError is raised.
  243. """
  244. found = False
  245. identity = self._title(key)
  246. ret = []
  247. for i in range(len(self._impl._items) - 1, -1, -1):
  248. item = self._impl._items[i]
  249. if item[0] == identity:
  250. ret.append(item[2])
  251. del self._impl._items[i]
  252. self._impl.incr_version()
  253. found = True
  254. if not found:
  255. if default is _marker:
  256. raise KeyError(key)
  257. else:
  258. return default
  259. else:
  260. ret.reverse()
  261. return ret
  262. def popitem(self):
  263. """Remove and return an arbitrary (key, value) pair."""
  264. if self._impl._items:
  265. i = self._impl._items.pop(0)
  266. self._impl.incr_version()
  267. return i[1], i[2]
  268. else:
  269. raise KeyError("empty multidict")
  270. def update(self, *args, **kwargs):
  271. """Update the dictionary from *other*, overwriting existing keys."""
  272. self._extend(args, kwargs, "update", self._update_items)
  273. def _update_items(self, items):
  274. if not items:
  275. return
  276. used_keys = {}
  277. for identity, key, value in items:
  278. start = used_keys.get(identity, 0)
  279. for i in range(start, len(self._impl._items)):
  280. item = self._impl._items[i]
  281. if item[0] == identity:
  282. used_keys[identity] = i + 1
  283. self._impl._items[i] = (identity, key, value)
  284. break
  285. else:
  286. self._impl._items.append((identity, key, value))
  287. used_keys[identity] = len(self._impl._items)
  288. # drop tails
  289. i = 0
  290. while i < len(self._impl._items):
  291. item = self._impl._items[i]
  292. identity = item[0]
  293. pos = used_keys.get(identity)
  294. if pos is None:
  295. i += 1
  296. continue
  297. if i >= pos:
  298. del self._impl._items[i]
  299. else:
  300. i += 1
  301. self._impl.incr_version()
  302. def _replace(self, key, value):
  303. key = self._key(key)
  304. identity = self._title(key)
  305. items = self._impl._items
  306. for i in range(len(items)):
  307. item = items[i]
  308. if item[0] == identity:
  309. items[i] = (identity, key, value)
  310. # i points to last found item
  311. rgt = i
  312. self._impl.incr_version()
  313. break
  314. else:
  315. self._impl._items.append((identity, key, value))
  316. self._impl.incr_version()
  317. return
  318. # remove all tail items
  319. i = rgt + 1
  320. while i < len(items):
  321. item = items[i]
  322. if item[0] == identity:
  323. del items[i]
  324. else:
  325. i += 1
  326. class CIMultiDict(MultiDict):
  327. """Dictionary with the support for duplicate case-insensitive keys."""
  328. def _title(self, key):
  329. return key.title()
  330. class _Iter:
  331. __slots__ = ("_size", "_iter")
  332. def __init__(self, size, iterator):
  333. self._size = size
  334. self._iter = iterator
  335. def __iter__(self):
  336. return self
  337. def __next__(self):
  338. return next(self._iter)
  339. def __length_hint__(self):
  340. return self._size
  341. class _ViewBase:
  342. def __init__(self, impl):
  343. self._impl = impl
  344. self._version = impl._version
  345. def __len__(self):
  346. return len(self._impl._items)
  347. class _ItemsView(_ViewBase, abc.ItemsView):
  348. def __contains__(self, item):
  349. assert isinstance(item, tuple) or isinstance(item, list)
  350. assert len(item) == 2
  351. for i, k, v in self._impl._items:
  352. if item[0] == k and item[1] == v:
  353. return True
  354. return False
  355. def __iter__(self):
  356. return _Iter(len(self), self._iter())
  357. def _iter(self):
  358. for i, k, v in self._impl._items:
  359. if self._version != self._impl._version:
  360. raise RuntimeError("Dictionary changed during iteration")
  361. yield k, v
  362. def __repr__(self):
  363. lst = []
  364. for item in self._impl._items:
  365. lst.append("{!r}: {!r}".format(item[1], item[2]))
  366. body = ", ".join(lst)
  367. return "{}({})".format(self.__class__.__name__, body)
  368. class _ValuesView(_ViewBase, abc.ValuesView):
  369. def __contains__(self, value):
  370. for item in self._impl._items:
  371. if item[2] == value:
  372. return True
  373. return False
  374. def __iter__(self):
  375. return _Iter(len(self), self._iter())
  376. def _iter(self):
  377. for item in self._impl._items:
  378. if self._version != self._impl._version:
  379. raise RuntimeError("Dictionary changed during iteration")
  380. yield item[2]
  381. def __repr__(self):
  382. lst = []
  383. for item in self._impl._items:
  384. lst.append("{!r}".format(item[2]))
  385. body = ", ".join(lst)
  386. return "{}({})".format(self.__class__.__name__, body)
  387. class _KeysView(_ViewBase, abc.KeysView):
  388. def __contains__(self, key):
  389. for item in self._impl._items:
  390. if item[1] == key:
  391. return True
  392. return False
  393. def __iter__(self):
  394. return _Iter(len(self), self._iter())
  395. def _iter(self):
  396. for item in self._impl._items:
  397. if self._version != self._impl._version:
  398. raise RuntimeError("Dictionary changed during iteration")
  399. yield item[1]
  400. def __repr__(self):
  401. lst = []
  402. for item in self._impl._items:
  403. lst.append("{!r}".format(item[1]))
  404. body = ", ".join(lst)
  405. return "{}({})".format(self.__class__.__name__, body)