Various updates.

This commit is contained in:
Jeremy Fincher 2004-08-17 06:36:51 +00:00
parent c7170240f2
commit f89bb167d1
2 changed files with 39 additions and 13 deletions

View File

@ -513,8 +513,8 @@ registerGroup(supybot, 'commands')
### ###
registerGroup(supybot, 'drivers') registerGroup(supybot, 'drivers')
registerGlobalValue(supybot.drivers, 'poll', registerGlobalValue(supybot.drivers, 'poll',
registry.Float(1.0, """Determines the default length of time a driver registry.PositiveFloat(1.0, """Determines the default length of time a
should block waiting for input.""")) driver should block waiting for input."""))
class ValidDriverModule(registry.OnlySomeStrings): class ValidDriverModule(registry.OnlySomeStrings):
validStrings = ('default', 'socketDrivers', validStrings = ('default', 'socketDrivers',

View File

@ -34,6 +34,7 @@ __revision__ = "$Id$"
import re import re
import sets import sets
import time import time
import string
import textwrap import textwrap
import supybot.fix as fix import supybot.fix as fix
@ -66,11 +67,12 @@ def open(filename, clear=False):
for (i, line) in enumerate(fd): for (i, line) in enumerate(fd):
line = line.rstrip('\r\n') line = line.rstrip('\r\n')
try: try:
#print '***', repr(line)
(key, value) = re.split(r'(?<!\\):', line, 1) (key, value) = re.split(r'(?<!\\):', line, 1)
key = key.strip() key = key.strip()
value = value.strip() value = value.strip()
except ValueError: except ValueError:
raise InvalidRegistryFile, 'Error unpacking line #%s' % (i+1) raise InvalidRegistryFile, 'Error unpacking line %r' % line
_cache[key] = value _cache[key] = value
_lastModified = time.time() _lastModified = time.time()
_fd.close() _fd.close()
@ -291,6 +293,8 @@ class Value(Group):
if _lastModified > self._lastModified: if _lastModified > self._lastModified:
if self._name in _cache: if self._name in _cache:
self.set(_cache[self._name]) self.set(_cache[self._name])
else:
self.setValue(self._default)
return self.value return self.value
class Boolean(Value): class Boolean(Value):
@ -318,13 +322,6 @@ class Integer(Value):
except ValueError: except ValueError:
self.error() self.error()
class PositiveInteger(Integer):
"""Value must be positive (non-zero) integer."""
def setValue(self, v):
if v <= 0:
self.error()
Integer.setValue(self, v)
class NonNegativeInteger(Integer): class NonNegativeInteger(Integer):
"""Value must not be negative.""" """Value must not be negative."""
def setValue(self, v): def setValue(self, v):
@ -332,6 +329,13 @@ class NonNegativeInteger(Integer):
self.error() self.error()
Integer.setValue(self, v) Integer.setValue(self, v)
class PositiveInteger(NonNegativeInteger):
"""Value must be positive (non-zero) integer."""
def setValue(self, v):
if not v:
self.error()
NonNegativeInteger.setValue(self, v)
class Float(Value): class Float(Value):
"""Value must be a floating-point number.""" """Value must be a floating-point number."""
def set(self, s): def set(self, s):
@ -346,6 +350,14 @@ class Float(Value):
except ValueError: except ValueError:
self.error() self.error()
class PositiveFloat(Float):
"""Value must be a float-point number greater than zero."""
def setValue(self, v):
if v <= 0:
self.error()
else:
Float.setValue(self, v)
class String(Value): class String(Value):
"""Value is not a valid Python string.""" """Value is not a valid Python string."""
def set(self, s): def set(self, s):
@ -361,6 +373,16 @@ class String(Value):
except ValueError: # This catches utils.safeEval(s) errors too. except ValueError: # This catches utils.safeEval(s) errors too.
self.error() self.error()
_printable = string.printable[:-4]
def _needsQuoting(self, s):
return s.translate(string.ascii, self._printable) and s.strip() != s
def __str__(self):
s = self.value
if self._needsQuoting(s):
s = repr(s)
return s
class OnlySomeStrings(String): class OnlySomeStrings(String):
validStrings = () validStrings = ()
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -390,6 +412,10 @@ class OnlySomeStrings(String):
self.error() self.error()
class NormalizedString(String): class NormalizedString(String):
def __init__(self, default, help):
default = self.normalize(default)
String.__init__(self, default, help)
def normalize(self, s): def normalize(self, s):
return utils.normalizeWhitespace(s.strip()) return utils.normalizeWhitespace(s.strip())
@ -471,9 +497,9 @@ class SeparatedListOf(Value):
Value.setValue(self, self.List(v)) Value.setValue(self, self.List(v))
def __str__(self): def __str__(self):
value = self() values = self()
if value: if values:
return self.joiner(value) return self.joiner(values)
else: else:
# We must return *something* here, otherwise down along the road we # We must return *something* here, otherwise down along the road we
# can run into issues showing users the value if they've disabled # can run into issues showing users the value if they've disabled