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.
 
 
 
 

364 lines
11 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012 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:`~mwparserfromhell.smart_list.SmartList`
  24. type, as well as its :py:class:`~mwparserfromhell.smart_list._ListProxy` child,
  25. which together implement a list whose sublists reflect changes made to the main
  26. list, and vice-versa.
  27. """
  28. from __future__ import unicode_literals
  29. from .compat import maxsize, py3k
  30. __all__ = ["SmartList"]
  31. def inheritdoc(method):
  32. """Set __doc__ of *method* to __doc__ of *method* in its parent class.
  33. Since this is used on
  34. :py:class:`~mwparserfromhell.smart_list.SmartList`, the "parent class" used
  35. is ``list``. This function can be used as a decorator.
  36. """
  37. method.__doc__ = getattr(list, method.__name__).__doc__
  38. return method
  39. class SmartList(list):
  40. """Implements the ``list`` interface with special handling of sublists.
  41. When a sublist is created (by ``list[i:j]``), any changes made to this
  42. list (such as the addition, removal, or replacement of elements) will be
  43. reflected in the sublist, or vice-versa, to the greatest degree possible.
  44. This is implemented by having sublists - instances of the
  45. :py:class:`~mwparserfromhell.smart_list._ListProxy` type - dynamically
  46. determine their elements by storing their slice info and retrieving that
  47. slice from the parent. Methods that change the size of the list also change
  48. the slice info. For example::
  49. >>> parent = SmartList([0, 1, 2, 3])
  50. >>> parent
  51. [0, 1, 2, 3]
  52. >>> child = parent[2:]
  53. >>> child
  54. [2, 3]
  55. >>> child.append(4)
  56. >>> child
  57. [2, 3, 4]
  58. >>> parent
  59. [0, 1, 2, 3, 4]
  60. """
  61. def __init__(self, iterable=None):
  62. if iterable:
  63. super(SmartList, self).__init__(iterable)
  64. else:
  65. super(SmartList, self).__init__()
  66. self._children = {}
  67. def __getitem__(self, key):
  68. if not isinstance(key, slice):
  69. return super(SmartList, self).__getitem__(key)
  70. sliceinfo = [key.start, key.stop, 1 if not key.step else key.step]
  71. child = _ListProxy(self, sliceinfo)
  72. self._children[id(child)] = (child, sliceinfo)
  73. return child
  74. def __setitem__(self, key, item):
  75. if not isinstance(key, slice):
  76. return super(SmartList, self).__setitem__(key, item)
  77. item = list(item)
  78. super(SmartList, self).__setitem__(key, item)
  79. diff = len(item) - key.stop + key.start
  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 not isinstance(key, slice):
  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:`~mwparserfromhell.smart_list.SmartList`
  157. object when slicing. It does not actually store the list at any time;
  158. instead, whenever the list is needed, it builds it dynamically using the
  159. :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. adjusted = slice(key.start + self._start, key.stop + self._stop,
  204. key.step)
  205. self._parent[adjusted] = item
  206. else:
  207. self._parent[self._start + key] = item
  208. def __delitem__(self, key):
  209. if isinstance(key, slice):
  210. adjusted = slice(key.start + self._start, key.stop + self._stop,
  211. key.step)
  212. del self._parent[adjusted]
  213. else:
  214. del self._parent[self._start + key]
  215. def __iter__(self):
  216. i = self._start
  217. while i < self._stop:
  218. yield self._parent[i]
  219. i += self._step
  220. def __reversed__(self):
  221. i = self._stop - 1
  222. while i >= self._start:
  223. yield self._parent[i]
  224. i -= self._step
  225. def __contains__(self, item):
  226. return item in self._render()
  227. if not py3k:
  228. def __getslice__(self, start, stop):
  229. return self.__getitem__(slice(start, stop))
  230. def __setslice__(self, start, stop, iterable):
  231. self.__setitem__(slice(start, stop), iterable)
  232. def __delslice__(self, start, stop):
  233. self.__delitem__(slice(start, stop))
  234. def __add__(self, other):
  235. return SmartList(list(self) + other)
  236. def __radd__(self, other):
  237. return SmartList(other + list(self))
  238. def __iadd__(self, other):
  239. self.extend(other)
  240. return self
  241. @property
  242. def _start(self):
  243. return self._sliceinfo[0]
  244. @property
  245. def _stop(self):
  246. return self._sliceinfo[1]
  247. @property
  248. def _step(self):
  249. return self._sliceinfo[2]
  250. def _render(self):
  251. return list(self._parent)[self._start:self._stop:self._step]
  252. @inheritdoc
  253. def append(self, item):
  254. self._parent.insert(self._stop, item)
  255. @inheritdoc
  256. def count(self, item):
  257. return self._render().count(item)
  258. @inheritdoc
  259. def index(self, item, start=None, stop=None):
  260. if start is not None:
  261. if stop is not None:
  262. return self._render().index(item, start, stop)
  263. return self._render().index(item, start)
  264. return self._render().index(item)
  265. @inheritdoc
  266. def extend(self, item):
  267. self._parent[self._stop:self._stop] = item
  268. @inheritdoc
  269. def insert(self, index, item):
  270. self._parent.insert(self._start + index, item)
  271. @inheritdoc
  272. def pop(self, index=None):
  273. if index is None:
  274. index = len(self) - 1
  275. return self._parent.pop(self._start + index)
  276. @inheritdoc
  277. def remove(self, item):
  278. index = self.index(item)
  279. del self._parent[index]
  280. @inheritdoc
  281. def reverse(self):
  282. item = self._render()
  283. item.reverse()
  284. self._parent[self._start:self._stop:self._step] = item
  285. @inheritdoc
  286. def sort(self, cmp=None, key=None, reverse=None):
  287. item = self._render()
  288. if cmp is not None:
  289. if key is not None:
  290. if reverse is not None:
  291. item.sort(cmp, key, reverse)
  292. else:
  293. item.sort(cmp, key)
  294. else:
  295. item.sort(cmp)
  296. else:
  297. item.sort()
  298. self._parent[self._start:self._stop:self._step] = item