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.
 
 
 
 

462 lines
15 KiB

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