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.

parameter.py 3.0 KiB

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