Skip to content

Commit

Permalink
Add the content of the new value of a config var, when it is not comp…
Browse files Browse the repository at this point in the history
…atible with this config type.
  • Loading branch information
progval committed Dec 12, 2012
1 parent d9f7e0e commit ad3f58e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
2 changes: 1 addition & 1 deletion plugins/Config/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def testConfigErrors(self):
self.assertRegexp('config supybot.replies.', 'not a valid')
self.assertRegexp('config supybot.repl', 'not a valid')
self.assertRegexp('config supybot.reply.withNickPrefix 123',
'True or False')
'True or False.*, not \'123\'.')
self.assertRegexp('config supybot.replies foo', 'settable')

# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
Expand Down
66 changes: 45 additions & 21 deletions src/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import supybot.utils as utils
import supybot.i18n as i18n

_ = i18n.PluginInternationalization()

def error(s):
"""Replace me with something better from another module!"""
print '***', s
Expand Down Expand Up @@ -308,6 +310,9 @@ def getValues(self, getChildren=False, fullNames=True):
L = [(split(s)[-1], node) for (s, node) in L]
return L

class _NoValueGiven:
# Special value for Value.error()
pass

class Value(Group):
"""Invalid registry value. If you're getting this message, report it,
Expand All @@ -323,8 +328,13 @@ def __init__(self, default, help, setDefault=True,
if setDefault:
self.setValue(default)

def error(self):
if self.__doc__:
def error(self, value=_NoValueGiven):
if hasattr(self, 'errormsg') and value is not _NoValueGiven:
try:
s = self.errormsg % value
except TypeError:
s = self.errormsg
elif self.__doc__:
s = self.__doc__
else:
s = """%s has no docstring. If you're getting this message,
Expand Down Expand Up @@ -400,65 +410,74 @@ def __call__(self):

class Boolean(Value):
"""Value must be either True or False (or On or Off)."""
errormsg = _('Value must be either True or False (or On or Off), not %r.')
def set(self, s):
try:
v = utils.str.toBool(s)
except ValueError:
if s.strip().lower() == 'toggle':
v = not self.value
else:
self.error()
self.error(s)
self.setValue(v)

def setValue(self, v):
super(Boolean, self).setValue(bool(v))

class Integer(Value):
"""Value must be an integer."""
errormsg = _('Value must be an integer, not %r.')
def set(self, s):
try:
self.setValue(int(s))
except ValueError:
self.error()
self.error(s)

class NonNegativeInteger(Integer):
"""Value must be a non-negative integer."""
errormsg = _('Value must be a non-negative integer, not %r.')
def setValue(self, v):
if v < 0:
self.error()
self.error(v)
super(NonNegativeInteger, self).setValue(v)

class PositiveInteger(NonNegativeInteger):
"""Value must be positive (non-zero) integer."""
errormsg = _('Value must be positive (non-zero) integer, not %r.')
def setValue(self, v):
if not v:
self.error()
self.error(v)
super(PositiveInteger, self).setValue(v)

class Float(Value):
"""Value must be a floating-point number."""
errormsg = _('Value must be a floating-point number, not %r.')
def set(self, s):
try:
self.setValue(float(s))
except ValueError:
self.error()
self.error(s)

def setValue(self, v):
try:
super(Float, self).setValue(float(v))
except ValueError:
self.error()
self.error(v)

class PositiveFloat(Float):
"""Value must be a floating-point number greater than zero."""
errormsg = _('Value must be a floating-point number greater than zero, '
'not %r.')
def setValue(self, v):
if v <= 0:
self.error()
self.error(v)
else:
super(PositiveFloat, self).setValue(v)

class Probability(Float):
"""Value must be a floating point number in the range [0, 1]."""
errormsg = _('Value must be a floating point number in the range [0, 1], '
'not %r.')
def __init__(self, *args, **kwargs):
self.__parent = super(Probability, self)
self.__parent.__init__(*args, **kwargs)
Expand All @@ -467,22 +486,24 @@ def setValue(self, v):
if 0 <= v <= 1:
self.__parent.setValue(v)
else:
self.error()
self.error(v)

class String(Value):
"""Value is not a valid Python string."""
errormsg = _('Value is not a valid Python string, not %r.')
def set(self, s):
if not s:
s = '""'
elif s[0] != s[-1] or s[0] not in '\'"':
s = repr(s)
v = s
if not v:
v = '""'
elif v[0] != v[-1] or v[0] not in '\'"':
v = repr(v)
try:
v = utils.safeEval(s)
v = utils.safeEval(v)
if not isinstance(v, basestring):
raise ValueError
self.setValue(v)
except ValueError: # This catches utils.safeEval(s) errors too.
self.error()
self.error(s)

_printable = string.printable[:-4]
def _needsQuoting(self, s):
Expand All @@ -501,7 +522,9 @@ def __init__(self, *args, **kwargs):
'This is a bug.'
self.__parent = super(OnlySomeStrings, self)
self.__parent.__init__(*args, **kwargs)
self.__doc__ = format('Valid values include %L.',
self.__doc__ = format(_('Valid values include %L.'),
map(repr, self.validStrings))
self.errormsg = format(_('Valid values include %L, not %%r.'),
map(repr, self.validStrings))

def help(self):
Expand All @@ -518,11 +541,11 @@ def normalize(self, s):
return self.validStrings[i]

def setValue(self, s):
s = self.normalize(s)
v = self.normalize(s)
if s in self.validStrings:
self.__parent.setValue(s)
self.__parent.setValue(v)
else:
self.error()
self.error(v)

class NormalizedString(String):
def __init__(self, default, *args, **kwargs):
Expand Down Expand Up @@ -572,6 +595,7 @@ def setValue(self, v):

class Regexp(Value):
"""Value must be a valid regular expression."""
errormsg = _('Value must be a valid regular expression, not %r.')
def __init__(self, *args, **kwargs):
kwargs['setDefault'] = False
self.sr = ''
Expand Down Expand Up @@ -677,7 +701,7 @@ def hasTemplate(s):
if utils.iter.all(hasTemplate, self.requiredTemplates):
self.__parent.setValue(v)
else:
self.error()
self.error(v)

class Json(String):
# Json-serializable data
Expand Down

0 comments on commit ad3f58e

Please sign in to comment.