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.
 
 
 
 

370 lines
12 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2013 Ben Kurtovic <ben.kurtovic@verizon.net>
  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. """
  23. This module contains the :py:class:`~.SmartList` type, as well as its
  24. :py:class:`~._ListProxy` child, which together implement a list whose sublists
  25. reflect changes made to the main list, and vice-versa.
  26. """
  27. from __future__ import unicode_literals
  28. from .compat import maxsize, py3k
  29. __all__ = ["SmartList"]
  30. def inheritdoc(method):
  31. """Set __doc__ of *method* to __doc__ of *method* in its parent class.
  32. Since this is used on :py:class:`~.SmartList`, the "parent class" used is
  33. ``list``. This function can be used as a decorator.
  34. """
  35. method.__doc__ = getattr(list, method.__name__).__doc__
  36. return method
  37. class SmartList(list):
  38. """Implements the ``list`` interface with special handling of sublists.
  39. When a sublist is created (by ``list[i:j]``), any changes made to this
  40. list (such as the addition, removal, or replacement of elements) will be
  41. reflected in the sublist, or vice-versa, to the greatest degree possible.
  42. This is implemented by having sublists - instances of the
  43. :py:class:`~._ListProxy` type - dynamically determine their elements by
  44. storing their slice info and retrieving that slice from the parent. Methods
  45. that change the size of the list also change the slice info. For example::
  46. >>> parent = SmartList([0, 1, 2, 3])
  47. >>> parent
  48. [0, 1, 2, 3]
  49. >>> child = parent[2:]
  50. >>> child
  51. [2, 3]
  52. >>> child.append(4)
  53. >>> child
  54. [2, 3, 4]
  55. >>> parent
  56. [0, 1, 2, 3, 4]
  57. """
  58. def __init__(self, iterable=None):
  59. if iterable:
  60. super(SmartList, self).__init__(iterable)
  61. else:
  62. super(SmartList, self).__init__()
  63. self._children = {}
  64. def __getitem__(self, key):
  65. if not isinstance(key, slice):
  66. return super(SmartList, self).__getitem__(key)
  67. sliceinfo = [key.start or 0, key.stop or maxsize, key.step or 1]
  68. child = _ListProxy(self, sliceinfo)
  69. self._children[id(child)] = (child, sliceinfo)
  70. return child
  71. def __setitem__(self, key, item):
  72. if not isinstance(key, slice):
  73. return super(SmartList, self).__setitem__(key, item)
  74. item = list(item)
  75. super(SmartList, self).__setitem__(key, item)
  76. key = slice(key.start or 0, key.stop or maxsize)
  77. diff = len(item) - key.stop + key.start
  78. values = self._children.values if py3k else self._children.itervalues
  79. if diff:
  80. for child, (start, stop, step) in values():
  81. if start >= key.stop:
  82. self._children[id(child)][1][0] += diff
  83. if stop >= key.stop and stop != maxsize:
  84. self._children[id(child)][1][1] += diff
  85. def __delitem__(self, key):
  86. super(SmartList, self).__delitem__(key)
  87. if isinstance(key, slice):
  88. key = slice(key.start or 0, key.stop or maxsize)
  89. else:
  90. key = slice(key, key + 1)
  91. diff = key.stop - key.start
  92. values = self._children.values if py3k else self._children.itervalues
  93. for child, (start, stop, step) in values():
  94. if start > key.start:
  95. self._children[id(child)][1][0] -= diff
  96. if stop >= key.stop:
  97. self._children[id(child)][1][1] -= diff
  98. if not py3k:
  99. def __getslice__(self, start, stop):
  100. return self.__getitem__(slice(start, stop))
  101. def __setslice__(self, start, stop, iterable):
  102. self.__setitem__(slice(start, stop), iterable)
  103. def __delslice__(self, start, stop):
  104. self.__delitem__(slice(start, stop))
  105. def __add__(self, other):
  106. return SmartList(list(self) + other)
  107. def __radd__(self, other):
  108. return SmartList(other + list(self))
  109. def __iadd__(self, other):
  110. self.extend(other)
  111. return self
  112. @inheritdoc
  113. def append(self, item):
  114. head = len(self)
  115. self[head:head] = [item]
  116. @inheritdoc
  117. def extend(self, item):
  118. head = len(self)
  119. self[head:head] = item
  120. @inheritdoc
  121. def insert(self, index, item):
  122. self[index:index] = [item]
  123. @inheritdoc
  124. def pop(self, index=None):
  125. if index is None:
  126. index = len(self) - 1
  127. item = self[index]
  128. del self[index]
  129. return item
  130. @inheritdoc
  131. def remove(self, item):
  132. del self[self.index(item)]
  133. @inheritdoc
  134. def reverse(self):
  135. copy = list(self)
  136. for child in self._children:
  137. child._parent = copy
  138. super(SmartList, self).reverse()
  139. @inheritdoc
  140. def sort(self, cmp=None, key=None, reverse=None):
  141. copy = list(self)
  142. for child in self._children:
  143. child._parent = copy
  144. if cmp is not None:
  145. if key is not None:
  146. if reverse is not None:
  147. super(SmartList, self).sort(cmp, key, reverse)
  148. else:
  149. super(SmartList, self).sort(cmp, key)
  150. else:
  151. super(SmartList, self).sort(cmp)
  152. else:
  153. super(SmartList, self).sort()
  154. class _ListProxy(list):
  155. """Implement the ``list`` interface by getting elements from a parent.
  156. This is created by a :py:class:`~.SmartList` object when slicing. It does
  157. not actually store the list at any time; instead, whenever the list is
  158. needed, it builds it dynamically using the :py:meth:`_render` method.
  159. """
  160. def __init__(self, parent, sliceinfo):
  161. super(_ListProxy, self).__init__()
  162. self._parent = parent
  163. self._sliceinfo = sliceinfo
  164. def __repr__(self):
  165. return repr(self._render())
  166. def __lt__(self, other):
  167. if isinstance(other, _ListProxy):
  168. return self._render() < list(other)
  169. return self._render() < other
  170. def __le__(self, other):
  171. if isinstance(other, _ListProxy):
  172. return self._render() <= list(other)
  173. return self._render() <= other
  174. def __eq__(self, other):
  175. if isinstance(other, _ListProxy):
  176. return self._render() == list(other)
  177. return self._render() == other
  178. def __ne__(self, other):
  179. if isinstance(other, _ListProxy):
  180. return self._render() != list(other)
  181. return self._render() != other
  182. def __gt__(self, other):
  183. if isinstance(other, _ListProxy):
  184. return self._render() > list(other)
  185. return self._render() > other
  186. def __ge__(self, other):
  187. if isinstance(other, _ListProxy):
  188. return self._render() >= list(other)
  189. return self._render() >= other
  190. if py3k:
  191. def __bool__(self):
  192. return bool(self._render())
  193. else:
  194. def __nonzero__(self):
  195. return bool(self._render())
  196. def __len__(self):
  197. return (self._stop - self._start) / self._step
  198. def __getitem__(self, key):
  199. return self._render()[key]
  200. def __setitem__(self, key, item):
  201. if isinstance(key, slice):
  202. adjusted = slice(key.start + self._start, key.stop + self._stop,
  203. key.step)
  204. self._parent[adjusted] = item
  205. else:
  206. self._parent[self._start + key] = item
  207. def __delitem__(self, key):
  208. if isinstance(key, slice):
  209. adjusted = slice(key.start + self._start, key.stop + self._stop,
  210. key.step)
  211. del self._parent[adjusted]
  212. else:
  213. del self._parent[self._start + key]
  214. def __iter__(self):
  215. i = self._start
  216. while i < self._stop:
  217. yield self._parent[i]
  218. i += self._step
  219. def __reversed__(self):
  220. i = self._stop - 1
  221. while i >= self._start:
  222. yield self._parent[i]
  223. i -= self._step
  224. def __contains__(self, item):
  225. return item in self._render()
  226. if not py3k:
  227. def __getslice__(self, start, stop):
  228. return self.__getitem__(slice(start, stop))
  229. def __setslice__(self, start, stop, iterable):
  230. self.__setitem__(slice(start, stop), iterable)
  231. def __delslice__(self, start, stop):
  232. self.__delitem__(slice(start, stop))
  233. def __add__(self, other):
  234. return SmartList(list(self) + other)
  235. def __radd__(self, other):
  236. return SmartList(other + list(self))
  237. def __iadd__(self, other):
  238. self.extend(other)
  239. return self
  240. @property
  241. def _start(self):
  242. """The starting index of this list, inclusive."""
  243. return self._sliceinfo[0]
  244. @property
  245. def _stop(self):
  246. """The ending index of this list, exclusive."""
  247. return self._sliceinfo[1]
  248. @property
  249. def _step(self):
  250. """The number to increase the index by between items."""
  251. return self._sliceinfo[2]
  252. def _render(self):
  253. """Return the actual list from the stored start/stop/step."""
  254. return list(self._parent)[self._start:self._stop:self._step]
  255. @inheritdoc
  256. def append(self, item):
  257. self._parent.insert(self._stop, item)
  258. @inheritdoc
  259. def count(self, item):
  260. return self._render().count(item)
  261. @inheritdoc
  262. def index(self, item, start=None, stop=None):
  263. if start is not None:
  264. if stop is not None:
  265. return self._render().index(item, start, stop)
  266. return self._render().index(item, start)
  267. return self._render().index(item)
  268. @inheritdoc
  269. def extend(self, item):
  270. self._parent[self._stop:self._stop] = item
  271. @inheritdoc
  272. def insert(self, index, item):
  273. self._parent.insert(self._start + index, item)
  274. @inheritdoc
  275. def pop(self, index=None):
  276. if index is None:
  277. index = len(self) - 1
  278. return self._parent.pop(self._start + index)
  279. @inheritdoc
  280. def remove(self, item):
  281. index = self.index(item)
  282. del self._parent[index]
  283. @inheritdoc
  284. def reverse(self):
  285. item = self._render()
  286. item.reverse()
  287. self._parent[self._start:self._stop:self._step] = item
  288. @inheritdoc
  289. def sort(self, cmp=None, key=None, reverse=None):
  290. item = self._render()
  291. if cmp is not None:
  292. if key is not None:
  293. if reverse is not None:
  294. item.sort(cmp, key, reverse)
  295. else:
  296. item.sort(cmp, key)
  297. else:
  298. item.sort(cmp)
  299. else:
  300. item.sort()
  301. self._parent[self._start:self._stop:self._step] = item
  302. del inheritdoc