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.
 
 
 
 

406 lines
13 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. keystop = maxsize if key.stop is None else key.stop
  68. sliceinfo = [key.start or 0, keystop, key.step or 1]
  69. child = _ListProxy(self, sliceinfo)
  70. self._children[id(child)] = (child, sliceinfo)
  71. return child
  72. def __setitem__(self, key, item):
  73. if not isinstance(key, slice):
  74. return super(SmartList, self).__setitem__(key, item)
  75. item = list(item)
  76. super(SmartList, self).__setitem__(key, item)
  77. keystop = maxsize if key.stop is None else key.stop
  78. key = slice(key.start or 0, keystop, key.step or 1)
  79. diff = len(item) + (key.start - key.stop) / key.step
  80. values = self._children.values if py3k else self._children.itervalues
  81. if diff:
  82. for child, (start, stop, step) in values():
  83. if start > key.stop:
  84. self._children[id(child)][1][0] += diff
  85. if stop >= key.stop and stop != maxsize:
  86. self._children[id(child)][1][1] += diff
  87. def __delitem__(self, key):
  88. super(SmartList, self).__delitem__(key)
  89. if isinstance(key, slice):
  90. keystop = maxsize if key.stop is None else key.stop
  91. key = slice(key.start or 0, keystop, key.step or 1)
  92. else:
  93. key = slice(key, key + 1, 1)
  94. diff = (key.stop - key.start) / key.step
  95. values = self._children.values if py3k else self._children.itervalues
  96. for child, (start, stop, step) in values():
  97. if start > key.start:
  98. self._children[id(child)][1][0] -= diff
  99. if stop >= key.stop and stop != maxsize:
  100. self._children[id(child)][1][1] -= diff
  101. if not py3k:
  102. def __getslice__(self, start, stop):
  103. return self.__getitem__(slice(start, stop))
  104. def __setslice__(self, start, stop, iterable):
  105. self.__setitem__(slice(start, stop), iterable)
  106. def __delslice__(self, start, stop):
  107. self.__delitem__(slice(start, stop))
  108. def __add__(self, other):
  109. return SmartList(list(self) + other)
  110. def __radd__(self, other):
  111. return SmartList(other + list(self))
  112. def __iadd__(self, other):
  113. self.extend(other)
  114. return self
  115. @inheritdoc
  116. def append(self, item):
  117. head = len(self)
  118. self[head:head] = [item]
  119. @inheritdoc
  120. def extend(self, item):
  121. head = len(self)
  122. self[head:head] = item
  123. @inheritdoc
  124. def insert(self, index, item):
  125. self[index:index] = [item]
  126. @inheritdoc
  127. def pop(self, index=None):
  128. if index is None:
  129. index = len(self) - 1
  130. item = self[index]
  131. del self[index]
  132. return item
  133. @inheritdoc
  134. def remove(self, item):
  135. del self[self.index(item)]
  136. @inheritdoc
  137. def reverse(self):
  138. copy = list(self)
  139. for child in self._children:
  140. child._parent = copy
  141. super(SmartList, self).reverse()
  142. @inheritdoc
  143. def sort(self, cmp=None, key=None, reverse=None):
  144. copy = list(self)
  145. for child in self._children:
  146. child._parent = copy
  147. kwargs = {}
  148. if cmp is not None:
  149. kwargs["cmp"] = cmp
  150. if key is not None:
  151. kwargs["key"] = key
  152. if reverse is not None:
  153. kwargs["reverse"] = reverse
  154. super(SmartList, self).sort(**kwargs)
  155. class _ListProxy(list):
  156. """Implement the ``list`` interface by getting elements from a parent.
  157. This is created by a :py:class:`~.SmartList` object when slicing. It does
  158. not actually store the list at any time; instead, whenever the list is
  159. needed, it builds it dynamically using the :py:meth:`_render` method.
  160. """
  161. def __init__(self, parent, sliceinfo):
  162. super(_ListProxy, self).__init__()
  163. self._parent = parent
  164. self._sliceinfo = sliceinfo
  165. def __repr__(self):
  166. return repr(self._render())
  167. def __lt__(self, other):
  168. if isinstance(other, _ListProxy):
  169. return self._render() < list(other)
  170. return self._render() < other
  171. def __le__(self, other):
  172. if isinstance(other, _ListProxy):
  173. return self._render() <= list(other)
  174. return self._render() <= other
  175. def __eq__(self, other):
  176. if isinstance(other, _ListProxy):
  177. return self._render() == list(other)
  178. return self._render() == other
  179. def __ne__(self, other):
  180. if isinstance(other, _ListProxy):
  181. return self._render() != list(other)
  182. return self._render() != other
  183. def __gt__(self, other):
  184. if isinstance(other, _ListProxy):
  185. return self._render() > list(other)
  186. return self._render() > other
  187. def __ge__(self, other):
  188. if isinstance(other, _ListProxy):
  189. return self._render() >= list(other)
  190. return self._render() >= other
  191. if py3k:
  192. def __bool__(self):
  193. return bool(self._render())
  194. else:
  195. def __nonzero__(self):
  196. return bool(self._render())
  197. def __len__(self):
  198. return (self._stop - self._start) / self._step
  199. def __getitem__(self, key):
  200. return self._render()[key]
  201. def __setitem__(self, key, item):
  202. if isinstance(key, slice):
  203. keystart = (key.start or 0) + self._start
  204. if key.stop is None or key.stop == maxsize:
  205. keystop = self._stop
  206. else:
  207. keystop = key.stop + self._start
  208. adjusted = slice(keystart, keystop, key.step)
  209. self._parent[adjusted] = item
  210. else:
  211. length = len(self)
  212. if key < 0:
  213. key = length + key
  214. if key < 0 or key >= length:
  215. raise IndexError("list assignment index out of range")
  216. self._parent[self._start + key] = item
  217. def __delitem__(self, key):
  218. if isinstance(key, slice):
  219. keystart = (key.start or 0) + self._start
  220. if key.stop is None or key.stop == maxsize:
  221. keystop = self._stop
  222. else:
  223. keystop = key.stop + self._start
  224. adjusted = slice(keystart, keystop, key.step)
  225. del self._parent[adjusted]
  226. else:
  227. length = len(self)
  228. if key < 0:
  229. key = length + key
  230. if key < 0 or key >= length:
  231. raise IndexError("list assignment index out of range")
  232. del self._parent[self._start + key]
  233. def __iter__(self):
  234. i = self._start
  235. while i < self._stop:
  236. yield self._parent[i]
  237. i += self._step
  238. def __reversed__(self):
  239. i = self._stop - 1
  240. while i >= self._start:
  241. yield self._parent[i]
  242. i -= self._step
  243. def __contains__(self, item):
  244. return item in self._render()
  245. if not py3k:
  246. def __getslice__(self, start, stop):
  247. return self.__getitem__(slice(start, stop))
  248. def __setslice__(self, start, stop, iterable):
  249. self.__setitem__(slice(start, stop), iterable)
  250. def __delslice__(self, start, stop):
  251. self.__delitem__(slice(start, stop))
  252. def __add__(self, other):
  253. return SmartList(list(self) + other)
  254. def __radd__(self, other):
  255. return SmartList(other + list(self))
  256. def __iadd__(self, other):
  257. self.extend(other)
  258. return self
  259. def __mul__(self, other):
  260. return SmartList(list(self) * other)
  261. def __rmul__(self, other):
  262. return SmartList(other * list(self))
  263. def __imul__(self, other):
  264. self.extend(list(self) * (other - 1))
  265. return self
  266. @property
  267. def _start(self):
  268. """The starting index of this list, inclusive."""
  269. return self._sliceinfo[0]
  270. @property
  271. def _stop(self):
  272. """The ending index of this list, exclusive."""
  273. if self._sliceinfo[1] == maxsize:
  274. return len(self._parent)
  275. return self._sliceinfo[1]
  276. @property
  277. def _step(self):
  278. """The number to increase the index by between items."""
  279. return self._sliceinfo[2]
  280. def _render(self):
  281. """Return the actual list from the stored start/stop/step."""
  282. return list(self._parent)[self._start:self._stop:self._step]
  283. @inheritdoc
  284. def append(self, item):
  285. self._parent.insert(self._stop, item)
  286. @inheritdoc
  287. def count(self, item):
  288. return self._render().count(item)
  289. @inheritdoc
  290. def index(self, item, start=None, stop=None):
  291. if start is not None:
  292. if stop is not None:
  293. return self._render().index(item, start, stop)
  294. return self._render().index(item, start)
  295. return self._render().index(item)
  296. @inheritdoc
  297. def extend(self, item):
  298. self._parent[self._stop:self._stop] = item
  299. @inheritdoc
  300. def insert(self, index, item):
  301. if index < 0:
  302. index = len(self) + index
  303. self._parent.insert(self._start + index, item)
  304. @inheritdoc
  305. def pop(self, index=None):
  306. length = len(self)
  307. if index is None:
  308. index = length - 1
  309. elif index < 0:
  310. index = length + index
  311. if index < 0 or index >= length:
  312. raise IndexError("pop index out of range")
  313. return self._parent.pop(self._start + index)
  314. @inheritdoc
  315. def remove(self, item):
  316. index = self.index(item)
  317. del self._parent[self._start + index]
  318. @inheritdoc
  319. def reverse(self):
  320. item = self._render()
  321. item.reverse()
  322. self._parent[self._start:self._stop:self._step] = item
  323. @inheritdoc
  324. def sort(self, cmp=None, key=None, reverse=None):
  325. item = self._render()
  326. kwargs = {}
  327. if cmp is not None:
  328. kwargs["cmp"] = cmp
  329. if key is not None:
  330. kwargs["key"] = key
  331. if reverse is not None:
  332. kwargs["reverse"] = reverse
  333. item.sort(**kwargs)
  334. self._parent[self._start:self._stop:self._step] = item
  335. del inheritdoc