Updated to allow help-getting.

This commit is contained in:
Jeremy Fincher 2004-01-13 15:56:58 +00:00
parent 20984842f3
commit 2ea181073b

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
### ###
# Copyright (c) 2002, Jeremiah Fincher # Copyright (c) 2004, Jeremiah Fincher
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -47,7 +47,7 @@ class NonExistentRegistryEntry(RegistryException):
class Value(object): class Value(object):
def __init__(self, default, help): def __init__(self, default, help):
self.help = help self.help = utils.normalizeWhitespace(help)
self.value = default self.value = default
def set(self, s): def set(self, s):
@ -107,6 +107,10 @@ class Group(object):
self.__dict__['children'] = {} self.__dict__['children'] = {}
self.__dict__['originals'] = {} self.__dict__['originals'] = {}
def __nonExistentEntry(self, attr):
s = '%s is not a valid entry in %s' % (attr, self.name)
raise NonExistentRegistryEntry, s
def __getattr__(self, attr): def __getattr__(self, attr):
original = attr original = attr
attr = attr.lower() attr = attr.lower()
@ -115,8 +119,7 @@ class Group(object):
elif attr in self.children: elif attr in self.children:
return self.children[attr] return self.children[attr]
else: else:
s = '%s is not a valid attribute of %s' % (original, self.name) self.__nonExistentEntry(original)
raise NonExistentRegistryEntry, s
def __setattr__(self, attr, s): def __setattr__(self, attr, s):
original = attr original = attr
@ -126,11 +129,20 @@ class Group(object):
elif attr in self.children and hasattr(self.children[attr], 'set'): elif attr in self.children and hasattr(self.children[attr], 'set'):
self.children[attr].set(s) self.children[attr].set(s)
else: else:
s = '%s is not a valid attribute of %s' % (original, self.name) self.__nonExistentEntry(original)
raise NonExistentRegistryEntry, s
def get(self, attr): def get(self, attr):
return self.__getattr__(attr) return self.__getattr__(attr)
def help(self, attr):
original = attr
attr = attr.lower()
if attr in self.values:
return self.values[attr].help
elif attr in self.children and hasattr(self.children[attr], 'help'):
return self.children[attr].help
else:
self.__nonExistentEntry(original)
def setName(self, name): def setName(self, name):
self.__dict__['name'] = name self.__dict__['name'] = name
@ -144,7 +156,7 @@ class Group(object):
if name in self.values: if name in self.values:
value.set(str(self.values[name])) value.set(str(self.values[name]))
self.values[name] = value self.values[name] = value
self.originals[original] = name self.originals[name] = original
def registerGroup(self, name, group=None): def registerGroup(self, name, group=None):
original = name original = name
@ -155,7 +167,7 @@ class Group(object):
group.__dict__['values'] = self.children[name].values group.__dict__['values'] = self.children[name].values
group.__dict__['children'] = self.children[name].children group.__dict__['children'] = self.children[name].children
self.children[name] = group self.children[name] = group
self.originals[original] = group self.originals[name] = original
group.setName('%s.%s' % (self.name, name)) group.setName('%s.%s' % (self.name, name))
def getValues(self): def getValues(self):
@ -175,6 +187,7 @@ class GroupWithDefault(Group):
def __init__(self, value): def __init__(self, value):
Group.__init__(self) Group.__init__(self)
self.__dict__['value'] = value self.__dict__['value'] = value
self.__dict__['help'] = value.help
def __getattr__(self, attr): def __getattr__(self, attr):
try: try:
@ -186,8 +199,9 @@ class GroupWithDefault(Group):
try: try:
Group.__setattr__(self, attr, s) Group.__setattr__(self, attr, s)
except NonExistentRegistryEntry: except NonExistentRegistryEntry:
self.values[attr] = copy.copy(self.value) v = copy.copy(self.value)
self.values[attr].set(s) v.set(s)
self.register(attr, v)
def set(self, *args): def set(self, *args):
if len(args) == 1: if len(args) == 1:
@ -220,6 +234,9 @@ 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 supybot.help('throttleTime')
print supybot.plugins.topic.help('separator')
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: