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.
 
 
 
 

461 lines
15 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2014 Ben Kurtovic <ben.kurtovic@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. """
  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 _SliceNormalizerMixIn(object):
  38. """MixIn that provides a private method to normalize slices."""
  39. def _normalize_slice(self, key):
  40. """Return a slice equivalent to the input *key*, standardized."""
  41. if key.start is not None:
  42. start = (len(self) + key.start) if key.start < 0 else key.start
  43. else:
  44. start = 0
  45. if key.stop is not None:
  46. stop = (len(self) + key.stop) if key.stop < 0 else key.stop
  47. else:
  48. stop = maxsize
  49. return slice(start, stop, key.step or 1)
  50. class SmartList(_SliceNormalizerMixIn, list):
  51. """Implements the ``list`` interface with special handling of sublists.
  52. When a sublist is created (by ``list[i:j]``), any changes made to this
  53. list (such as the addition, removal, or replacement of elements) will be
  54. reflected in the sublist, or vice-versa, to the greatest degree possible.
  55. This is implemented by having sublists - instances of the
  56. :py:class:`~._ListProxy` type - dynamically determine their elements by
  57. storing their slice info and retrieving that slice from the parent. Methods
  58. that change the size of the list also change the slice info. For example::
  59. >>> parent = SmartList([0, 1, 2, 3])
  60. >>> parent
  61. [0, 1, 2, 3]
  62. >>> child = parent[2:]
  63. >>> child
  64. [2, 3]
  65. >>> child.append(4)
  66. >>> child
  67. [2, 3, 4]
  68. >>> parent
  69. [0, 1, 2, 3, 4]
  70. The parent needs to keep a list of its children in order to update them,
  71. which prevents them from being garbage-collected. If you are keeping the
  72. parent around for a while but creating many children, it is advisable to
  73. call :py:meth:`~._ListProxy.destroy` when you're finished with them.
  74. """
  75. def __init__(self, iterable=None):
  76. if iterable:
  77. super(SmartList, self).__init__(iterable)
  78. else:
  79. super(SmartList, self).__init__()
  80. self._children = {}
  81. def __getitem__(self, key):
  82. if not isinstance(key, slice):
  83. return super(SmartList, self).__getitem__(key)
  84. key = self._normalize_slice(key)
  85. sliceinfo = [key.start, key.stop, key.step]
  86. child = _ListProxy(self, sliceinfo)
  87. self._children[id(child)] = (child, sliceinfo)
  88. return child
  89. def __setitem__(self, key, item):
  90. if not isinstance(key, slice):
  91. return super(SmartList, self).__setitem__(key, item)
  92. item = list(item)
  93. super(SmartList, self).__setitem__(key, item)
  94. key = self._normalize_slice(key)
  95. diff = len(item) + (key.start - key.stop) // key.step
  96. values = self._children.values if py3k else self._children.itervalues
  97. if diff:
  98. for child, (start, stop, step) in values():
  99. if start > key.stop:
  100. self._children[id(child)][1][0] += diff
  101. if stop >= key.stop and stop != maxsize:
  102. self._children[id(child)][1][1] += diff
  103. def __delitem__(self, key):
  104. super(SmartList, self).__delitem__(key)
  105. if isinstance(key, slice):
  106. key = self._normalize_slice(key)
  107. else:
  108. key = slice(key, key + 1, 1)
  109. diff = (key.stop - key.start) // key.step
  110. values = self._children.values if py3k else self._children.itervalues
  111. for child, (start, stop, step) in values():
  112. if start > key.start:
  113. self._children[id(child)][1][0] -= diff
  114. if stop >= key.stop and stop != maxsize:
  115. self._children[id(child)][1][1] -= diff
  116. if not py3k:
  117. def __getslice__(self, start, stop):
  118. return self.__getitem__(slice(start, stop))
  119. def __setslice__(self, start, stop, iterable):
  120. self.__setitem__(slice(start, stop), iterable)
  121. def __delslice__(self, start, stop):
  122. self.__delitem__(slice(start, stop))
  123. def __add__(self, other):
  124. return SmartList(list(self) + other)
  125. def __radd__(self, other):
  126. return SmartList(other + list(self))
  127. def __iadd__(self, other):
  128. self.extend(other)
  129. return self
  130. def _release_children(self):
  131. copy = list(self)
  132. for child in self._children:
  133. child._parent = copy
  134. @inheritdoc
  135. def append(self, item):
  136. head = len(self)
  137. self[head:head] = [item]
  138. @inheritdoc
  139. def extend(self, item):
  140. head = len(self)
  141. self[head:head] = item
  142. @inheritdoc
  143. def insert(self, index, item):
  144. self[index:index] = [item]
  145. @inheritdoc
  146. def pop(self, index=None):
  147. if index is None:
  148. index = len(self) - 1
  149. item = self[index]
  150. del self[index]
  151. return item
  152. @inheritdoc
  153. def remove(self, item):
  154. del self[self.index(item)]
  155. @inheritdoc
  156. def reverse(self):
  157. self._release_children()
  158. super(SmartList, self).reverse()
  159. if py3k:
  160. @inheritdoc
  161. def sort(self, key=None, reverse=None):
  162. self._release_children()
  163. kwargs = {}
  164. if key is not None:
  165. kwargs["key"] = key
  166. if reverse is not None:
  167. kwargs["reverse"] = reverse
  168. super(SmartList, self).sort(**kwargs)
  169. else:
  170. @inheritdoc
  171. def sort(self, cmp=None, key=None, reverse=None):
  172. self._release_children()
  173. kwargs = {}
  174. if cmp is not None:
  175. kwargs["cmp"] = cmp
  176. if key is not None:
  177. kwargs["key"] = key
  178. if reverse is not None:
  179. kwargs["reverse"] = reverse
  180. super(SmartList, self).sort(**kwargs)
  181. class _ListProxy(_SliceNormalizerMixIn, list):
  182. """Implement the ``list`` interface by getting elements from a parent.
  183. This is created by a :py:class:`~.SmartList` object when slicing. It does
  184. not actually store the list at any time; instead, whenever the list is
  185. needed, it builds it dynamically using the :py:meth:`_render` method.
  186. """
  187. def __init__(self, parent, sliceinfo):
  188. super(_ListProxy, self).__init__()
  189. self._parent = parent
  190. self._sliceinfo = sliceinfo
  191. def __repr__(self):
  192. return repr(self._render())
  193. def __lt__(self, other):
  194. if isinstance(other, _ListProxy):
  195. return self._render() < list(other)
  196. return self._render() < other
  197. def __le__(self, other):
  198. if isinstance(other, _ListProxy):
  199. return self._render() <= list(other)
  200. return self._render() <= other
  201. def __eq__(self, other):
  202. if isinstance(other, _ListProxy):
  203. return self._render() == list(other)
  204. return self._render() == other
  205. def __ne__(self, other):
  206. if isinstance(other, _ListProxy):
  207. return self._render() != list(other)
  208. return self._render() != other
  209. def __gt__(self, other):
  210. if isinstance(other, _ListProxy):
  211. return self._render() > list(other)
  212. return self._render() > other
  213. def __ge__(self, other):
  214. if isinstance(other, _ListProxy):
  215. return self._render() >= list(other)
  216. return self._render() >= other
  217. if py3k:
  218. def __bool__(self):
  219. return bool(self._render())
  220. else:
  221. def __nonzero__(self):
  222. return bool(self._render())
  223. def __len__(self):
  224. return (self._stop - self._start) // self._step
  225. def __getitem__(self, key):
  226. if isinstance(key, slice):
  227. key = self._normalize_slice(key)
  228. if key.stop == maxsize:
  229. keystop = self._stop
  230. else:
  231. keystop = key.stop + self._start
  232. adjusted = slice(key.start + self._start, keystop, key.step)
  233. return self._parent[adjusted]
  234. else:
  235. return self._render()[key]
  236. def __setitem__(self, key, item):
  237. if isinstance(key, slice):
  238. key = self._normalize_slice(key)
  239. if key.stop == maxsize:
  240. keystop = self._stop
  241. else:
  242. keystop = key.stop + self._start
  243. adjusted = slice(key.start + self._start, keystop, key.step)
  244. self._parent[adjusted] = item
  245. else:
  246. length = len(self)
  247. if key < 0:
  248. key = length + key
  249. if key < 0 or key >= length:
  250. raise IndexError("list assignment index out of range")
  251. self._parent[self._start + key] = item
  252. def __delitem__(self, key):
  253. if isinstance(key, slice):
  254. key = self._normalize_slice(key)
  255. if key.stop == maxsize:
  256. keystop = self._stop
  257. else:
  258. keystop = key.stop + self._start
  259. adjusted = slice(key.start + self._start, keystop, key.step)
  260. del self._parent[adjusted]
  261. else:
  262. length = len(self)
  263. if key < 0:
  264. key = length + key
  265. if key < 0 or key >= length:
  266. raise IndexError("list assignment index out of range")
  267. del self._parent[self._start + key]
  268. def __iter__(self):
  269. i = self._start
  270. while i < self._stop:
  271. yield self._parent[i]
  272. i += self._step
  273. def __reversed__(self):
  274. i = self._stop - 1
  275. while i >= self._start:
  276. yield self._parent[i]
  277. i -= self._step
  278. def __contains__(self, item):
  279. return item in self._render()
  280. if not py3k:
  281. def __getslice__(self, start, stop):
  282. return self.__getitem__(slice(start, stop))
  283. def __setslice__(self, start, stop, iterable):
  284. self.__setitem__(slice(start, stop), iterable)
  285. def __delslice__(self, start, stop):
  286. self.__delitem__(slice(start, stop))
  287. def __add__(self, other):
  288. return SmartList(list(self) + other)
  289. def __radd__(self, other):
  290. return SmartList(other + list(self))
  291. def __iadd__(self, other):
  292. self.extend(other)
  293. return self
  294. def __mul__(self, other):
  295. return SmartList(list(self) * other)
  296. def __rmul__(self, other):
  297. return SmartList(other * list(self))
  298. def __imul__(self, other):
  299. self.extend(list(self) * (other - 1))
  300. return self
  301. @property
  302. def _start(self):
  303. """The starting index of this list, inclusive."""
  304. return self._sliceinfo[0]
  305. @property
  306. def _stop(self):
  307. """The ending index of this list, exclusive."""
  308. if self._sliceinfo[1] == maxsize:
  309. return len(self._parent)
  310. return self._sliceinfo[1]
  311. @property
  312. def _step(self):
  313. """The number to increase the index by between items."""
  314. return self._sliceinfo[2]
  315. def _render(self):
  316. """Return the actual list from the stored start/stop/step."""
  317. return list(self._parent)[self._start:self._stop:self._step]
  318. @inheritdoc
  319. def append(self, item):
  320. self._parent.insert(self._stop, item)
  321. @inheritdoc
  322. def count(self, item):
  323. return self._render().count(item)
  324. @inheritdoc
  325. def index(self, item, start=None, stop=None):
  326. if start is not None:
  327. if stop is not None:
  328. return self._render().index(item, start, stop)
  329. return self._render().index(item, start)
  330. return self._render().index(item)
  331. @inheritdoc
  332. def extend(self, item):
  333. self._parent[self._stop:self._stop] = item
  334. @inheritdoc
  335. def insert(self, index, item):
  336. if index < 0:
  337. index = len(self) + index
  338. self._parent.insert(self._start + index, item)
  339. @inheritdoc
  340. def pop(self, index=None):
  341. length = len(self)
  342. if index is None:
  343. index = length - 1
  344. elif index < 0:
  345. index = length + index
  346. if index < 0 or index >= length:
  347. raise IndexError("pop index out of range")
  348. return self._parent.pop(self._start + index)
  349. @inheritdoc
  350. def remove(self, item):
  351. index = self.index(item)
  352. del self._parent[self._start + index]
  353. @inheritdoc
  354. def reverse(self):
  355. item = self._render()
  356. item.reverse()
  357. self._parent[self._start:self._stop:self._step] = item
  358. if py3k:
  359. @inheritdoc
  360. def sort(self, key=None, reverse=None):
  361. item = self._render()
  362. kwargs = {}
  363. if key is not None:
  364. kwargs["key"] = key
  365. if reverse is not None:
  366. kwargs["reverse"] = reverse
  367. item.sort(**kwargs)
  368. self._parent[self._start:self._stop:self._step] = item
  369. else:
  370. @inheritdoc
  371. def sort(self, cmp=None, key=None, reverse=None):
  372. item = self._render()
  373. kwargs = {}
  374. if cmp is not None:
  375. kwargs["cmp"] = cmp
  376. if key is not None:
  377. kwargs["key"] = key
  378. if reverse is not None:
  379. kwargs["reverse"] = reverse
  380. item.sort(**kwargs)
  381. self._parent[self._start:self._stop:self._step] = item
  382. def destroy(self):
  383. """Make the parent forget this child. The child will no longer work."""
  384. self._parent._children.pop(id(self))
  385. del inheritdoc