A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

108 rindas
4.1 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 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. """
  23. Based on:
  24. * https://gist.github.com/844388
  25. * http://pyyaml.org/attachment/ticket/161/use_ordered_dict.py
  26. with modifications.
  27. """
  28. from collections import OrderedDict
  29. try:
  30. import yaml
  31. except ImportError:
  32. yaml = None
  33. __all__ = ["OrderedLoader", "OrderedDumper"]
  34. class OrderedLoader(yaml.Loader):
  35. """A YAML loader that loads mappings into ordered dictionaries."""
  36. def __init__(self, *args, **kwargs):
  37. super(OrderedLoader, self).__init__(*args, **kwargs)
  38. constructor = type(self).construct_yaml_map
  39. self.add_constructor(u"tag:yaml.org,2002:map", constructor)
  40. self.add_constructor(u"tag:yaml.org,2002:omap", constructor)
  41. def construct_yaml_map(self, node):
  42. data = OrderedDict()
  43. yield data
  44. value = self.construct_mapping(node)
  45. data.update(value)
  46. def construct_mapping(self, node, deep=False):
  47. if isinstance(node, yaml.MappingNode):
  48. self.flatten_mapping(node)
  49. else:
  50. raise yaml.constructor.ConstructorError(None, None,
  51. "expected a mapping node, but found {0}".format(node.id),
  52. node.start_mark)
  53. mapping = OrderedDict()
  54. for key_node, value_node in node.value:
  55. key = self.construct_object(key_node, deep=deep)
  56. try:
  57. hash(key)
  58. except TypeError, exc:
  59. raise yaml.constructor.ConstructorError(
  60. "while constructing a mapping", node.start_mark,
  61. "found unacceptable key ({0})".format(exc),
  62. key_node.start_mark)
  63. value = self.construct_object(value_node, deep=deep)
  64. mapping[key] = value
  65. return mapping
  66. class OrderedDumper(yaml.Dumper):
  67. """A YAML dumper that dumps ordered dictionaries into mappings."""
  68. def __init__(self, *args, **kwargs):
  69. super(OrderedDumper, self).__init__(*args, **kwargs)
  70. self.add_representer(OrderedDict, type(self).represent_dict)
  71. def represent_mapping(self, tag, mapping, flow_style=None):
  72. value = []
  73. node = yaml.MappingNode(tag, value, flow_style=flow_style)
  74. if self.alias_key is not None:
  75. self.represented_objects[self.alias_key] = node
  76. best_style = True
  77. if hasattr(mapping, "items"):
  78. mapping = list(mapping.items())
  79. for item_key, item_value in mapping:
  80. node_key = self.represent_data(item_key)
  81. node_value = self.represent_data(item_value)
  82. if not (isinstance(node_key, yaml.ScalarNode) and not
  83. node_key.style):
  84. best_style = False
  85. if not (isinstance(node_value, yaml.ScalarNode) and not
  86. node_value.style):
  87. best_style = False
  88. value.append((node_key, node_value))
  89. if flow_style is None:
  90. if self.default_flow_style is not None:
  91. node.flow_style = self.default_flow_style
  92. else:
  93. node.flow_style = best_style
  94. return node