From 48b2bfd700428de170553956b1933e6173aedcc5 Mon Sep 17 00:00:00 2001 From: Alexander Cherniuk Date: Thu, 18 Mar 2010 07:37:49 +0200 Subject: [PATCH] Added a comment about command escaping and tiny touchups --- src/command_system/errors.py | 6 ++++ src/command_system/framework.py | 4 +-- .../implementation/middleware.py | 29 ++++++++++++------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/command_system/errors.py b/src/command_system/errors.py index c4e71b616..a81e7a8fb 100644 --- a/src/command_system/errors.py +++ b/src/command_system/errors.py @@ -45,3 +45,9 @@ class CommandError(BaseError): Used to indicate errors occured during command execution. """ pass + +class NoCommandError(BaseError): + """ + Used to indicate an inability to find the specified command. + """ + pass diff --git a/src/command_system/framework.py b/src/command_system/framework.py index f67f68da5..bcfe829c4 100644 --- a/src/command_system/framework.py +++ b/src/command_system/framework.py @@ -25,7 +25,7 @@ from inspect import getargspec from dispatching import Dispatcher, HostDispatcher, ContainerDispatcher from mapping import parse_arguments, adapt_arguments -from errors import DefinitionError, CommandError +from errors import DefinitionError, CommandError, NoCommandError class CommandHost(object): """ @@ -128,7 +128,7 @@ class CommandProcessor(object): def get_command(self, name): command = Dispatcher.get_command(self.COMMAND_HOST, name) if not command: - raise CommandError("Command does not exist", name=name) + raise NoCommandError("Command does not exist", name=name) return command def list_commands(self): diff --git a/src/command_system/implementation/middleware.py b/src/command_system/implementation/middleware.py index d575b7725..b564956ff 100644 --- a/src/command_system/implementation/middleware.py +++ b/src/command_system/implementation/middleware.py @@ -26,7 +26,7 @@ from traceback import print_exc from common import gajim from ..framework import CommandProcessor -from ..errors import CommandError +from ..errors import CommandError, NoCommandError class ChatCommandProcessor(CommandProcessor): """ @@ -44,33 +44,40 @@ class ChatCommandProcessor(CommandProcessor): def execute_command(self, name, arguments): try: super(ChatCommandProcessor, self).execute_command(name, arguments) + except NoCommandError, error: + details = dict(name=error.name, message=error.message) + message = "%(name)s: %(message)s\n" % details + message += "Try using the //%(name)s or /say /%(name)s " % details + message += "construct if you intended to send it as a text." + self.echo(message, 'error') except CommandError, error: - self.echo("%s: %s" %(error.name, error.message), 'error') + self.echo("%s: %s" % (error.name, error.message), 'error') except Exception: self.echo("An error occured while trying to execute the command", 'error') print_exc() def looks_like_command(self, text, body, name, arguments): - # Command escape stuff ggoes here. If text was prepended by the command - # prefix twice, like //not_a_command (if prefix is set to /) then it - # will be escaped, that is sent just as a regular message with one (only - # one) prefix removed, so message will be /not_a_command. + # Command escape stuff ggoes here. If text was prepended by the + # command prefix twice, like //not_a_command (if prefix is set + # to /) then it will be escaped, that is sent just as a regular + # message with one (only one) prefix removed, so message will be + # /not_a_command. if body.startswith(self.COMMAND_PREFIX): self.send(body) return True def command_preprocessor(self, command, name, arguments, args, kwargs): - # If command argument contain h or help option - forward it to the /help - # command. Dont forget to pass self, as all commands are unbound. And - # also don't forget to print output. + # If command argument contain h or help option - forward it to + # the /help command. Dont forget to pass self, as all commands + # are unbound. And also don't forget to print output. if 'h' in kwargs or 'help' in kwargs: help = self.get_command('help') self.echo(help(self, name)) return True def command_postprocessor(self, command, name, arguments, args, kwargs, value): - # If command returns a string - print it to a user. A convenient and - # sufficient in most simple cases shortcut to a using echo. + # If command returns a string - print it to a user. A convenient + # and sufficient in most simple cases shortcut to a using echo. if value and isinstance(value, StringTypes): self.echo(value)