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