Added writing!

This commit is contained in:
Jeremy Fincher 2004-01-14 06:05:58 +00:00
parent fe0e5e9c43
commit 3319e2562d

View File

@ -45,6 +45,22 @@ class InvalidRegistryValue(RegistryException):
class NonExistentRegistryEntry(RegistryException): class NonExistentRegistryEntry(RegistryException):
pass pass
cache = {}
def open(filename):
"""Initializes the module by loading the registry file into memory."""
cache.clear()
fd = utils.nonCommentNonEmptyLines(file(filename))
for line in fd:
line = line.rstrip()
(key, value) = line.split(': ', 1)
cache[key] = value
def close(registry, filename):
fd = file(filename, 'w')
for (name, value) in registry.getValues(askChildren=True):
fd.write('%s: %s\n' % (name, value))
fd.close()
class Value(object): class Value(object):
def __init__(self, default, help): def __init__(self, default, help):
self.help = utils.normalizeWhitespace(help) self.help = utils.normalizeWhitespace(help)
@ -157,6 +173,10 @@ class Group(object):
value.set(str(self.values[name])) value.set(str(self.values[name]))
self.values[name] = value self.values[name] = value
self.originals[name] = original self.originals[name] = original
if cache:
fullname = '%s.%s' % (self.name, name)
if fullname in cache:
value.set(cache[fullname])
def registerGroup(self, name, group=None): def registerGroup(self, name, group=None):
original = name original = name
@ -168,18 +188,22 @@ class Group(object):
group.__dict__['children'] = self.children[name].children group.__dict__['children'] = self.children[name].children
self.children[name] = group self.children[name] = group
self.originals[name] = original self.originals[name] = original
group.setName('%s.%s' % (self.name, name)) fullname = '%s.%s' % (self.name, name)
group.setName(fullname)
if cache and fullname in cache:
group.set(cache[fullname])
def getValues(self): def getValues(self, askChildren=False):
L = [] L = []
items = self.values.items() items = self.values.items()
items.sort() items.sort()
for (name, value) in items: for (name, value) in items:
L.append(('%s.%s' % (self.getName(), name), str(value))) L.append(('%s.%s' % (self.getName(), name), str(value)))
items = self.children.items() if askChildren:
items.sort() items = self.children.items()
for (_, child) in items: items.sort()
L.extend(child.getValues()) for (_, child) in items:
L.extend(child.getValues(askChildren))
return L return L
@ -189,6 +213,11 @@ class GroupWithDefault(Group):
self.__dict__['value'] = value self.__dict__['value'] = value
self.__dict__['help'] = value.help self.__dict__['help'] = value.help
def __makeChild(self, attr, s):
v = copy.copy(self.value)
v.set(s)
self.register(attr, v)
def __getattr__(self, attr): def __getattr__(self, attr):
try: try:
return Group.__getattr__(self, attr) return Group.__getattr__(self, attr)
@ -199,9 +228,14 @@ class GroupWithDefault(Group):
try: try:
Group.__setattr__(self, attr, s) Group.__setattr__(self, attr, s)
except NonExistentRegistryEntry: except NonExistentRegistryEntry:
v = copy.copy(self.value) self.__makeChild(attr, s)
v.set(s)
self.register(attr, v) def setName(self, name):
Group.setName(self, name)
for (k, v) in cache.iteritems():
if k.startswith(self.name):
(_, group) = rsplit(k, '.', 1)
self.__makeChild(group, v)
def set(self, *args): def set(self, *args):
if len(args) == 1: if len(args) == 1:
@ -211,8 +245,8 @@ class GroupWithDefault(Group):
(attr, s) = args (attr, s) = args
self.__setattr__(attr, s) self.__setattr__(attr, s)
def getValues(self): def getValues(self, askChildren=False):
L = Group.getValues(self) L = Group.getValues(self, askChildren)
L.insert(0, (self.getName(), str(self.value))) L.insert(0, (self.getName(), str(self.value)))
return L return L
@ -235,6 +269,13 @@ if __name__ == '__main__':
for (k, v) in supybot.getValues(): for (k, v) in supybot.getValues():
print '%s: %s' % (k, v) print '%s: %s' % (k, v)
print
print 'Asking children'
print
for (k, v) in supybot.getValues(askChildren=True):
print '%s: %s' % (k, v)
print supybot.help('throttleTime') print supybot.help('throttleTime')
print supybot.plugins.topic.help('separator') print supybot.plugins.topic.help('separator')