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.
 
 
 
 

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