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.

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