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.
 
 
 
 

231 lines
7.5 KiB

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