Upgraded middleware inside the command system. Added support for proper output style

This commit is contained in:
Alexander Cherniuk 2010-08-06 06:07:28 +03:00
parent 7c1f4bf23e
commit e78e4318c5
3 changed files with 91 additions and 24 deletions

View File

@ -409,6 +409,10 @@ class ChatControlBase(MessageControl, ChatCommandProcessor, CommandTools):
self.command_hits = []
self.last_key_tabs = False
# This is bascially a very nasty hack to surpass the inability
# to properly use the super, because of the old code.
CommandTools.__init__(self)
def set_speller(self):
# now set the one the user selected
per_type = 'contacts'

View File

@ -1,42 +1,58 @@
# Copyright (C) 2009-2010 Alexander Cherniuk <ts33kr@gmail.com>
# Copyright (c) 2009-2010, Alexander Cherniuk (ts33kr@gmail.com)
# All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
Provides a glue to tie command system framework and the actual code
where it would be dropped in. Defines a little bit of scaffolding to
support interaction between the two and a few utility methods so you
don't need to dig up the code itself code to write basic commands.
don't need to dig up the code itself to write basic commands.
"""
from types import StringTypes
from traceback import print_exc
from pango import FontDescription
from common import gajim
from ..framework import CommandProcessor
from ..errors import CommandError, NoCommandError
from ..tools import gconf
class ChatCommandProcessor(CommandProcessor):
"""
A basic scaffolding to provide convenient interaction between the
command system and chat controls.
command system and chat controls. It will be merged directly into
the controls, by ChatCommandProcessor being among superclasses of
the controls.
"""
def process_as_command(self, text):
self.command_succeeded = False
flag = super(ChatCommandProcessor, self).process_as_command(text)
parents = super(ChatCommandProcessor, self)
flag = parents.process_as_command(text)
if flag and self.command_succeeded:
self.add_history(text)
self.clear_input()
@ -44,17 +60,18 @@ class ChatCommandProcessor(CommandProcessor):
def execute_command(self, name, arguments):
try:
super(ChatCommandProcessor, self).execute_command(name, arguments)
parents = super(ChatCommandProcessor, self)
parents.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')
self.echo_error(message)
except CommandError, error:
self.echo("%s: %s" % (error.name, error.message), 'error')
self.echo_error("%s: %s" % (error.name, error.message))
except Exception:
self.echo("An error occured while trying to execute the command", 'error')
self.echo_error("Error during command execution!")
print_exc()
else:
self.command_succeeded = True
@ -87,14 +104,52 @@ class ChatCommandProcessor(CommandProcessor):
class CommandTools:
"""
Contains a set of basic tools and shortcuts you can use in your
commands to performe some simple operations.
commands to perform some simple operations. These will be merged
directly into the controls, by CommandTools being among superclasses
of the controls.
"""
def echo(self, text, kind='info'):
def __init__(self):
self.install_tags()
def install_tags(self):
buffer = self.conv_textview.tv.get_buffer()
name = gconf("/desktop/gnome/interface/monospace_font_name")
name = name if name else "Monospace"
font = FontDescription(name)
command_ok_tag = buffer.create_tag("command_ok")
command_ok_tag.set_property("font-desc", font)
command_ok_tag.set_property("foreground", "#3465A4")
command_error_tag = buffer.create_tag("command_error")
command_error_tag.set_property("font-desc", font)
command_error_tag.set_property("foreground", "#F57900")
def shift_line(self):
buffer = self.conv_textview.tv.get_buffer()
iter = buffer.get_end_iter()
if iter.ends_line() and not iter.is_start():
buffer.insert_with_tags_by_name(iter, "\n", "eol")
def append_with_tags(self, text, *tags):
buffer = self.conv_textview.tv.get_buffer()
iter = buffer.get_end_iter()
buffer.insert_with_tags_by_name(iter, text, *tags)
def echo(self, text, tag="command_ok"):
"""
Print given text to the user.
Print given text to the user, as a regular command output.
"""
self.print_conversation(str(text), kind)
self.shift_line()
self.append_with_tags(text, tag)
def echo_error(self, text):
"""
Print given text to the user, as an error command output.
"""
self.echo(text, "command_error")
def send(self, text):
"""

View File

@ -33,3 +33,11 @@ def remove(sequence, target):
elif isinstance(sequence, DictType):
if target in sequence:
del sequence[target]
def gconf(path):
try:
from gconf import client_get_default
except ImportError:
return
client = client_get_default()
return client.get_string(path)