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.
 
 
 
 

107 lines
3.3 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, py3k, str
  29. __all__ = ["StringMixIn"]
  30. class StringMixIn(object):
  31. """Implement the interface for ``unicode``/``str`` in a dynamic manner.
  32. To use this class, inherit from it and override the :py:meth:`__unicode__`
  33. method (same on py3k) to return the string representation of the object.
  34. The various string methods will operate on the value of
  35. :py:meth:`__unicode__` instead of the immutable ``self`` like the regular
  36. ``str`` type.
  37. """
  38. if py3k:
  39. def __str__(self):
  40. return self.__unicode__()
  41. def __bytes__(self):
  42. return bytes(self.__unicode__(), getdefaultencoding())
  43. else:
  44. def __str__(self):
  45. return bytes(self.__unicode__())
  46. def __unicode__(self):
  47. raise NotImplementedError()
  48. def __repr__(self):
  49. return repr(self.__unicode__())
  50. def __lt__(self, other):
  51. return self.__unicode__() < other
  52. def __le__(self, other):
  53. return self.__unicode__() <= other
  54. def __eq__(self, other):
  55. return self.__unicode__() == other
  56. def __ne__(self, other):
  57. return self.__unicode__() != other
  58. def __gt__(self, other):
  59. return self.__unicode__() > other
  60. def __ge__(self, other):
  61. return self.__unicode__() >= other
  62. if py3k:
  63. def __bool__(self):
  64. return bool(self.__unicode__())
  65. else:
  66. def __nonzero__(self):
  67. return bool(self.__unicode__())
  68. def __len__(self):
  69. return len(self.__unicode__())
  70. def __iter__(self):
  71. for char in self.__unicode__():
  72. yield char
  73. def __getitem__(self, key):
  74. return self.__unicode__()[key]
  75. def __reversed__(self):
  76. return reversed(self.__unicode__())
  77. def __contains__(self, item):
  78. return str(item) in self.__unicode__()
  79. def __getattr__(self, attr):
  80. return getattr(self.__unicode__(), attr)
  81. if py3k:
  82. maketrans = str.maketrans # Static method can't rely on __getattr__