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.
 
 
 
 

128 lines
4.0 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2014 Ben Kurtovic <ben.kurtovic@gmail.com>
  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 sys import getdefaultencoding
  28. from .compat import bytes, py26, py3k, str
  29. __all__ = ["StringMixIn"]
  30. def inheritdoc(method):
  31. """Set __doc__ of *method* to __doc__ of *method* in its parent class.
  32. Since this is used on :py:class:`~.StringMixIn`, the "parent class" used is
  33. ``str``. This function can be used as a decorator.
  34. """
  35. method.__doc__ = getattr(str, method.__name__).__doc__
  36. return method
  37. class StringMixIn(object):
  38. """Implement the interface for ``unicode``/``str`` in a dynamic manner.
  39. To use this class, inherit from it and override the :py:meth:`__unicode__`
  40. method (same on py3k) to return the string representation of the object.
  41. The various string methods will operate on the value of
  42. :py:meth:`__unicode__` instead of the immutable ``self`` like the regular
  43. ``str`` type.
  44. """
  45. if py3k:
  46. def __str__(self):
  47. return self.__unicode__()
  48. def __bytes__(self):
  49. return bytes(self.__unicode__(), getdefaultencoding())
  50. else:
  51. def __str__(self):
  52. return bytes(self.__unicode__())
  53. def __unicode__(self):
  54. raise NotImplementedError()
  55. def __repr__(self):
  56. return repr(self.__unicode__())
  57. def __lt__(self, other):
  58. return self.__unicode__() < other
  59. def __le__(self, other):
  60. return self.__unicode__() <= other
  61. def __eq__(self, other):
  62. return self.__unicode__() == other
  63. def __ne__(self, other):
  64. return self.__unicode__() != other
  65. def __gt__(self, other):
  66. return self.__unicode__() > other
  67. def __ge__(self, other):
  68. return self.__unicode__() >= other
  69. if py3k:
  70. def __bool__(self):
  71. return bool(self.__unicode__())
  72. else:
  73. def __nonzero__(self):
  74. return bool(self.__unicode__())
  75. def __len__(self):
  76. return len(self.__unicode__())
  77. def __iter__(self):
  78. for char in self.__unicode__():
  79. yield char
  80. def __getitem__(self, key):
  81. return self.__unicode__()[key]
  82. def __reversed__(self):
  83. return reversed(self.__unicode__())
  84. def __contains__(self, item):
  85. return str(item) in self.__unicode__()
  86. def __getattr__(self, attr):
  87. return getattr(self.__unicode__(), attr)
  88. if py3k:
  89. maketrans = str.maketrans # Static method can't rely on __getattr__
  90. if py26:
  91. @inheritdoc
  92. def encode(self, encoding=None, errors=None):
  93. if encoding is None:
  94. encoding = getdefaultencoding()
  95. if errors is not None:
  96. return self.__unicode__().encode(encoding, errors)
  97. return self.__unicode__().encode(encoding)
  98. del inheritdoc