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.
 
 
 
 

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