A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

103 linhas
3.3 KiB

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