Package command_system :: Package implementation :: Module middleware
[hide private]
[frames] | no frames]

Source Code for Module command_system.implementation.middleware

  1  # Copyright (c) 2009-2010, Alexander Cherniuk (ts33kr@gmail.com) 
  2  # All rights reserved. 
  3  # 
  4  # Redistribution and use in source and binary forms, with or without 
  5  # modification, are permitted provided that the following conditions 
  6  # are met: 
  7  # 
  8  # * Redistributions of source code must retain the above copyright 
  9  #   notice, this list of conditions and the following disclaimer. 
 10  # 
 11  # * Redistributions in binary form must reproduce the above copyright 
 12  #   notice, this list of conditions and the following disclaimer in the 
 13  #   documentation and/or other materials provided with the distribution. 
 14  # 
 15  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 16  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 17  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 18  # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 19  # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 20  # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 21  # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 22  # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 23  # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 24  # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 25  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 26   
 27  """ 
 28  Provides a glue to tie command system framework and the actual code 
 29  where it would be dropped in. Defines a little bit of scaffolding to 
 30  support interaction between the two and a few utility methods so you 
 31  don't need to dig up the code itself to write basic commands. 
 32  """ 
 33   
 34  from types import StringTypes 
 35  from traceback import print_exc 
 36   
 37  from pango import FontDescription 
 38  from common import gajim 
 39   
 40  from ..framework import CommandProcessor 
 41  from ..errors import CommandError, NoCommandError 
 42  from ..tools import gconf 
43 44 -class ChatCommandProcessor(CommandProcessor):
45 """ 46 A basic scaffolding to provide convenient interaction between the 47 command system and chat controls. It will be merged directly into 48 the controls, by ChatCommandProcessor being among superclasses of 49 the controls. 50 """ 51
52 - def process_as_command(self, text):
53 self.command_succeeded = False 54 parents = super(ChatCommandProcessor, self) 55 flag = parents.process_as_command(text) 56 if flag and self.command_succeeded: 57 self.add_history(text) 58 self.clear_input() 59 return flag
60
61 - def execute_command(self, name, arguments):
62 try: 63 parents = super(ChatCommandProcessor, self) 64 parents.execute_command(name, arguments) 65 except NoCommandError, error: 66 details = dict(name=error.name, message=error.message) 67 message = "%(name)s: %(message)s\n" % details 68 message += "Try using the //%(name)s or /say /%(name)s " % details 69 message += "construct if you intended to send it as a text." 70 self.echo_error(message) 71 except CommandError, error: 72 self.echo_error("%s: %s" % (error.name, error.message)) 73 except Exception: 74 self.echo_error("Error during command execution!") 75 print_exc() 76 else: 77 self.command_succeeded = True
78
79 - def looks_like_command(self, text, body, name, arguments):
80 # Command escape stuff ggoes here. If text was prepended by the 81 # command prefix twice, like //not_a_command (if prefix is set 82 # to /) then it will be escaped, that is sent just as a regular 83 # message with one (only one) prefix removed, so message will be 84 # /not_a_command. 85 if body.startswith(self.COMMAND_PREFIX): 86 self.send(body) 87 return True
88
89 - def command_preprocessor(self, command, name, arguments, args, kwargs):
90 # If command argument contain h or help option - forward it to 91 # the /help command. Dont forget to pass self, as all commands 92 # are unbound. And also don't forget to print output. 93 if 'h' in kwargs or 'help' in kwargs: 94 help = self.get_command('help') 95 self.echo(help(self, name)) 96 return True
97
98 - def command_postprocessor(self, command, name, arguments, args, kwargs, value):
99 # If command returns a string - print it to a user. A convenient 100 # and sufficient in most simple cases shortcut to a using echo. 101 if value and isinstance(value, StringTypes): 102 self.echo(value)
103
104 -class CommandTools:
105 """ 106 Contains a set of basic tools and shortcuts you can use in your 107 commands to perform some simple operations. These will be merged 108 directly into the controls, by CommandTools being among superclasses 109 of the controls. 110 """ 111
112 - def __init__(self):
113 self.install_tags()
114
115 - def install_tags(self):
116 buffer = self.conv_textview.tv.get_buffer() 117 118 name = gconf("/desktop/gnome/interface/monospace_font_name") 119 name = name if name else "Monospace" 120 font = FontDescription(name) 121 122 command_ok_tag = buffer.create_tag("command_ok") 123 command_ok_tag.set_property("font-desc", font) 124 command_ok_tag.set_property("foreground", "#3465A4") 125 126 command_error_tag = buffer.create_tag("command_error") 127 command_error_tag.set_property("font-desc", font) 128 command_error_tag.set_property("foreground", "#F57900")
129
130 - def shift_line(self):
131 buffer = self.conv_textview.tv.get_buffer() 132 iter = buffer.get_end_iter() 133 if iter.ends_line() and not iter.is_start(): 134 buffer.insert_with_tags_by_name(iter, "\n", "eol")
135
136 - def append_with_tags(self, text, *tags):
137 buffer = self.conv_textview.tv.get_buffer() 138 iter = buffer.get_end_iter() 139 buffer.insert_with_tags_by_name(iter, text, *tags)
140
141 - def echo(self, text, tag="command_ok"):
142 """ 143 Print given text to the user, as a regular command output. 144 """ 145 self.shift_line() 146 self.append_with_tags(text, tag)
147
148 - def echo_error(self, text):
149 """ 150 Print given text to the user, as an error command output. 151 """ 152 self.echo(text, "command_error")
153
154 - def send(self, text):
155 """ 156 Send a message to the contact. 157 """ 158 self.send_message(text, process_commands=False)
159
160 - def set_input(self, text):
161 """ 162 Set given text into the input. 163 """ 164 buffer = self.msg_textview.get_buffer() 165 buffer.set_text(text)
166
167 - def clear_input(self):
168 """ 169 Clear input. 170 """ 171 self.set_input(str())
172
173 - def add_history(self, text):
174 """ 175 Add given text to the input history, so user can scroll through 176 it using ctrl + up/down arrow keys. 177 """ 178 self.save_sent_message(text)
179 180 @property
181 - def connection(self):
182 """ 183 Get the current connection object. 184 """ 185 return gajim.connections[self.account]
186 187 @property
188 - def full_jid(self):
189 """ 190 Get a full JID of the contact. 191 """ 192 return self.contact.get_full_jid()
193