From 71ea32c811064d1e3061a4899bbcf8b6ee38777a Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Tue, 30 Mar 2004 08:27:05 +0000 Subject: [PATCH] Added supybot.brackets for specifying what valid bracket characters are. --- ChangeLog | 3 +++ src/callbacks.py | 17 ++++++++++------- src/conf.py | 9 +++++++++ src/registry.py | 10 ++++++++++ 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index e32ecf61b..43d3432f9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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. diff --git a/src/callbacks.py b/src/callbacks.py index 48b742a0a..9887f1fcd 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -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) diff --git a/src/conf.py b/src/conf.py index e7cb534f2..f32cf478d 100644 --- a/src/conf.py +++ b/src/conf.py @@ -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'.""")) diff --git a/src/registry.py b/src/registry.py index 25cf73b7f..80315376a 100644 --- a/src/registry.py +++ b/src/registry.py @@ -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())