Added supybot.brackets for specifying what valid bracket characters are.

This commit is contained in:
Jeremy Fincher 2004-03-30 08:27:05 +00:00
parent d9c535f2bc
commit 71ea32c811
4 changed files with 32 additions and 7 deletions

View File

@ -1,3 +1,6 @@
* Added supybot.brackets, a configuration variable for specifying
which set of matching brackets to use for nested commands. Valid
values are [] (the default), {}, <>, and ().
* Added a configuration variable to ChannelLogger, timestamp,
which determines whether the bot will timestamp its logfiles.
This is a channel-specific variable, of course.

View File

@ -175,6 +175,9 @@ class Tokenizer:
def __init__(self, tokens=''):
# Add a '|' to tokens to have the pipe syntax.
self.validChars = self.validChars.translate(string.ascii, tokens)
if len(tokens) >= 2:
self.left = tokens[0]
self.right = tokens[1]
def _handleToken(self, token):
if token[0] == token[-1] and token[0] in self.quotes:
@ -187,10 +190,10 @@ class Tokenizer:
while True:
token = lexer.get_token()
if not token:
raise SyntaxError, 'Missing "]".'
elif token == ']':
raise SyntaxError, 'Missing "%s".' % self.right
elif token == self.right:
return ret
elif token == '[':
elif token == self.left:
ret.append(self._insideBrackets(lexer))
else:
ret.append(self._handleToken(token))
@ -214,10 +217,10 @@ class Tokenizer:
ends.append(args)
args = []
elif conf.supybot.bracketSyntax():
if token == '[':
if token == self.left:
args.append(self._insideBrackets(lexer))
elif token == ']':
raise SyntaxError, 'Spurious "]".'
elif token == self.right:
raise SyntaxError, 'Spurious "%s".' % self.right
else:
args.append(self._handleToken(token))
else:
@ -241,7 +244,7 @@ def tokenize(s):
_lastTokenized = s
tokens = ''
if conf.supybot.bracketSyntax():
tokens = '[]'
tokens = conf.supybot.brackets()
if conf.supybot.pipeSyntax():
tokens = '%s|' % tokens
_lastTokenizeResult = Tokenizer(tokens).tokenize(s)

View File

@ -199,6 +199,15 @@ supybot.register('bracketSyntax', registry.Boolean(True, """Supybot allows
nested commands. If this option is enabled users can nest commands using a
bracket syntax, for example: 'bot: bar [foo]'."""))
class ValidBrackets(registry.OnlySomeStrings):
validStrings = ('[]', '<>', '{}', '()')
supybot.register('brackets', ValidBrackets('[]', """Supybot allows you to
specify what brackets are used for your nested commands. Valid sets of
brackets include [], <>, and {} (). [] has strong historical motivation, as
well as being the brackets that don't require shift. <> or () might be
slightly superior because they cannot occur in a nick."""))
supybot.register('pipeSyntax', registry.Boolean(False, """Supybot allows
nested commands. Enabling this option will allow nested commands with a syntax
similar to UNIX pipes, for example: 'bot: foo | bar'."""))

View File

@ -296,6 +296,16 @@ class String(Value):
except ValueError: # This catches utils.safeEval(s) errors too.
raise InvalidRegistryValue, '%r is not a string.' % s
class OnlySomeStrings(String):
validStrings = ()
def setValue(self, s):
if s in self.validStrings:
String.setValue(self, s)
else:
raise InvalidRegistryValue, \
'%r is not a valid value. Valid values include %s.' % \
(utils.commaAndify(map(repr, self.validStrings)))
class NormalizedString(String):
def set(self, s):
s = utils.normalizeWhitespace(s.strip())