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.
 
 
 
 

296 lines
9.0 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. from __future__ import unicode_literals
  23. from .compat import maxsize, py3k
  24. __all__ = ["SmartList"]
  25. class SmartList(list):
  26. def __init__(self, iterable=None):
  27. if iterable:
  28. super(SmartList, self).__init__(iterable)
  29. else:
  30. super(SmartList, self).__init__()
  31. self._children = {}
  32. def __getitem__(self, key):
  33. if not isinstance(key, slice):
  34. return super(SmartList, self).__getitem__(key)
  35. sliceinfo = [key.start, key.stop, 1 if not key.step else key.step]
  36. child = _ListProxy(self, sliceinfo)
  37. self._children[id(child)] = (child, sliceinfo)
  38. return child
  39. def __setitem__(self, key, item):
  40. if not isinstance(key, slice):
  41. return super(SmartList, self).__setitem__(key, item)
  42. item = list(item)
  43. super(SmartList, self).__setitem__(key, item)
  44. diff = len(item) - key.stop + key.start
  45. if diff:
  46. for child, (start, stop, step) in self._children.itervalues():
  47. if start >= key.stop:
  48. self._children[id(child)][1][0] += diff
  49. if stop >= key.stop and stop != maxsize:
  50. self._children[id(child)][1][1] += diff
  51. def __delitem__(self, key):
  52. super(SmartList, self).__delitem__(key)
  53. if not isinstance(key, slice):
  54. key = slice(key, key + 1)
  55. diff = key.stop - key.start
  56. for child, (start, stop, step) in self._children.itervalues():
  57. if start > key.start:
  58. self._children[id(child)][1][0] -= diff
  59. if stop >= key.stop:
  60. self._children[id(child)][1][1] -= diff
  61. if not py3k:
  62. def __getslice__(self, start, stop):
  63. return self.__getitem__(slice(start, stop))
  64. def __setslice__(self, start, stop, iterable):
  65. self.__setitem__(slice(start, stop), iterable)
  66. def __delslice__(self, start, stop):
  67. self.__delitem__(slice(start, stop))
  68. def __add__(self, other):
  69. return SmartList(list(self) + other)
  70. def __radd__(self, other):
  71. return SmartList(other + list(self))
  72. def __iadd__(self, other):
  73. self.extend(other)
  74. return self
  75. def append(self, item):
  76. head = len(self)
  77. self[head:head] = [item]
  78. def extend(self, item):
  79. head = len(self)
  80. self[head:head] = item
  81. def insert(self, index, item):
  82. self[index:index] = [item]
  83. def pop(self, index=None):
  84. if index is None:
  85. index = len(self) - 1
  86. item = self[index]
  87. del self[index]
  88. return item
  89. def remove(self, item):
  90. del self[self.index(item)]
  91. def reverse(self):
  92. copy = list(self)
  93. for child in self._children:
  94. child._parent = copy
  95. super(SmartList, self).reverse()
  96. def sort(self, cmp=None, key=None, reverse=None):
  97. copy = list(self)
  98. for child in self._children:
  99. child._parent = copy
  100. if cmp is not None:
  101. if key is not None:
  102. if reverse is not None:
  103. super(SmartList, self).sort(cmp, key, reverse)
  104. else:
  105. super(SmartList, self).sort(cmp, key)
  106. else:
  107. super(SmartList, self).sort(cmp)
  108. else:
  109. super(SmartList, self).sort()
  110. class _ListProxy(list):
  111. def __init__(self, parent, sliceinfo):
  112. super(_ListProxy, self).__init__()
  113. self._parent = parent
  114. self._sliceinfo = sliceinfo
  115. def __repr__(self):
  116. return repr(self._render())
  117. def __lt__(self, other):
  118. if isinstance(other, _ListProxy):
  119. return self._render() < list(other)
  120. return self._render() < other
  121. def __le__(self, other):
  122. if isinstance(other, _ListProxy):
  123. return self._render() <= list(other)
  124. return self._render() <= other
  125. def __eq__(self, other):
  126. if isinstance(other, _ListProxy):
  127. return self._render() == list(other)
  128. return self._render() == other
  129. def __ne__(self, other):
  130. if isinstance(other, _ListProxy):
  131. return self._render() != list(other)
  132. return self._render() != other
  133. def __gt__(self, other):
  134. if isinstance(other, _ListProxy):
  135. return self._render() > list(other)
  136. return self._render() > other
  137. def __ge__(self, other):
  138. if isinstance(other, _ListProxy):
  139. return self._render() >= list(other)
  140. return self._render() >= other
  141. if py3k:
  142. def __bool__(self):
  143. return bool(self._render())
  144. else:
  145. def __nonzero__(self):
  146. return bool(self._render())
  147. def __len__(self):
  148. return (self._stop - self._start) / self._step
  149. def __getitem__(self, key):
  150. return self._render()[key]
  151. def __setitem__(self, key, item):
  152. if isinstance(key, slice):
  153. adjusted = slice(key.start + self._start, key.stop + self._stop,
  154. key.step)
  155. self._parent[adjusted] = item
  156. else:
  157. self._parent[self._start + key] = item
  158. def __delitem__(self, key):
  159. if isinstance(key, slice):
  160. adjusted = slice(key.start + self._start, key.stop + self._stop,
  161. key.step)
  162. del self._parent[adjusted]
  163. else:
  164. del self._parent[self._start + key]
  165. def __iter__(self):
  166. i = self._start
  167. while i < self._stop:
  168. yield self._parent[i]
  169. i += self._step
  170. def __reversed__(self):
  171. i = self._stop - 1
  172. while i >= self._start:
  173. yield self._parent[i]
  174. i -= self._step
  175. def __contains__(self, item):
  176. return item in self._render()
  177. if not py3k:
  178. def __getslice__(self, start, stop):
  179. return self.__getitem__(slice(start, stop))
  180. def __setslice__(self, start, stop, iterable):
  181. self.__setitem__(slice(start, stop), iterable)
  182. def __delslice__(self, start, stop):
  183. self.__delitem__(slice(start, stop))
  184. def __add__(self, other):
  185. return SmartList(list(self) + other)
  186. def __radd__(self, other):
  187. return SmartList(other + list(self))
  188. def __iadd__(self, other):
  189. self.extend(other)
  190. return self
  191. @property
  192. def _start(self):
  193. return self._sliceinfo[0]
  194. @property
  195. def _stop(self):
  196. return self._sliceinfo[1]
  197. @property
  198. def _step(self):
  199. return self._sliceinfo[2]
  200. def _render(self):
  201. return list(self._parent)[self._start:self._stop:self._step]
  202. def append(self, item):
  203. self._parent.insert(self._stop, item)
  204. def count(self, item):
  205. return self._render().count(item)
  206. def index(self, item, start=None, stop=None):
  207. if start is not None:
  208. if stop is not None:
  209. return self._render().index(item, start, stop)
  210. return self._render().index(item, start)
  211. return self._render().index(item)
  212. def extend(self, item):
  213. self._parent[self._stop:self._stop] = item
  214. def insert(self, index, item):
  215. self._parent.insert(self._start + index, item)
  216. def pop(self, index=None):
  217. if index is None:
  218. index = len(self) - 1
  219. return self._parent.pop(self._start + index)
  220. def remove(self, item):
  221. index = self.index(item)
  222. del self._parent[index]
  223. def reverse(self):
  224. item = self._render()
  225. item.reverse()
  226. self._parent[self._start:self._stop:self._step] = item
  227. def sort(self, cmp=None, key=None, reverse=None):
  228. item = self._render()
  229. if cmp is not None:
  230. if key is not None:
  231. if reverse is not None:
  232. item.sort(cmp, key, reverse)
  233. else:
  234. item.sort(cmp, key)
  235. else:
  236. item.sort(cmp)
  237. else:
  238. item.sort()
  239. self._parent[self._start:self._stop:self._step] = item