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.4 KiB

  1. #
  2. # Copyright (C) 2012-2016 Ben Kurtovic <ben.kurtovic@gmail.com>
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. """
  22. This module contains the :class:`.StringMixIn` type, which implements the
  23. interface for the ``str`` type in a dynamic manner.
  24. """
  25. from sys import getdefaultencoding
  26. __all__ = ["StringMixIn"]
  27. def inheritdoc(method):
  28. """Set __doc__ of *method* to __doc__ of *method* in its parent class.
  29. Since this is used on :class:`.StringMixIn`, the "parent class" used is
  30. ``str``. This function can be used as a decorator.
  31. """
  32. method.__doc__ = getattr(str, method.__name__).__doc__
  33. return method
  34. class StringMixIn:
  35. """Implement the interface for ``unicode``/``str`` in a dynamic manner.
  36. To use this class, inherit from it and override the :meth:`__unicode__`
  37. method to return the string representation of the object.
  38. The various string methods will operate on the value of :meth:`__unicode__`
  39. instead of the immutable ``self`` like the regular ``str`` type.
  40. """
  41. def __str__(self):
  42. return self.__unicode__()
  43. def __bytes__(self):
  44. return bytes(self.__unicode__(), getdefaultencoding())
  45. def __unicode__(self):
  46. raise NotImplementedError()
  47. def __repr__(self):
  48. return repr(self.__unicode__())
  49. def __lt__(self, other):
  50. return self.__unicode__() < other
  51. def __le__(self, other):
  52. return self.__unicode__() <= other
  53. def __eq__(self, other):
  54. return self.__unicode__() == other
  55. def __ne__(self, other):
  56. return self.__unicode__() != other
  57. def __gt__(self, other):
  58. return self.__unicode__() > other
  59. def __ge__(self, other):
  60. return self.__unicode__() >= other
  61. def __bool__(self):
  62. return bool(self.__unicode__())
  63. def __len__(self):
  64. return len(self.__unicode__())
  65. def __iter__(self):
  66. yield from self.__unicode__()
  67. def __getitem__(self, key):
  68. return self.__unicode__()[key]
  69. def __reversed__(self):
  70. return reversed(self.__unicode__())
  71. def __contains__(self, item):
  72. return str(item) in self.__unicode__()
  73. def __getattr__(self, attr):
  74. if not hasattr(str, attr):
  75. raise AttributeError("{!r} object has no attribute {!r}".format(
  76. type(self).__name__, attr))
  77. return getattr(self.__unicode__(), attr)
  78. maketrans = str.maketrans # Static method can't rely on __getattr__
  79. del inheritdoc