mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-02-18 14:40:51 +01:00
Updated some design of the Values.
This commit is contained in:
parent
0027117e8c
commit
d5ee023bfa
15
src/conf.py
15
src/conf.py
@ -79,22 +79,22 @@ def registerGroup(Group, name, group=None):
|
|||||||
Group.register(name, group)
|
Group.register(name, group)
|
||||||
|
|
||||||
class ValidNick(registry.String):
|
class ValidNick(registry.String):
|
||||||
|
"""Value must be a valid IRC nick."""
|
||||||
def setValue(self, v):
|
def setValue(self, v):
|
||||||
if not ircutils.isNick(v):
|
if not ircutils.isNick(v):
|
||||||
raise registry.InvalidRegistryValue, \
|
self.error()
|
||||||
'Value must be a valid IRC nick.'
|
|
||||||
else:
|
else:
|
||||||
registry.String.setValue(self, v)
|
registry.String.setValue(self, v)
|
||||||
|
|
||||||
class ValidChannel(registry.String):
|
class ValidChannel(registry.String):
|
||||||
|
"""Value must be a valid IRC channel name."""
|
||||||
def setValue(self, v):
|
def setValue(self, v):
|
||||||
if ',' in v:
|
if ',' in v:
|
||||||
(channel, _) = v.split(',', 1)
|
(channel, _) = v.split(',', 1)
|
||||||
else:
|
else:
|
||||||
channel = v
|
channel = v
|
||||||
if not ircutils.isChannel(channel):
|
if not ircutils.isChannel(channel):
|
||||||
raise registry.InvalidRegistryValue, \
|
self.error()
|
||||||
'Value must be a valid IRC channel name.'
|
|
||||||
else:
|
else:
|
||||||
registry.String.setValue(self, v)
|
registry.String.setValue(self, v)
|
||||||
|
|
||||||
@ -136,10 +136,10 @@ supybot.register('channels', SpaceSeparatedSetOfChannels(['#supybot'], """
|
|||||||
Determines what channels the bot will join when it connects to the server."""))
|
Determines what channels the bot will join when it connects to the server."""))
|
||||||
|
|
||||||
class ValidPrefixChars(registry.String):
|
class ValidPrefixChars(registry.String):
|
||||||
|
"""Value must contain only ~!@#$%^&*()_-+=[{}]\\|'\";:,<.>/?"""
|
||||||
def setValue(self, v):
|
def setValue(self, v):
|
||||||
if v.translate(string.ascii, '`~!@#$%^&*()_-+=[{}]\\|\'";:,<.>/?'):
|
if v.translate(string.ascii, '`~!@#$%^&*()_-+=[{}]\\|\'";:,<.>/?'):
|
||||||
raise registry.InvalidRegistryValue, \
|
self.error()
|
||||||
'Value must contain only ~!@#$%^&*()_-+=[{}]\\|\'";:,<.>/?'
|
|
||||||
registry.String.setValue(self, v)
|
registry.String.setValue(self, v)
|
||||||
|
|
||||||
supybot.register('prefixChars', ValidPrefixChars('', """Determines what prefix
|
supybot.register('prefixChars', ValidPrefixChars('', """Determines what prefix
|
||||||
@ -176,9 +176,10 @@ Refer to the Python documentation for the time module to see valid formatting
|
|||||||
characteres for time formats."""))
|
characteres for time formats."""))
|
||||||
|
|
||||||
class IP(registry.String):
|
class IP(registry.String):
|
||||||
|
"""Value must be a valid IP."""
|
||||||
def setValue(self, v):
|
def setValue(self, v):
|
||||||
if v and not (utils.isIP(v) or utils.isIPV6(v)):
|
if v and not (utils.isIP(v) or utils.isIPV6(v)):
|
||||||
raise registry.InvalidRegistryValue, 'Value must be a valid IP.'
|
self.error()
|
||||||
else:
|
else:
|
||||||
registry.String.setValue(self, v)
|
registry.String.setValue(self, v)
|
||||||
|
|
||||||
|
@ -128,7 +128,8 @@ class Group(object):
|
|||||||
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
|
# Undo this later unless you understand why you commented it out!
|
||||||
|
#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
|
||||||
@ -171,11 +172,17 @@ class Group(object):
|
|||||||
|
|
||||||
def unregister(self, name):
|
def unregister(self, name):
|
||||||
try:
|
try:
|
||||||
|
node = self.children[name]
|
||||||
del self.children[name]
|
del self.children[name]
|
||||||
self.added.remove(name)
|
self.added.remove(name)
|
||||||
|
return node
|
||||||
except KeyError:
|
except KeyError:
|
||||||
self.__nonExistentEntry(name)
|
self.__nonExistentEntry(name)
|
||||||
|
|
||||||
|
def rename(self, old, new):
|
||||||
|
node = self.unregister(old)
|
||||||
|
self.register(new, node)
|
||||||
|
|
||||||
def getValues(self, getChildren=False, fullNames=True):
|
def getValues(self, getChildren=False, fullNames=True):
|
||||||
L = []
|
L = []
|
||||||
for name in self.added:
|
for name in self.added:
|
||||||
@ -191,6 +198,8 @@ class Group(object):
|
|||||||
|
|
||||||
|
|
||||||
class Value(Group):
|
class Value(Group):
|
||||||
|
"""Invalid registry value. If you're getting this message, report it,
|
||||||
|
because someone forgot to put a proper help string here."""
|
||||||
def __init__(self, default, help,
|
def __init__(self, default, help,
|
||||||
private=False, showDefault=True, **kwargs):
|
private=False, showDefault=True, **kwargs):
|
||||||
Group.__init__(self, **kwargs)
|
Group.__init__(self, **kwargs)
|
||||||
@ -200,6 +209,9 @@ class Value(Group):
|
|||||||
self.help = utils.normalizeWhitespace(help.strip())
|
self.help = utils.normalizeWhitespace(help.strip())
|
||||||
self.setValue(default)
|
self.setValue(default)
|
||||||
|
|
||||||
|
def error(self):
|
||||||
|
raise InvalidRegistryValue, utils.normalizeWhitespace(self.__doc__)
|
||||||
|
|
||||||
def setName(self, *args):
|
def setName(self, *args):
|
||||||
if self.name == 'unset':
|
if self.name == 'unset':
|
||||||
self._lastModified = 0
|
self._lastModified = 0
|
||||||
@ -235,6 +247,7 @@ class Value(Group):
|
|||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
class Boolean(Value):
|
class Boolean(Value):
|
||||||
|
"""Value must be either True or False (or On or Off)."""
|
||||||
def set(self, s):
|
def set(self, s):
|
||||||
s = s.strip().lower()
|
s = s.strip().lower()
|
||||||
if s in ('true', 'on', 'enable', 'enabled'):
|
if s in ('true', 'on', 'enable', 'enabled'):
|
||||||
@ -244,45 +257,49 @@ class Boolean(Value):
|
|||||||
elif s == 'toggle':
|
elif s == 'toggle':
|
||||||
value = not self.value
|
value = not self.value
|
||||||
else:
|
else:
|
||||||
raise InvalidRegistryValue, '%r is not True or False.' % s
|
self.error()
|
||||||
self.setValue(value)
|
self.setValue(value)
|
||||||
|
|
||||||
def setValue(self, v):
|
def setValue(self, v):
|
||||||
Value.setValue(self, bool(v))
|
Value.setValue(self, bool(v))
|
||||||
|
|
||||||
class Integer(Value):
|
class Integer(Value):
|
||||||
|
"""Value must be an integer."""
|
||||||
def set(self, s):
|
def set(self, s):
|
||||||
try:
|
try:
|
||||||
self.setValue(int(s))
|
self.setValue(int(s))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise InvalidRegistryValue, '%r is not an integer.' % s
|
self.error()
|
||||||
|
|
||||||
class PositiveInteger(Value):
|
class PositiveInteger(Value):
|
||||||
|
"""Value must be positive (non-zero) integer."""
|
||||||
def set(self, s):
|
def set(self, s):
|
||||||
try:
|
try:
|
||||||
self.setValue(int(s))
|
self.setValue(int(s))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise InvalidRegistryValue, '%r is not a positive integer.' % s
|
self.error()
|
||||||
|
|
||||||
def setValue(self, v):
|
def setValue(self, v):
|
||||||
if v <= 0:
|
if v <= 0:
|
||||||
raise InvalidRegistryValue, '%r is not a positive integer.' % v
|
self.error()
|
||||||
Value.setValue(self, v)
|
Value.setValue(self, v)
|
||||||
|
|
||||||
class Float(Value):
|
class Float(Value):
|
||||||
|
"""Value must be a floating-point number."""
|
||||||
def set(self, s):
|
def set(self, s):
|
||||||
try:
|
try:
|
||||||
self.setValue(float(s))
|
self.setValue(float(s))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise InvalidRegistryValue, '%r is not a float.' % s
|
self.error()
|
||||||
|
|
||||||
def setValue(self, v):
|
def setValue(self, v):
|
||||||
try:
|
try:
|
||||||
Value.setValue(self, float(v))
|
Value.setValue(self, float(v))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise InvalidRegistryValue, '%r is not a float.' % v
|
self.error()
|
||||||
|
|
||||||
class String(Value):
|
class String(Value):
|
||||||
|
"""Value is not a valid Python string."""
|
||||||
def set(self, s):
|
def set(self, s):
|
||||||
if not s:
|
if not s:
|
||||||
s = '""'
|
s = '""'
|
||||||
@ -294,7 +311,7 @@ class String(Value):
|
|||||||
raise ValueError
|
raise ValueError
|
||||||
self.setValue(v)
|
self.setValue(v)
|
||||||
except ValueError: # This catches utils.safeEval(s) errors too.
|
except ValueError: # This catches utils.safeEval(s) errors too.
|
||||||
raise InvalidRegistryValue, '%r is not a string.' % s
|
self.error()
|
||||||
|
|
||||||
class OnlySomeStrings(String):
|
class OnlySomeStrings(String):
|
||||||
validStrings = ()
|
validStrings = ()
|
||||||
@ -303,6 +320,11 @@ class OnlySomeStrings(String):
|
|||||||
'This is a bug.'
|
'This is a bug.'
|
||||||
String.__init__(self, *args, **kwargs)
|
String.__init__(self, *args, **kwargs)
|
||||||
|
|
||||||
|
def error(self):
|
||||||
|
raise InvalidRegistryValue, \
|
||||||
|
'That is not a valid value. Valid values include %s.' % \
|
||||||
|
utils.commaAndify(map(repr, self.validStrings))
|
||||||
|
|
||||||
def normalize(self, s):
|
def normalize(self, s):
|
||||||
lowered = s.lower()
|
lowered = s.lower()
|
||||||
L = list(map(str.lower, self.validStrings))
|
L = list(map(str.lower, self.validStrings))
|
||||||
@ -317,10 +339,8 @@ class OnlySomeStrings(String):
|
|||||||
if s in self.validStrings:
|
if s in self.validStrings:
|
||||||
String.setValue(self, s)
|
String.setValue(self, s)
|
||||||
else:
|
else:
|
||||||
raise InvalidRegistryValue, \
|
self.error()
|
||||||
'%r is not a valid value. Valid values include %s.' % \
|
|
||||||
(s, utils.commaAndify(map(repr, self.validStrings)))
|
|
||||||
|
|
||||||
class NormalizedString(String):
|
class NormalizedString(String):
|
||||||
def set(self, s):
|
def set(self, s):
|
||||||
s = utils.normalizeWhitespace(s.strip())
|
s = utils.normalizeWhitespace(s.strip())
|
||||||
@ -349,6 +369,9 @@ class StringWithSpaceOnRight(String):
|
|||||||
String.setValue(self, v)
|
String.setValue(self, v)
|
||||||
|
|
||||||
class Regexp(Value):
|
class Regexp(Value):
|
||||||
|
def error(self, e):
|
||||||
|
raise InvalidRegistryValue, 'Invalid regexp: %s' % e
|
||||||
|
|
||||||
def set(self, s):
|
def set(self, s):
|
||||||
try:
|
try:
|
||||||
if s:
|
if s:
|
||||||
@ -358,14 +381,14 @@ class Regexp(Value):
|
|||||||
self.setValue(None)
|
self.setValue(None)
|
||||||
self.sr = s
|
self.sr = s
|
||||||
except ValueError, e:
|
except ValueError, e:
|
||||||
raise InvalidRegistryValue, '%r is not a valid regexp: %s' % (s, e)
|
self.error(e)
|
||||||
|
|
||||||
def setValue(self, v):
|
def setValue(self, v):
|
||||||
if v is None:
|
if v is None:
|
||||||
self.sr = ''
|
self.sr = ''
|
||||||
Value.setValue(self, None)
|
Value.setValue(self, None)
|
||||||
else:
|
else:
|
||||||
raise InvalidRegistryValue, \
|
raise ValueError, \
|
||||||
'Can\'t set to a regexp, there would be an inconsistency ' \
|
'Can\'t set to a regexp, there would be an inconsistency ' \
|
||||||
'between the regexp and the recorded string value.'
|
'between the regexp and the recorded string value.'
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user