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.
 
 
 
 

137 lines
4.3 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012 by Ben Kurtovic <ben.kurtovic@verizon.net>
  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. from mwparserfromhell.template import Template
  23. __all__ = ["Parameter"]
  24. class Parameter(object):
  25. def __init__(self, name, value, templates=None):
  26. self._name = name
  27. self._value = value
  28. if templates:
  29. self._templates = templates
  30. else:
  31. self._templates = []
  32. def __repr__(self):
  33. return repr(self.value)
  34. def __str__(self):
  35. return self.value
  36. def __lt__(self, other):
  37. if isinstance(other, Parameter):
  38. return self.value < other.value
  39. return self.value < other
  40. def __le__(self, other):
  41. if isinstance(other, Parameter):
  42. return self.value <= other.value
  43. return self.value <= other
  44. def __eq__(self, other):
  45. if isinstance(other, Parameter):
  46. return (self.name == other.name and self.value == other.value and
  47. self.templates == other.templates)
  48. return self.value == other
  49. def __ne__(self, other):
  50. if isinstance(other, Parameter):
  51. return (self.name != other.name or self.value != other.value or
  52. self.templates != other.templates)
  53. return self.value != other
  54. def __gt__(self, other):
  55. if isinstance(other, Parameter):
  56. return self.value > other.value
  57. return self.value > other
  58. def __ge__(self, other):
  59. if isinstance(other, Parameter):
  60. return self.value >= other.value
  61. return self.value >= other
  62. def __nonzero__(self):
  63. return bool(self.value)
  64. def __len__(self):
  65. return len(self.value)
  66. def __iter__(self):
  67. for char in self.value:
  68. yield char
  69. def __getitem__(self, key):
  70. return self.value[key]
  71. def __contains__(self, item):
  72. return item in self.value or item in self.templates
  73. def __add__(self, other):
  74. if isinstance(other, Parameter):
  75. return Parameter(self.name, self.value + other.value,
  76. self.templates + other.templates)
  77. if isinstance(other, Template):
  78. return Parameter(self.name, self.value + other.render(),
  79. self.templates + [other])
  80. return self.value + other
  81. def __radd__(self, other):
  82. if isinstance(other, Template):
  83. return Template(other.name, other.params + [self])
  84. return other + self.value
  85. def __iadd__(self, other):
  86. if isinstance(other, Parameter):
  87. self.value += other.value
  88. self.templates += other.templates
  89. elif isinstance(other, Template):
  90. self.value += other.render()
  91. self.templates.append(other)
  92. else:
  93. self.value += other
  94. return self
  95. def __mul__(self, other):
  96. return Parameter(self.name, self.value * other, self.templates * other)
  97. def __rmul__(self, other):
  98. return Parameter(self.name, other * self.value, other * self.templates)
  99. def __imul__(self, other):
  100. self.value *= other
  101. self.templates *= other
  102. return self
  103. @property
  104. def name(self):
  105. return self._name
  106. @property
  107. def value(self):
  108. return self._value
  109. @property
  110. def templates(self):
  111. return self._templates