Update to use a help method instead of a help string. Allows sub-classes of

Group/Value to define their own help method.
This commit is contained in:
James Vega 2004-09-25 17:06:05 +00:00
parent b9db0330e4
commit 9291c56e3f
2 changed files with 21 additions and 14 deletions

View File

@ -240,7 +240,7 @@ class Config(callbacks.Privmsg):
name = self._canonicalizeName(name) name = self._canonicalizeName(name)
wrapper = getWrapper(name) wrapper = getWrapper(name)
if hasattr(wrapper, 'help'): if hasattr(wrapper, 'help'):
s = wrapper.help s = wrapper.help()
if s: if s:
if hasattr(wrapper, 'value') and not wrapper._private: if hasattr(wrapper, 'value') and not wrapper._private:
s += ' (Current value: %s)' % wrapper s += ' (Current value: %s)' % wrapper
@ -249,7 +249,6 @@ class Config(callbacks.Privmsg):
irc.reply('That configuration group exists, but seems to have ' irc.reply('That configuration group exists, but seems to have '
'no help. Try "config list %s" to see if it has ' 'no help. Try "config list %s" to see if it has '
'any children values.') 'any children values.')
else: else:
irc.error('%s has no help.' % name) irc.error('%s has no help.' % name)

View File

@ -80,10 +80,10 @@ def close(registry, filename, annotated=True, helpOnceOnly=False):
helpCache = sets.Set() helpCache = sets.Set()
fd = utils.transactionalFile(filename) fd = utils.transactionalFile(filename)
for (name, value) in registry.getValues(getChildren=True): for (name, value) in registry.getValues(getChildren=True):
if annotated and hasattr(value,'help') and value.help: if annotated and hasattr(value,'_help') and value._help:
if not helpOnceOnly or value.help not in helpCache: if not helpOnceOnly or value._help not in helpCache:
helpCache.add(value.help) helpCache.add(value._help)
lines = textwrap.wrap(value.help) lines = textwrap.wrap(value._help)
for (i, line) in enumerate(lines): for (i, line) in enumerate(lines):
lines[i] = '# %s\n' % line lines[i] = '# %s\n' % line
lines.insert(0, '###\n') lines.insert(0, '###\n')
@ -136,7 +136,7 @@ class Group(object):
"""A group; it doesn't hold a value unless handled by a subclass.""" """A group; it doesn't hold a value unless handled by a subclass."""
def __init__(self, help='', def __init__(self, help='',
supplyDefault=False, orderAlphabetically=False): supplyDefault=False, orderAlphabetically=False):
self.help = help self._help = help
self._name = 'unset' self._name = 'unset'
self._added = [] self._added = []
self._children = utils.InsensitivePreservingDict() self._children = utils.InsensitivePreservingDict()
@ -163,11 +163,11 @@ class Group(object):
raise NonExistentRegistryEntry, s raise NonExistentRegistryEntry, s
def __makeChild(self, attr, s): def __makeChild(self, attr, s):
v = self.__class__(self._default, self.help) v = self.__class__(self._default, self._help)
v.set(s) v.set(s)
v.__class__ = self.X v.__class__ = self.X
v._supplyDefault = False v._supplyDefault = False
v.help = '' # Clear this so it doesn't print a bazillion times. v._help = '' # Clear this so it doesn't print a bazillion times.
self.register(attr, v) self.register(attr, v)
return v return v
@ -179,6 +179,9 @@ class Group(object):
else: else:
self.__nonExistentEntry(attr) self.__nonExistentEntry(attr)
def help(self):
return self._help
def get(self, attr): def get(self, attr):
# Not getattr(self, attr) because some nodes might have groups that # Not getattr(self, attr) because some nodes might have groups that
# are named the same as their methods. # are named the same as their methods.
@ -270,7 +273,7 @@ class Value(Group):
self._default = default self._default = default
self._private = private self._private = private
self.showDefault = showDefault self.showDefault = showDefault
self.help = utils.normalizeWhitespace(help.strip()) self._help = utils.normalizeWhitespace(help.strip())
if setDefault: if setDefault:
self.setValue(default) self.setValue(default)
@ -400,7 +403,7 @@ class String(Value):
_printable = string.printable[:-4] _printable = string.printable[:-4]
def _needsQuoting(self, s): def _needsQuoting(self, s):
return s.translate(string.ascii, self._printable) and s.strip() != s return s.translate(string.ascii, self._printable) and s.strip() != s
def __str__(self): def __str__(self):
s = self.value s = self.value
if self._needsQuoting(s): if self._needsQuoting(s):
@ -415,6 +418,11 @@ class OnlySomeStrings(String):
self.__parent = super(OnlySomeStrings, self) self.__parent = super(OnlySomeStrings, self)
self.__parent.__init__(*args, **kwargs) self.__parent.__init__(*args, **kwargs)
def help(self):
strings = [s for s in self.validStrings if s]
return '%s Valid strings: %s.' % \
(self._help, utils.commaAndify(strings))
def error(self): def error(self):
raise InvalidRegistryValue, \ raise InvalidRegistryValue, \
'That is not a valid value. Valid values include %s.' % \ 'That is not a valid value. Valid values include %s.' % \
@ -441,10 +449,10 @@ class NormalizedString(String):
default = self.normalize(default) default = self.normalize(default)
self.__parent = super(NormalizedString, self) self.__parent = super(NormalizedString, self)
self.__parent.__init__(default, *args, **kwargs) self.__parent.__init__(default, *args, **kwargs)
def normalize(self, s): def normalize(self, s):
return utils.normalizeWhitespace(s.strip()) return utils.normalizeWhitespace(s.strip())
def set(self, s): def set(self, s):
s = self.normalize(s) s = self.normalize(s)
self.__parent.set(s) self.__parent.set(s)
@ -474,7 +482,7 @@ class Regexp(Value):
self.sr = '' self.sr = ''
self.value = None self.value = None
super(Regexp, self).__init__(*args, **kwargs) super(Regexp, self).__init__(*args, **kwargs)
def error(self, e): def error(self, e):
raise InvalidRegistryValue, 'Value must be a regexp of the form %s' % e raise InvalidRegistryValue, 'Value must be a regexp of the form %s' % e