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.

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