A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

342 řádky
10 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2013 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. """
  23. This module contains the :py:class:`~.StringMixIn` type, which implements the
  24. interface for the ``unicode`` type (``str`` on py3k) in a dynamic manner.
  25. """
  26. from __future__ import unicode_literals
  27. from .compat import py3k, str
  28. __all__ = ["StringMixIn"]
  29. def inheritdoc(method):
  30. """Set __doc__ of *method* to __doc__ of *method* in its parent class.
  31. Since this is used on :py:class:`~.StringMixIn`, the "parent class" used is
  32. ``str``. This function can be used as a decorator.
  33. """
  34. method.__doc__ = getattr(str, method.__name__).__doc__
  35. return method
  36. class StringMixIn(object):
  37. """Implement the interface for ``unicode``/``str`` in a dynamic manner.
  38. To use this class, inherit from it and override the :py:meth:`__unicode__`
  39. method (same on py3k) to return the string representation of the object.
  40. The various string methods will operate on the value of
  41. :py:meth:`__unicode__` instead of the immutable ``self`` like the regular
  42. ``str`` type.
  43. """
  44. if py3k:
  45. def __str__(self):
  46. return self.__unicode__()
  47. def __bytes__(self):
  48. return self.__unicode__().encode("utf8")
  49. else:
  50. def __str__(self):
  51. return self.__unicode__().encode("utf8")
  52. def __unicode__(self):
  53. raise NotImplementedError()
  54. def __repr__(self):
  55. return repr(self.__unicode__())
  56. def __lt__(self, other):
  57. if isinstance(other, StringMixIn):
  58. return self.__unicode__() < other.__unicode__()
  59. return self.__unicode__() < other
  60. def __le__(self, other):
  61. if isinstance(other, StringMixIn):
  62. return self.__unicode__() <= other.__unicode__()
  63. return self.__unicode__() <= other
  64. def __eq__(self, other):
  65. if isinstance(other, StringMixIn):
  66. return self.__unicode__() == other.__unicode__()
  67. return self.__unicode__() == other
  68. def __ne__(self, other):
  69. if isinstance(other, StringMixIn):
  70. return self.__unicode__() != other.__unicode__()
  71. return self.__unicode__() != other
  72. def __gt__(self, other):
  73. if isinstance(other, StringMixIn):
  74. return self.__unicode__() > other.__unicode__()
  75. return self.__unicode__() > other
  76. def __ge__(self, other):
  77. if isinstance(other, StringMixIn):
  78. return self.__unicode__() >= other.__unicode__()
  79. return self.__unicode__() >= other
  80. if py3k:
  81. def __bool__(self):
  82. return bool(self.__unicode__())
  83. else:
  84. def __nonzero__(self):
  85. return bool(self.__unicode__())
  86. def __len__(self):
  87. return len(self.__unicode__())
  88. def __iter__(self):
  89. for char in self.__unicode__():
  90. yield char
  91. def __getitem__(self, key):
  92. return self.__unicode__()[key]
  93. def __contains__(self, item):
  94. if isinstance(item, StringMixIn):
  95. return str(item) in self.__unicode__()
  96. return item in self.__unicode__()
  97. @inheritdoc
  98. def capitalize(self):
  99. return self.__unicode__().capitalize()
  100. if py3k:
  101. @inheritdoc
  102. def casefold(self):
  103. return self.__unicode__().casefold()
  104. @inheritdoc
  105. def center(self, width, fillchar=None):
  106. if fillchar is None:
  107. return self.__unicode__().center(width)
  108. return self.__unicode__().center(width, fillchar)
  109. @inheritdoc
  110. def count(self, sub, start=None, end=None):
  111. return self.__unicode__().count(sub, start, end)
  112. if not py3k:
  113. @inheritdoc
  114. def decode(self, encoding=None, errors=None):
  115. if errors is None:
  116. if encoding is None:
  117. return self.__unicode__().decode()
  118. return self.__unicode__().decode(encoding)
  119. return self.__unicode__().decode(encoding, errors)
  120. @inheritdoc
  121. def encode(self, encoding=None, errors=None):
  122. if errors is None:
  123. if encoding is None:
  124. return self.__unicode__().encode()
  125. return self.__unicode__().encode(encoding)
  126. return self.__unicode__().encode(encoding, errors)
  127. @inheritdoc
  128. def endswith(self, prefix, start=None, end=None):
  129. return self.__unicode__().endswith(prefix, start, end)
  130. @inheritdoc
  131. def expandtabs(self, tabsize=None):
  132. if tabsize is None:
  133. return self.__unicode__().expandtabs()
  134. return self.__unicode__().expandtabs(tabsize)
  135. @inheritdoc
  136. def find(self, sub, start=None, end=None):
  137. return self.__unicode__().find(sub, start, end)
  138. @inheritdoc
  139. def format(self, *args, **kwargs):
  140. return self.__unicode__().format(*args, **kwargs)
  141. if py3k:
  142. @inheritdoc
  143. def format_map(self, mapping):
  144. return self.__unicode__().format_map(mapping)
  145. @inheritdoc
  146. def index(self, sub, start=None, end=None):
  147. return self.__unicode__().index(sub, start, end)
  148. @inheritdoc
  149. def isalnum(self):
  150. return self.__unicode__().isalnum()
  151. @inheritdoc
  152. def isalpha(self):
  153. return self.__unicode__().isalpha()
  154. @inheritdoc
  155. def isdecimal(self):
  156. return self.__unicode__().isdecimal()
  157. @inheritdoc
  158. def isdigit(self):
  159. return self.__unicode__().isdigit()
  160. if py3k:
  161. @inheritdoc
  162. def isidentifier(self):
  163. return self.__unicode__().isidentifier()
  164. @inheritdoc
  165. def islower(self):
  166. return self.__unicode__().islower()
  167. @inheritdoc
  168. def isnumeric(self):
  169. return self.__unicode__().isnumeric()
  170. if py3k:
  171. @inheritdoc
  172. def isprintable(self):
  173. return self.__unicode__().isprintable()
  174. @inheritdoc
  175. def isspace(self):
  176. return self.__unicode__().isspace()
  177. @inheritdoc
  178. def istitle(self):
  179. return self.__unicode__().istitle()
  180. @inheritdoc
  181. def isupper(self):
  182. return self.__unicode__().isupper()
  183. @inheritdoc
  184. def join(self, iterable):
  185. return self.__unicode__().join(iterable)
  186. @inheritdoc
  187. def ljust(self, width, fillchar=None):
  188. if fillchar is None:
  189. return self.__unicode__().ljust(width)
  190. return self.__unicode__().ljust(width, fillchar)
  191. @inheritdoc
  192. def lower(self):
  193. return self.__unicode__().lower()
  194. @inheritdoc
  195. def lstrip(self, chars=None):
  196. return self.__unicode__().lstrip(chars)
  197. if py3k:
  198. @inheritdoc
  199. @staticmethod
  200. def maketrans(self, x, y=None, z=None):
  201. if z is None:
  202. if y is None:
  203. return self.__unicode__.maketrans(x)
  204. return self.__unicode__.maketrans(x, y)
  205. return self.__unicode__.maketrans(x, y, z)
  206. @inheritdoc
  207. def partition(self, sep):
  208. return self.__unicode__().partition(sep)
  209. @inheritdoc
  210. def replace(self, old, new, count=None):
  211. if count is None:
  212. return self.__unicode__().replace(old, new)
  213. return self.__unicode__().replace(old, new, count)
  214. @inheritdoc
  215. def rfind(self, sub, start=None, end=None):
  216. return self.__unicode__().rfind(sub, start, end)
  217. @inheritdoc
  218. def rindex(self, sub, start=None, end=None):
  219. return self.__unicode__().rindex(sub, start, end)
  220. @inheritdoc
  221. def rjust(self, width, fillchar=None):
  222. if fillchar is None:
  223. return self.__unicode__().rjust(width)
  224. return self.__unicode__().rjust(width, fillchar)
  225. @inheritdoc
  226. def rpartition(self, sep):
  227. return self.__unicode__().rpartition(sep)
  228. @inheritdoc
  229. def rsplit(self, sep=None, maxsplit=None):
  230. if maxsplit is None:
  231. if sep is None:
  232. return self.__unicode__().rsplit()
  233. return self.__unicode__().rsplit(sep)
  234. return self.__unicode__().rsplit(sep, maxsplit)
  235. @inheritdoc
  236. def rstrip(self, chars=None):
  237. return self.__unicode__().rstrip(chars)
  238. @inheritdoc
  239. def split(self, sep=None, maxsplit=None):
  240. if maxsplit is None:
  241. if sep is None:
  242. return self.__unicode__().split()
  243. return self.__unicode__().split(sep)
  244. return self.__unicode__().split(sep, maxsplit)
  245. @inheritdoc
  246. def splitlines(self, keepends=None):
  247. if keepends is None:
  248. return self.__unicode__().splitlines()
  249. return self.__unicode__().splitlines(keepends)
  250. @inheritdoc
  251. def startswith(self, prefix, start=None, end=None):
  252. return self.__unicode__().startswith(prefix, start, end)
  253. @inheritdoc
  254. def strip(self, chars=None):
  255. return self.__unicode__().strip(chars)
  256. @inheritdoc
  257. def swapcase(self):
  258. return self.__unicode__().swapcase()
  259. @inheritdoc
  260. def title(self):
  261. return self.__unicode__().title()
  262. @inheritdoc
  263. def translate(self, table):
  264. return self.__unicode__().translate(table)
  265. @inheritdoc
  266. def upper(self):
  267. return self.__unicode__().upper()
  268. @inheritdoc
  269. def zfill(self, width):
  270. return self.__unicode__().zfill(width)
  271. del inheritdoc