A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
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.

105 lines
3.1 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-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 collections import OrderedDict
  23. __all__ = ["ConfigNode"]
  24. class ConfigNode(object):
  25. def __init__(self):
  26. self._data = OrderedDict()
  27. def __repr__(self):
  28. return self._data
  29. def __nonzero__(self):
  30. return bool(self._data)
  31. def __len__(self):
  32. return len(self._data)
  33. def __getitem__(self, key):
  34. return self._data[key]
  35. def __setitem__(self, key, item):
  36. self._data[key] = item
  37. def __getattr__(self, key):
  38. if key == "_data":
  39. return super(ConfigNode, self).__getattr__(key)
  40. return self._data[key]
  41. def __setattr__(self, key, item):
  42. if key == "_data":
  43. super(ConfigNode, self).__setattr__(key, item)
  44. else:
  45. self._data[key] = item
  46. def __iter__(self):
  47. for key in self._data:
  48. yield key
  49. def __contains__(self, item):
  50. return item in self._data
  51. def _dump(self):
  52. data = self._data.copy()
  53. for key, val in data.iteritems():
  54. if isinstance(val, ConfigNode):
  55. data[key] = val._dump()
  56. return data
  57. def _load(self, data):
  58. self._data = data.copy()
  59. def _decrypt(self, cipher, intermediates, item):
  60. base = self._data
  61. for inter in intermediates:
  62. try:
  63. base = base[inter]
  64. except KeyError:
  65. return
  66. if item in base:
  67. ciphertext = base[item].decode("hex")
  68. base[item] = cipher.decrypt(ciphertext).rstrip("\x00")
  69. def get(self, *args, **kwargs):
  70. return self._data.get(*args, **kwargs)
  71. def keys(self):
  72. return self._data.keys()
  73. def values(self):
  74. return self._data.values()
  75. def items(self):
  76. return self._data.items()
  77. def iterkeys(self):
  78. return self._data.iterkeys()
  79. def itervalues(self):
  80. return self._data.itervalues()
  81. def iteritems(self):
  82. return self._data.iteritems()