A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

65 rader
2.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 collections import OrderedDict
  23. __all__ = ["Template"]
  24. class Template(object):
  25. def __init__(self, name, params=None):
  26. self._name = name
  27. self._params = OrderedDict()
  28. if params:
  29. for param in params:
  30. self._params[param.name] = param
  31. def __repr__(self):
  32. paramlist = []
  33. for name, param in self._params.iteritems():
  34. paramlist.append('"{0}": "{1}"'.format(name, str(param)))
  35. params = "{" + ", ".join(paramlist) + "}"
  36. return "Template(name={0}, params={1})".format(self.name, params)
  37. def __eq__(self, other):
  38. if isinstance(other, Template):
  39. return self.name == other.name and self._params == other._params
  40. return False
  41. def __ne__(self, other):
  42. if isinstance(other, Template):
  43. return self.name != other.name or self._params != other._params
  44. return True
  45. @property
  46. def name(self):
  47. return self._name
  48. @property
  49. def params(self):
  50. return self._params.values()
  51. def get(self, name):
  52. try:
  53. return self._params[name]
  54. except KeyError: # Try lookup by order in param list
  55. return self._params.values()[name]