A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

211 lignes
5.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. __delitem__
  51. def __getslice__(self, start, stop):
  52. return self.__getitem__(slice(start, stop))
  53. def __setslice__(self, start, stop, iterable):
  54. self.__setitem__(slice(start, stop), iterable)
  55. def __delslice__(self, start, stop):
  56. self.__delitem__(slice(start, stop))
  57. __add__
  58. __radd__
  59. __iadd__
  60. __mul__
  61. __rmul__
  62. __imul__
  63. def append(self, item):
  64. super(SmartList, self).append(item)
  65. for child, (start, stop, step) in self._children.itervalues():
  66. if stop >= len(self) - 1 and stop != sys.maxint:
  67. self._children[id(child)][1][1] += 1
  68. count
  69. index
  70. extend
  71. insert
  72. pop
  73. remove
  74. reverse
  75. sort
  76. class _ListProxy(list):
  77. def __init__(self, parent, sliceinfo):
  78. super(_ListProxy, self).__init__()
  79. self._parent = parent
  80. self._sliceinfo = sliceinfo
  81. def __repr__(self):
  82. return repr(self._render())
  83. __lt__
  84. __le__
  85. __eq__
  86. __ne__
  87. __gt__
  88. __ge__
  89. __nonzero__
  90. def __len__(self):
  91. return (self._stop - self._start) / self._step
  92. def __getitem__(self, key):
  93. return self._render()[key]
  94. def __setitem__(self, key, item):
  95. if isinstance(key, slice):
  96. adjusted = slice(key.start + self._start, key.stop + self._stop,
  97. key.step)
  98. self._parent[adjusted] = item
  99. else:
  100. self._parent[self._start + index] = item
  101. __delitem__
  102. def __iter__(self):
  103. i = self._start
  104. while i < self._stop:
  105. yield self._parent[i]
  106. i += self._step
  107. def __reversed__(self):
  108. i = self._stop - 1
  109. while i >= self._start:
  110. yield self._parent[i]
  111. i -= self._step
  112. def __contains__(self, item):
  113. return item in self._render()
  114. def __getslice__(self, start, stop):
  115. return self.__getitem__(slice(start, stop))
  116. def __setslice__(self, start, stop, iterable):
  117. self.__setitem__(slice(start, stop), iterable)
  118. def __delslice__(self, start, stop):
  119. self.__delitem__(slice(start, stop))
  120. __add__
  121. __radd__
  122. __iadd__
  123. __mul__
  124. __rmul__
  125. __imul__
  126. @property
  127. def _start(self):
  128. return self._sliceinfo[0]
  129. @property
  130. def _stop(self):
  131. return self._sliceinfo[1]
  132. @property
  133. def _step(self):
  134. return self._sliceinfo[2]
  135. def _render(self):
  136. return self._parent[self._start:self._stop:self._step]
  137. def append(self, obj):
  138. self._parent.insert(self._stop, obj)
  139. count
  140. index
  141. extend
  142. def insert(self, index, obj):
  143. self._parent.insert(self._start + index, obj)
  144. pop
  145. remove
  146. reverse
  147. sort