A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

345 rader
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 __reversed__(self):
  94. return reversed(self.__unicode__())
  95. def __contains__(self, item):
  96. if isinstance(item, StringMixIn):
  97. return str(item) in self.__unicode__()
  98. return item in self.__unicode__()
  99. @inheritdoc
  100. def capitalize(self):
  101. return self.__unicode__().capitalize()
  102. if py3k:
  103. @inheritdoc
  104. def casefold(self):
  105. return self.__unicode__().casefold()
  106. @inheritdoc
  107. def center(self, width, fillchar=None):
  108. if fillchar is None:
  109. return self.__unicode__().center(width)
  110. return self.__unicode__().center(width, fillchar)
  111. @inheritdoc
  112. def count(self, sub, start=None, end=None):
  113. return self.__unicode__().count(sub, start, end)
  114. if not py3k:
  115. @inheritdoc
  116. def decode(self, encoding=None, errors=None):
  117. if errors is None:
  118. if encoding is None:
  119. return self.__unicode__().decode()
  120. return self.__unicode__().decode(encoding)
  121. return self.__unicode__().decode(encoding, errors)
  122. @inheritdoc
  123. def encode(self, encoding=None, errors=None):
  124. if errors is None:
  125. if encoding is None:
  126. return self.__unicode__().encode()
  127. return self.__unicode__().encode(encoding)
  128. return self.__unicode__().encode(encoding, errors)
  129. @inheritdoc
  130. def endswith(self, prefix, start=None, end=None):
  131. return self.__unicode__().endswith(prefix, start, end)
  132. @inheritdoc
  133. def expandtabs(self, tabsize=None):
  134. if tabsize is None:
  135. return self.__unicode__().expandtabs()
  136. return self.__unicode__().expandtabs(tabsize)
  137. @inheritdoc
  138. def find(self, sub, start=None, end=None):
  139. return self.__unicode__().find(sub, start, end)
  140. @inheritdoc
  141. def format(self, *args, **kwargs):
  142. return self.__unicode__().format(*args, **kwargs)
  143. if py3k:
  144. @inheritdoc
  145. def format_map(self, mapping):
  146. return self.__unicode__().format_map(mapping)
  147. @inheritdoc
  148. def index(self, sub, start=None, end=None):
  149. return self.__unicode__().index(sub, start, end)
  150. @inheritdoc
  151. def isalnum(self):
  152. return self.__unicode__().isalnum()
  153. @inheritdoc
  154. def isalpha(self):
  155. return self.__unicode__().isalpha()
  156. @inheritdoc
  157. def isdecimal(self):
  158. return self.__unicode__().isdecimal()
  159. @inheritdoc
  160. def isdigit(self):
  161. return self.__unicode__().isdigit()
  162. if py3k:
  163. @inheritdoc
  164. def isidentifier(self):
  165. return self.__unicode__().isidentifier()
  166. @inheritdoc
  167. def islower(self):
  168. return self.__unicode__().islower()
  169. @inheritdoc
  170. def isnumeric(self):
  171. return self.__unicode__().isnumeric()
  172. if py3k:
  173. @inheritdoc
  174. def isprintable(self):
  175. return self.__unicode__().isprintable()
  176. @inheritdoc
  177. def isspace(self):
  178. return self.__unicode__().isspace()
  179. @inheritdoc
  180. def istitle(self):
  181. return self.__unicode__().istitle()
  182. @inheritdoc
  183. def isupper(self):
  184. return self.__unicode__().isupper()
  185. @inheritdoc
  186. def join(self, iterable):
  187. return self.__unicode__().join(iterable)
  188. @inheritdoc
  189. def ljust(self, width, fillchar=None):
  190. if fillchar is None:
  191. return self.__unicode__().ljust(width)
  192. return self.__unicode__().ljust(width, fillchar)
  193. @inheritdoc
  194. def lower(self):
  195. return self.__unicode__().lower()
  196. @inheritdoc
  197. def lstrip(self, chars=None):
  198. return self.__unicode__().lstrip(chars)
  199. if py3k:
  200. @inheritdoc
  201. @staticmethod
  202. def maketrans(self, x, y=None, z=None):
  203. if z is None:
  204. if y is None:
  205. return self.__unicode__.maketrans(x)
  206. return self.__unicode__.maketrans(x, y)
  207. return self.__unicode__.maketrans(x, y, z)
  208. @inheritdoc
  209. def partition(self, sep):
  210. return self.__unicode__().partition(sep)
  211. @inheritdoc
  212. def replace(self, old, new, count=None):
  213. if count is None:
  214. return self.__unicode__().replace(old, new)
  215. return self.__unicode__().replace(old, new, count)
  216. @inheritdoc
  217. def rfind(self, sub, start=None, end=None):
  218. return self.__unicode__().rfind(sub, start, end)
  219. @inheritdoc
  220. def rindex(self, sub, start=None, end=None):
  221. return self.__unicode__().rindex(sub, start, end)
  222. @inheritdoc
  223. def rjust(self, width, fillchar=None):
  224. if fillchar is None:
  225. return self.__unicode__().rjust(width)
  226. return self.__unicode__().rjust(width, fillchar)
  227. @inheritdoc
  228. def rpartition(self, sep):
  229. return self.__unicode__().rpartition(sep)
  230. @inheritdoc
  231. def rsplit(self, sep=None, maxsplit=None):
  232. if maxsplit is None:
  233. if sep is None:
  234. return self.__unicode__().rsplit()
  235. return self.__unicode__().rsplit(sep)
  236. return self.__unicode__().rsplit(sep, maxsplit)
  237. @inheritdoc
  238. def rstrip(self, chars=None):
  239. return self.__unicode__().rstrip(chars)
  240. @inheritdoc
  241. def split(self, sep=None, maxsplit=None):
  242. if maxsplit is None:
  243. if sep is None:
  244. return self.__unicode__().split()
  245. return self.__unicode__().split(sep)
  246. return self.__unicode__().split(sep, maxsplit)
  247. @inheritdoc
  248. def splitlines(self, keepends=None):
  249. if keepends is None:
  250. return self.__unicode__().splitlines()
  251. return self.__unicode__().splitlines(keepends)
  252. @inheritdoc
  253. def startswith(self, prefix, start=None, end=None):
  254. return self.__unicode__().startswith(prefix, start, end)
  255. @inheritdoc
  256. def strip(self, chars=None):
  257. return self.__unicode__().strip(chars)
  258. @inheritdoc
  259. def swapcase(self):
  260. return self.__unicode__().swapcase()
  261. @inheritdoc
  262. def title(self):
  263. return self.__unicode__().title()
  264. @inheritdoc
  265. def translate(self, table):
  266. return self.__unicode__().translate(table)
  267. @inheritdoc
  268. def upper(self):
  269. return self.__unicode__().upper()
  270. @inheritdoc
  271. def zfill(self, width):
  272. return self.__unicode__().zfill(width)
  273. del inheritdoc