Browse Source

Be slightly less stupid about ConfigNodes

tags/v0.1^2
Ben Kurtovic 12 years ago
parent
commit
3cb89711f8
1 changed files with 24 additions and 12 deletions
  1. +24
    -12
      earwigbot/config/node.py

+ 24
- 12
earwigbot/config/node.py View File

@@ -23,25 +23,37 @@
__all__ = ["ConfigNode"] __all__ = ["ConfigNode"]


class ConfigNode(object): class ConfigNode(object):
def __init__(self):
self._data = {}

def __iter__(self): def __iter__(self):
for key in self.__dict__:
for key in self._data:
yield key yield key


def __getitem__(self, item):
return self.__dict__.__getitem__(item)
def __getitem__(self, key):
return self._data[key]

def __setitem__(self, key, item):
self._data[key] = item

def __getattr__(self, key):
return self._data[key]

def __setattr__(self, key, item):
self._data[key] = item


def _dump(self): def _dump(self):
data = self.__dict__.copy()
data = self._data.copy()
for key, val in data.iteritems(): for key, val in data.iteritems():
if isinstance(val, ConfigNode): if isinstance(val, ConfigNode):
data[key] = val._dump() data[key] = val._dump()
return data return data


def _load(self, data): def _load(self, data):
self.__dict__ = data.copy()
self._data = data.copy()


def _decrypt(self, cipher, intermediates, item): def _decrypt(self, cipher, intermediates, item):
base = self.__dict__
base = self._data
for inter in intermediates: for inter in intermediates:
try: try:
base = base[inter] base = base[inter]
@@ -52,22 +64,22 @@ class ConfigNode(object):
base[item] = cipher.decrypt(ciphertext).rstrip("\x00") base[item] = cipher.decrypt(ciphertext).rstrip("\x00")


def get(self, *args, **kwargs): def get(self, *args, **kwargs):
return self.__dict__.get(*args, **kwargs)
return self._data.get(*args, **kwargs)


def keys(self): def keys(self):
return self.__dict__.keys()
return self._data.keys()


def values(self): def values(self):
return self.__dict__.values()
return self._data.values()


def items(self): def items(self):
return self.__dict__.items()
return self._data.items()


def iterkeys(self): def iterkeys(self):
return self.__dict__.iterkeys()
return self._data.iterkeys()


def itervalues(self): def itervalues(self):
return self.__dict__.itervalues()
return self._data.itervalues()


def iteritems(self): def iteritems(self):
return self.__dict__.iteritems() return self.__dict__.iteritems()

Loading…
Cancel
Save