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.
 
 
 
 

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