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.
 
 
 
 

104 lines
3.3 KiB

  1. #
  2. # Copyright (C) 2012-2020 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 ``str`` in a dynamic manner.
  36. To use this class, inherit from it and override the :meth:`__str__` method
  37. to return the string representation of the object. The various string
  38. methods will operate on the value of :meth:`__str__` instead of the
  39. immutable ``self`` like the regular ``str`` type.
  40. """
  41. def __str__(self):
  42. raise NotImplementedError()
  43. def __bytes__(self):
  44. return bytes(self.__str__(), getdefaultencoding())
  45. def __repr__(self):
  46. return repr(self.__str__())
  47. def __lt__(self, other):
  48. return self.__str__() < other
  49. def __le__(self, other):
  50. return self.__str__() <= other
  51. def __eq__(self, other):
  52. return self.__str__() == other
  53. def __ne__(self, other):
  54. return self.__str__() != other
  55. def __gt__(self, other):
  56. return self.__str__() > other
  57. def __ge__(self, other):
  58. return self.__str__() >= other
  59. def __bool__(self):
  60. return bool(self.__str__())
  61. def __len__(self):
  62. return len(self.__str__())
  63. def __iter__(self):
  64. yield from self.__str__()
  65. def __getitem__(self, key):
  66. return self.__str__()[key]
  67. def __reversed__(self):
  68. return reversed(self.__str__())
  69. def __contains__(self, item):
  70. return str(item) in self.__str__()
  71. def __getattr__(self, attr):
  72. if not hasattr(str, attr):
  73. raise AttributeError("{!r} object has no attribute {!r}".format(
  74. type(self).__name__, attr))
  75. return getattr(self.__str__(), attr)
  76. maketrans = str.maketrans # Static method can't rely on __getattr__
  77. del inheritdoc