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.

parameter.py 2.8 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #
  2. # Copyright (C) 2012-2019 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. import re
  22. from ...string_mixin import StringMixIn
  23. from ...utils import parse_anything
  24. __all__ = ["Parameter"]
  25. class Parameter(StringMixIn):
  26. """Represents a paramater of a template.
  27. For example, the template ``{{foo|bar|spam=eggs}}`` contains two
  28. Parameters: one whose name is ``"1"``, value is ``"bar"``, and ``showkey``
  29. is ``False``, and one whose name is ``"spam"``, value is ``"eggs"``, and
  30. ``showkey`` is ``True``.
  31. """
  32. def __init__(self, name, value, showkey=True):
  33. super().__init__()
  34. self.name = name
  35. self.value = value
  36. self.showkey = showkey
  37. def __unicode__(self):
  38. if self.showkey:
  39. return str(self.name) + "=" + str(self.value)
  40. return str(self.value)
  41. @staticmethod
  42. def can_hide_key(key):
  43. """Return whether or not the given key can be hidden."""
  44. return re.match(r"[1-9][0-9]*$", str(key).strip())
  45. @property
  46. def name(self):
  47. """The name of the parameter as a :class:`.Wikicode` object."""
  48. return self._name
  49. @property
  50. def value(self):
  51. """The value of the parameter as a :class:`.Wikicode` object."""
  52. return self._value
  53. @property
  54. def showkey(self):
  55. """Whether to show the parameter's key (i.e., its "name")."""
  56. return self._showkey
  57. @name.setter
  58. def name(self, newval):
  59. self._name = parse_anything(newval)
  60. @value.setter
  61. def value(self, newval):
  62. self._value = parse_anything(newval)
  63. @showkey.setter
  64. def showkey(self, newval):
  65. newval = bool(newval)
  66. if not newval and not self.can_hide_key(self.name):
  67. raise ValueError("parameter key {!r} cannot be hidden".format(
  68. self.name))
  69. self._showkey = newval