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.
 
 
 
 

242 lines
8.0 KiB

  1. # Copyright (C) 2012-2023 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. import weakref
  22. from .utils import _SliceNormalizerMixIn, inheritdoc
  23. class ListProxy(_SliceNormalizerMixIn, list):
  24. """Implement the ``list`` interface by getting elements from a parent.
  25. This is created by a :class:`.SmartList` object when slicing. It does not
  26. actually store the list at any time; instead, whenever the list is needed,
  27. it builds it dynamically using the :meth:`_render` method.
  28. """
  29. __slots__ = ("__weakref__", "_parent", "_sliceinfo")
  30. def __init__(self, parent, sliceinfo):
  31. super().__init__()
  32. self._parent = parent
  33. self._sliceinfo = sliceinfo
  34. def __reduce_ex__(self, protocol: int) -> tuple:
  35. return (ListProxy, (self._parent, self._sliceinfo), ())
  36. def __setstate__(self, state: tuple) -> None:
  37. # Reregister with the parent
  38. child_ref = weakref.ref(self, self._parent._delete_child)
  39. self._parent._children[id(child_ref)] = (child_ref, self._sliceinfo)
  40. def __repr__(self):
  41. return repr(self._render())
  42. def __lt__(self, other):
  43. if isinstance(other, ListProxy):
  44. return self._render() < list(other)
  45. return self._render() < other
  46. def __le__(self, other):
  47. if isinstance(other, ListProxy):
  48. return self._render() <= list(other)
  49. return self._render() <= other
  50. def __eq__(self, other):
  51. if isinstance(other, ListProxy):
  52. return self._render() == list(other)
  53. return self._render() == other
  54. def __ne__(self, other):
  55. if isinstance(other, ListProxy):
  56. return self._render() != list(other)
  57. return self._render() != other
  58. def __gt__(self, other):
  59. if isinstance(other, ListProxy):
  60. return self._render() > list(other)
  61. return self._render() > other
  62. def __ge__(self, other):
  63. if isinstance(other, ListProxy):
  64. return self._render() >= list(other)
  65. return self._render() >= other
  66. def __bool__(self):
  67. return bool(self._render())
  68. def __len__(self):
  69. return max((self._stop - self._start) // self._step, 0)
  70. def __getitem__(self, key):
  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. return self._parent[adjusted]
  77. return self._render()[key]
  78. def __setitem__(self, key, item):
  79. if isinstance(key, slice):
  80. key = self._normalize_slice(key, clamp=True)
  81. keystart = min(self._start + key.start, self._stop)
  82. keystop = min(self._start + key.stop, self._stop)
  83. adjusted = slice(keystart, keystop, key.step)
  84. self._parent[adjusted] = item
  85. else:
  86. length = len(self)
  87. if key < 0:
  88. key = length + key
  89. if key < 0 or key >= length:
  90. raise IndexError("list assignment index out of range")
  91. self._parent[self._start + key] = item
  92. def __delitem__(self, key):
  93. if isinstance(key, slice):
  94. key = self._normalize_slice(key, clamp=True)
  95. keystart = min(self._start + key.start, self._stop)
  96. keystop = min(self._start + key.stop, self._stop)
  97. adjusted = slice(keystart, keystop, key.step)
  98. del self._parent[adjusted]
  99. else:
  100. length = len(self)
  101. if key < 0:
  102. key = length + key
  103. if key < 0 or key >= length:
  104. raise IndexError("list assignment index out of range")
  105. del self._parent[self._start + key]
  106. def __iter__(self):
  107. i = self._start
  108. while i < self._stop:
  109. yield self._parent[i]
  110. i += self._step
  111. def __reversed__(self):
  112. i = self._stop - 1
  113. while i >= self._start:
  114. yield self._parent[i]
  115. i -= self._step
  116. def __contains__(self, item):
  117. return item in self._render()
  118. def __add__(self, other):
  119. return type(self._parent)(list(self) + other)
  120. def __radd__(self, other):
  121. return type(self._parent)(other + list(self))
  122. def __iadd__(self, other):
  123. self.extend(other)
  124. return self
  125. def __mul__(self, other):
  126. return type(self._parent)(list(self) * other)
  127. def __rmul__(self, other):
  128. return type(self._parent)(other * list(self))
  129. def __imul__(self, other):
  130. self.extend(list(self) * (other - 1))
  131. return self
  132. @property
  133. def _start(self):
  134. """The starting index of this list, inclusive."""
  135. return self._sliceinfo[0]
  136. @property
  137. def _stop(self):
  138. """The ending index of this list, exclusive."""
  139. if self._sliceinfo[1] is None:
  140. return len(self._parent)
  141. return self._sliceinfo[1]
  142. @property
  143. def _step(self):
  144. """The number to increase the index by between items."""
  145. return self._sliceinfo[2]
  146. def _render(self):
  147. """Return the actual list from the stored start/stop/step."""
  148. return list(self._parent)[self._start : self._stop : self._step]
  149. @inheritdoc
  150. def append(self, item):
  151. self._parent.insert(self._stop, item)
  152. @inheritdoc
  153. def count(self, item):
  154. return self._render().count(item)
  155. @inheritdoc
  156. def index(self, item, start=None, stop=None):
  157. if start is not None:
  158. if stop is not None:
  159. return self._render().index(item, start, stop)
  160. return self._render().index(item, start)
  161. return self._render().index(item)
  162. @inheritdoc
  163. def extend(self, item):
  164. self._parent[self._stop : self._stop] = item
  165. @inheritdoc
  166. def insert(self, index, item):
  167. if index < 0:
  168. index = len(self) + index
  169. self._parent.insert(self._start + index, item)
  170. @inheritdoc
  171. def pop(self, index=None):
  172. length = len(self)
  173. if index is None:
  174. index = length - 1
  175. elif index < 0:
  176. index = length + index
  177. if index < 0 or index >= length:
  178. raise IndexError("pop index out of range")
  179. return self._parent.pop(self._start + index)
  180. @inheritdoc
  181. def remove(self, item):
  182. index = self.index(item)
  183. del self._parent[self._start + index]
  184. @inheritdoc
  185. def reverse(self):
  186. item = self._render()
  187. item.reverse()
  188. self._parent[self._start : self._stop : self._step] = item
  189. @inheritdoc
  190. def sort(self, key=None, reverse=None):
  191. item = self._render()
  192. kwargs = {}
  193. if key is not None:
  194. kwargs["key"] = key
  195. if reverse is not None:
  196. kwargs["reverse"] = reverse
  197. item.sort(**kwargs)
  198. self._parent[self._start : self._stop : self._step] = item