Added a comment about command escaping and tiny touchups
This commit is contained in:
parent
faca8c5d12
commit
48b2bfd700
|
@ -45,3 +45,9 @@ class CommandError(BaseError):
|
||||||
Used to indicate errors occured during command execution.
|
Used to indicate errors occured during command execution.
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class NoCommandError(BaseError):
|
||||||
|
"""
|
||||||
|
Used to indicate an inability to find the specified command.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
|
@ -25,7 +25,7 @@ from inspect import getargspec
|
||||||
|
|
||||||
from dispatching import Dispatcher, HostDispatcher, ContainerDispatcher
|
from dispatching import Dispatcher, HostDispatcher, ContainerDispatcher
|
||||||
from mapping import parse_arguments, adapt_arguments
|
from mapping import parse_arguments, adapt_arguments
|
||||||
from errors import DefinitionError, CommandError
|
from errors import DefinitionError, CommandError, NoCommandError
|
||||||
|
|
||||||
class CommandHost(object):
|
class CommandHost(object):
|
||||||
"""
|
"""
|
||||||
|
@ -128,7 +128,7 @@ class CommandProcessor(object):
|
||||||
def get_command(self, name):
|
def get_command(self, name):
|
||||||
command = Dispatcher.get_command(self.COMMAND_HOST, name)
|
command = Dispatcher.get_command(self.COMMAND_HOST, name)
|
||||||
if not command:
|
if not command:
|
||||||
raise CommandError("Command does not exist", name=name)
|
raise NoCommandError("Command does not exist", name=name)
|
||||||
return command
|
return command
|
||||||
|
|
||||||
def list_commands(self):
|
def list_commands(self):
|
||||||
|
|
|
@ -26,7 +26,7 @@ from traceback import print_exc
|
||||||
from common import gajim
|
from common import gajim
|
||||||
|
|
||||||
from ..framework import CommandProcessor
|
from ..framework import CommandProcessor
|
||||||
from ..errors import CommandError
|
from ..errors import CommandError, NoCommandError
|
||||||
|
|
||||||
class ChatCommandProcessor(CommandProcessor):
|
class ChatCommandProcessor(CommandProcessor):
|
||||||
"""
|
"""
|
||||||
|
@ -44,33 +44,40 @@ class ChatCommandProcessor(CommandProcessor):
|
||||||
def execute_command(self, name, arguments):
|
def execute_command(self, name, arguments):
|
||||||
try:
|
try:
|
||||||
super(ChatCommandProcessor, self).execute_command(name, arguments)
|
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:
|
except CommandError, error:
|
||||||
self.echo("%s: %s" %(error.name, error.message), 'error')
|
self.echo("%s: %s" % (error.name, error.message), 'error')
|
||||||
except Exception:
|
except Exception:
|
||||||
self.echo("An error occured while trying to execute the command", 'error')
|
self.echo("An error occured while trying to execute the command", 'error')
|
||||||
print_exc()
|
print_exc()
|
||||||
|
|
||||||
def looks_like_command(self, text, body, name, arguments):
|
def looks_like_command(self, text, body, name, arguments):
|
||||||
# Command escape stuff ggoes here. If text was prepended by the command
|
# Command escape stuff ggoes here. If text was prepended by the
|
||||||
# prefix twice, like //not_a_command (if prefix is set to /) then it
|
# command prefix twice, like //not_a_command (if prefix is set
|
||||||
# will be escaped, that is sent just as a regular message with one (only
|
# to /) then it will be escaped, that is sent just as a regular
|
||||||
# one) prefix removed, so message will be /not_a_command.
|
# message with one (only one) prefix removed, so message will be
|
||||||
|
# /not_a_command.
|
||||||
if body.startswith(self.COMMAND_PREFIX):
|
if body.startswith(self.COMMAND_PREFIX):
|
||||||
self.send(body)
|
self.send(body)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def command_preprocessor(self, command, name, arguments, args, kwargs):
|
def command_preprocessor(self, command, name, arguments, args, kwargs):
|
||||||
# If command argument contain h or help option - forward it to the /help
|
# If command argument contain h or help option - forward it to
|
||||||
# command. Dont forget to pass self, as all commands are unbound. And
|
# the /help command. Dont forget to pass self, as all commands
|
||||||
# also don't forget to print output.
|
# are unbound. And also don't forget to print output.
|
||||||
if 'h' in kwargs or 'help' in kwargs:
|
if 'h' in kwargs or 'help' in kwargs:
|
||||||
help = self.get_command('help')
|
help = self.get_command('help')
|
||||||
self.echo(help(self, name))
|
self.echo(help(self, name))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def command_postprocessor(self, command, name, arguments, args, kwargs, value):
|
def command_postprocessor(self, command, name, arguments, args, kwargs, value):
|
||||||
# If command returns a string - print it to a user. A convenient and
|
# If command returns a string - print it to a user. A convenient
|
||||||
# sufficient in most simple cases shortcut to a using echo.
|
# and sufficient in most simple cases shortcut to a using echo.
|
||||||
if value and isinstance(value, StringTypes):
|
if value and isinstance(value, StringTypes):
|
||||||
self.echo(value)
|
self.echo(value)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue