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.
 
 
 
 

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