A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

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