A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

list_proxy.py 7.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # Copyright (C) 2012-2020 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. # Copyright (C) 2019-2020 Yuri Astrakhan <YuriAstrakhan@gmail.com>
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. from .utils import _SliceNormalizerMixIn, inheritdoc
  22. class ListProxy(_SliceNormalizerMixIn, list):
  23. """Implement the ``list`` interface by getting elements from a parent.
  24. This is created by a :class:`.SmartList` object when slicing. It does not
  25. actually store the list at any time; instead, whenever the list is needed,
  26. it builds it dynamically using the :meth:`_render` method.
  27. """
  28. def __init__(self, parent, sliceinfo):
  29. super().__init__()
  30. self._parent = parent
  31. self._sliceinfo = sliceinfo
  32. def __repr__(self):
  33. return repr(self._render())
  34. def __lt__(self, other):
  35. if isinstance(other, ListProxy):
  36. return self._render() < list(other)
  37. return self._render() < other
  38. def __le__(self, other):
  39. if isinstance(other, ListProxy):
  40. return self._render() <= list(other)
  41. return self._render() <= other
  42. def __eq__(self, other):
  43. if isinstance(other, ListProxy):
  44. return self._render() == list(other)
  45. return self._render() == other
  46. def __ne__(self, other):
  47. if isinstance(other, ListProxy):
  48. return self._render() != list(other)
  49. return self._render() != other
  50. def __gt__(self, other):
  51. if isinstance(other, ListProxy):
  52. return self._render() > list(other)
  53. return self._render() > other
  54. def __ge__(self, other):
  55. if isinstance(other, ListProxy):
  56. return self._render() >= list(other)
  57. return self._render() >= other
  58. def __bool__(self):
  59. return bool(self._render())
  60. def __len__(self):
  61. return max((self._stop - self._start) // self._step, 0)
  62. def __getitem__(self, key):
  63. if isinstance(key, slice):
  64. key = self._normalize_slice(key, clamp=True)
  65. keystart = min(self._start + key.start, self._stop)
  66. keystop = min(self._start + key.stop, self._stop)
  67. adjusted = slice(keystart, keystop, key.step)
  68. return self._parent[adjusted]
  69. return self._render()[key]
  70. def __setitem__(self, key, item):
  71. if isinstance(key, slice):
  72. key = self._normalize_slice(key, clamp=True)
  73. keystart = min(self._start + key.start, self._stop)
  74. keystop = min(self._start + key.stop, self._stop)
  75. adjusted = slice(keystart, keystop, key.step)
  76. self._parent[adjusted] = item
  77. else:
  78. length = len(self)
  79. if key < 0:
  80. key = length + key
  81. if key < 0 or key >= length:
  82. raise IndexError("list assignment index out of range")
  83. self._parent[self._start + key] = item
  84. def __delitem__(self, key):
  85. if isinstance(key, slice):
  86. key = self._normalize_slice(key, clamp=True)
  87. keystart = min(self._start + key.start, self._stop)
  88. keystop = min(self._start + key.stop, self._stop)
  89. adjusted = slice(keystart, keystop, key.step)
  90. del self._parent[adjusted]
  91. else:
  92. length = len(self)
  93. if key < 0:
  94. key = length + key
  95. if key < 0 or key >= length:
  96. raise IndexError("list assignment index out of range")
  97. del self._parent[self._start + key]
  98. def __iter__(self):
  99. i = self._start
  100. while i < self._stop:
  101. yield self._parent[i]
  102. i += self._step
  103. def __reversed__(self):
  104. i = self._stop - 1
  105. while i >= self._start:
  106. yield self._parent[i]
  107. i -= self._step
  108. def __contains__(self, item):
  109. return item in self._render()
  110. def __add__(self, other):
  111. return type(self._parent)(list(self) + other)
  112. def __radd__(self, other):
  113. return type(self._parent)(other + list(self))
  114. def __iadd__(self, other):
  115. self.extend(other)
  116. return self
  117. def __mul__(self, other):
  118. return type(self._parent)(list(self) * other)
  119. def __rmul__(self, other):
  120. return type(self._parent)(other * list(self))
  121. def __imul__(self, other):
  122. self.extend(list(self) * (other - 1))
  123. return self
  124. @property
  125. def _start(self):
  126. """The starting index of this list, inclusive."""
  127. return self._sliceinfo[0]
  128. @property
  129. def _stop(self):
  130. """The ending index of this list, exclusive."""
  131. if self._sliceinfo[1] is None:
  132. return len(self._parent)
  133. return self._sliceinfo[1]
  134. @property
  135. def _step(self):
  136. """The number to increase the index by between items."""
  137. return self._sliceinfo[2]
  138. def _render(self):
  139. """Return the actual list from the stored start/stop/step."""
  140. return list(self._parent)[self._start : self._stop : self._step]
  141. @inheritdoc
  142. def append(self, item):
  143. self._parent.insert(self._stop, item)
  144. @inheritdoc
  145. def count(self, item):
  146. return self._render().count(item)
  147. @inheritdoc
  148. def index(self, item, start=None, stop=None):
  149. if start is not None:
  150. if stop is not None:
  151. return self._render().index(item, start, stop)
  152. return self._render().index(item, start)
  153. return self._render().index(item)
  154. @inheritdoc
  155. def extend(self, item):
  156. self._parent[self._stop : self._stop] = item
  157. @inheritdoc
  158. def insert(self, index, item):
  159. if index < 0:
  160. index = len(self) + index
  161. self._parent.insert(self._start + index, item)
  162. @inheritdoc
  163. def pop(self, index=None):
  164. length = len(self)
  165. if index is None:
  166. index = length - 1
  167. elif index < 0:
  168. index = length + index
  169. if index < 0 or index >= length:
  170. raise IndexError("pop index out of range")
  171. return self._parent.pop(self._start + index)
  172. @inheritdoc
  173. def remove(self, item):
  174. index = self.index(item)
  175. del self._parent[self._start + index]
  176. @inheritdoc
  177. def reverse(self):
  178. item = self._render()
  179. item.reverse()
  180. self._parent[self._start : self._stop : self._step] = item
  181. @inheritdoc
  182. def sort(self, key=None, reverse=None):
  183. item = self._render()
  184. kwargs = {}
  185. if key is not None:
  186. kwargs["key"] = key
  187. if reverse is not None:
  188. kwargs["reverse"] = reverse
  189. item.sort(**kwargs)
  190. self._parent[self._start : self._stop : self._step] = item