Changed supplyDefault to _supplyDefault and added %s handling for supybot.nick.alternates.

This commit is contained in:
Jeremy Fincher 2004-08-17 20:43:25 +00:00
parent 1290d2e3eb
commit b22e3c44e2
3 changed files with 35 additions and 14 deletions

View File

@ -93,7 +93,7 @@ def registerGlobalValue(group, name, value):
return group.register(name, value) return group.register(name, value)
def registerChannelValue(group, name, value): def registerChannelValue(group, name, value):
value.supplyDefault = True value._supplyDefault = True
value.channelValue = True value.channelValue = True
return group.register(name, value) return group.register(name, value)
@ -123,7 +123,7 @@ registerGroup(users, 'plugins')
def registerUserValue(group, name, value): def registerUserValue(group, name, value):
assert group._name.startswith('users') assert group._name.startswith('users')
value.supplyDefault = True value._supplyDefault = True
group.register(name, value) group.register(name, value)
class ValidNick(registry.String): class ValidNick(registry.String):
@ -137,6 +137,21 @@ class ValidNick(registry.String):
class ValidNicks(registry.SpaceSeparatedListOf): class ValidNicks(registry.SpaceSeparatedListOf):
Value = ValidNick Value = ValidNick
class ValidNickAllowingPercentS(ValidNick):
"""Value must be a valid IRC nick, with the possible exception of a %s
in it."""
def setValue(self, v):
# If this works, it's a valid nick, aside from the %s.
try:
ValidNick.setValue(self, v.replace('%s', ''))
# It's valid aside from the %s, we'll let it through.
registry.String.setValue(self, v)
except registry.InvalidRegistryValue:
self.error()
class ValidNicksAllowingPercentS(ValidNicks):
Value = ValidNickAllowingPercentS
class ValidChannel(registry.String): class ValidChannel(registry.String):
"""Value must be a valid IRC channel name.""" """Value must be a valid IRC channel name."""
def setValue(self, v): def setValue(self, v):
@ -152,10 +167,12 @@ class ValidChannel(registry.String):
registerGlobalValue(supybot, 'nick', registerGlobalValue(supybot, 'nick',
ValidNick('supybot', """Determines the bot's default nick.""")) ValidNick('supybot', """Determines the bot's default nick."""))
registerGlobalValue(supybot.nick, 'alternates', ValidNicks([], """Determines registerGlobalValue(supybot.nick, 'alternates',
what alternative nicks will be used if the primary nick (supybot.nick) ValidNicksAllowingPercentS(['%s`', '%s_'], """Determines what alternative
isn't available. If none are given, or if all are taken, the primary nick nicks will be used if the primary nick (supybot.nick) isn't available. A
will be perturbed appropriately until an unused nick is found.""")) %s in this nick is replaced by the value of supybot.nick when used. If no
alternates are given, or if all are used, the supybot.nick will be perturbed
appropriately until an unused nick is found."""))
registerGlobalValue(supybot, 'ident', registerGlobalValue(supybot, 'ident',
ValidNick('supybot', """Determines the bot's ident string, if the server ValidNick('supybot', """Determines the bot's ident string, if the server

View File

@ -499,7 +499,10 @@ class Irc(IrcCommandDispatcher):
def _getNextNick(self): def _getNextNick(self):
if self.alternateNicks: if self.alternateNicks:
return self.alternateNicks.pop(0) nick = self.alternateNicks.pop(0)
if '%s' in nick:
nick %= conf.supybot.nick()
return nick
else: else:
nick = conf.supybot.nick() nick = conf.supybot.nick()
ret = nick ret = nick

View File

@ -128,19 +128,19 @@ def unescape(name):
_splitRe = re.compile(r'(?<!\\)\.') _splitRe = re.compile(r'(?<!\\)\.')
def split(name): def split(name):
# XXX: This should eventually handle escapes.
return map(unescape, _splitRe.split(name)) return map(unescape, _splitRe.split(name))
def join(names): def join(names):
return '.'.join(map(escape, names)) return '.'.join(map(escape, names))
class Group(object): class Group(object):
"""A group; it doesn't hold a value unless handled by a subclass."""
def __init__(self, supplyDefault=False): def __init__(self, supplyDefault=False):
self._name = 'unset' self._name = 'unset'
self.added = [] self.added = []
self._children = utils.InsensitivePreservingDict() self._children = utils.InsensitivePreservingDict()
self._lastModified = 0 self._lastModified = 0
self.supplyDefault = supplyDefault self._supplyDefault = supplyDefault
OriginalClass = self.__class__ OriginalClass = self.__class__
class X(OriginalClass): class X(OriginalClass):
"""This class exists to differentiate those values that have """This class exists to differentiate those values that have
@ -164,7 +164,7 @@ 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 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
@ -172,7 +172,7 @@ class Group(object):
def __getattr__(self, attr): def __getattr__(self, attr):
if attr in self._children: if attr in self._children:
return self._children[attr] return self._children[attr]
elif self.supplyDefault: elif self._supplyDefault:
return self.__makeChild(attr, str(self)) return self.__makeChild(attr, str(self))
else: else:
self.__nonExistentEntry(attr) self.__nonExistentEntry(attr)
@ -188,7 +188,7 @@ class Group(object):
if name in _cache and self._lastModified < _lastModified: if name in _cache and self._lastModified < _lastModified:
#print '***>', _cache[name] #print '***>', _cache[name]
self.set(_cache[name]) self.set(_cache[name])
if self.supplyDefault: if self._supplyDefault:
for (k, v) in _cache.iteritems(): for (k, v) in _cache.iteritems():
if k.startswith(self._name): if k.startswith(self._name):
group = split(k)[-1] group = split(k)[-1]
@ -280,7 +280,7 @@ class Value(Group):
own setValue.""" own setValue."""
self._lastModified = time.time() self._lastModified = time.time()
self.value = v self.value = v
if self.supplyDefault: if self._supplyDefault:
for (name, v) in self._children.items(): for (name, v) in self._children.items():
if v.__class__ is self.X: if v.__class__ is self.X:
self.unregister(name) self.unregister(name)
@ -446,8 +446,9 @@ class StringWithSpaceOnRight(String):
String.setValue(self, v) String.setValue(self, v)
class Regexp(Value): class Regexp(Value):
"""Value must be a valid regular expression."""
def error(self, e): def error(self, e):
raise InvalidRegistryValue, 'Invalid regexp: %s' % e raise InvalidRegistryValue, 'Value must be a regexp of the form %s' % e
def set(self, s): def set(self, s):
try: try: