fixed line end characters.

This commit is contained in:
Dimitur Kirov 2005-08-01 15:23:45 +00:00
parent 51b4eafa8c
commit fcdbb820d0

View file

@ -1,172 +1,171 @@
#!/usr/bin/env python #!/usr/bin/env python
## scripts/gajim-remote.py ## scripts/gajim-remote.py
## ##
## Gajim Team: ## Gajim Team:
## - Yann Le Boulanger <asterix@lagaule.org> ## - Yann Le Boulanger <asterix@lagaule.org>
## - Vincent Hanquez <tab@snarc.org> ## - Vincent Hanquez <tab@snarc.org>
## - Nikos Kouremenos <kourem@gmail.com> ## - Nikos Kouremenos <kourem@gmail.com>
## - Dimitur Kirov <dkirov@gmail.com> ## - Dimitur Kirov <dkirov@gmail.com>
## ##
## This file was initially written by Dimitur Kirov ## This file was initially written by Dimitur Kirov
## ##
## Copyright (C) 2003-2005 Gajim Team ## Copyright (C) 2003-2005 Gajim Team
## ##
## This program is free software; you can redistribute it and/or modify ## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published ## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 2 only. ## by the Free Software Foundation; version 2 only.
## ##
## This program is distributed in the hope that it will be useful, ## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of ## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details. ## GNU General Public License for more details.
## ##
# gajim-remote help will show you the DBUS API of Gajim # gajim-remote help will show you the DBUS API of Gajim
import sys import sys
import gtk import gtk
import gobject import gobject
import signal import signal
signal.signal(signal.SIGINT, signal.SIG_DFL) # ^C exits the application signal.signal(signal.SIGINT, signal.SIG_DFL) # ^C exits the application
import i18n import i18n
_ = i18n._ _ = i18n._
i18n.init() i18n.init()
try:
try: import dbus
import dbus except:
except: send_error('Dbus is not supported.\n')
send_error('Dbus is not supported.\n')
_version = getattr(dbus, 'version', (0, 20, 0))
_version = getattr(dbus, 'version', (0, 20, 0)) if _version[1] >= 41:
if _version[1] >= 41: import dbus.service
import dbus.service import dbus.glib
import dbus.glib
OBJ_PATH = '/org/gajim/dbus/RemoteObject'
OBJ_PATH = '/org/gajim/dbus/RemoteObject' INTERFACE = 'org.gajim.dbus.RemoteInterface'
INTERFACE = 'org.gajim.dbus.RemoteInterface' SERVICE = 'org.gajim.dbus'
SERVICE = 'org.gajim.dbus' BASENAME = 'gajim-remote'
BASENAME = 'gajim-remote'
class GajimRemote:
class GajimRemote:
def __init__(self):
def __init__(self): self.argv_len = len(sys.argv)
self.argv_len = len(sys.argv) # define commands dict. Prototype :
# define commands dict. Prototype : # {
# { # 'command': [comment, [list of arguments] ]
# 'command': [comment, [list of arguments] ] # }
# } #
# # each argument is defined as a tuple:
# each argument is defined as a tuple: # (argument name, help on argument, is mandatory)
# (argument name, help on argument, is mandatory) #
# self.commands = {
self.commands = { 'help':[
'help':[ _('show a help on specific command'),
_('show a help on specific command'), [
[ (_('on_command'), _('show help on command'), False)
(_('on_command'), _('show help on command'), False) ]
] ],
], 'toggle_roster_appearance' : [
'toggle_roster_appearance' : [ _('Shows or hides the roster window'),
_('Shows or hides the roster window'), []
[] ],
], 'show_next_unread': [
'show_next_unread': [ _('Popup a window with the next unread message'),
_('Popup a window with the next unread message'), []
[] ],
], 'list_contacts': [
'list_contacts': [ _('Print a list of all contacts in the roster. \
_('Print a list of all contacts in the roster. \ Each contact appear on a separate line'),
Each contact appear on a separate line'), [
[
(_('account'), _('show only contacts of the \ (_('account'), _('show only contacts of the \
given account'), False) given account'), False)
] ]
], ],
'list_accounts': [ 'list_accounts': [
_('Print a list of registered accounts'), _('Print a list of registered accounts'),
[] []
], ],
'change_status': [ 'change_status': [
_('Change the status of account or accounts'), _('Change the status of account or accounts'),
[ [
(_('status'), _('one of: offline, online, chat, away, \ (_('status'), _('one of: offline, online, chat, away, \
xa, dnd, invisible '), True), xa, dnd, invisible '), True),
(_('message'), _('status message'), False), (_('message'), _('status message'), False),
(_('account'), _('change status of account "account". \ (_('account'), _('change status of account "account". \
If not specified try to change status of all accounts that \ If not specified try to change status of all accounts that \
have "sync with global status" option set'), False) have "sync with global status" option set'), False)
] ]
], ],
'open_chat': [ 'open_chat': [
_('Show the chat dialog so that you can send message to a \ _('Show the chat dialog so that you can send message to a \
contact'), contact'),
[ [
('jid', _('jid of the contact that you want to chat \ ('jid', _('jid of the contact that you want to chat \
with'), with'),
True), True),
(_('account'), _('if specified contact is taken from \ (_('account'), _('if specified contact is taken from \
the contact list of this account'), False) the contact list of this account'), False)
] ]
], ],
'send_message':[ 'send_message':[
_('Send new message to a contact in the roster. Both pgp \ _('Send new message to a contact in the roster. Both pgp \
key and account are optional. If you want to set only \'account\', whitout \ key and account are optional. If you want to set only \'account\', whitout \
\'pgp key\', just set \'pgp key\' to \'\'.'), \'pgp key\', just set \'pgp key\' to \'\'.'),
[ [
('jid', _('jid of the contact that will receive the \ ('jid', _('jid of the contact that will receive the \
message'), True), message'), True),
(_('message'), _('message contents'), True), (_('message'), _('message contents'), True),
(_('pgp key'), _('if specified the message will be \ (_('pgp key'), _('if specified the message will be \
encrypted using this pulic key'), False), encrypted using this pulic key'), False),
(_('account'), _('if specified the message will be \ (_('account'), _('if specified the message will be \
sent using this account'), False), sent using this account'), False),
] ]
], ],
'contact_info': [ 'contact_info': [
_('Get detailed info on a contact'), _('Get detailed info on a contact'),
[ [
('jid', _('jid of the contact'), True) ('jid', _('jid of the contact'), True)
] ]
] ]
} }
if self.argv_len < 2 or \ if self.argv_len < 2 or \
sys.argv[1] not in self.commands.keys(): # no args or bad args sys.argv[1] not in self.commands.keys(): # no args or bad args
self.send_error(self.compose_help()) self.send_error(self.compose_help())
self.command = sys.argv[1] self.command = sys.argv[1]
if self.command == 'help': if self.command == 'help':
if self.argv_len == 3: if self.argv_len == 3:
print self.help_on_command(sys.argv[2]) print self.help_on_command(sys.argv[2])
else: else:
print self.compose_help() print self.compose_help()
sys.exit() sys.exit()
self.init_connection() self.init_connection()
self.check_arguments() self.check_arguments()
if self.command == 'contact_info': if self.command == 'contact_info':
if self.argv_len < 3: if self.argv_len < 3:
self.send_error(_('Missing argument "contact_jid"')) self.send_error(_('Missing argument "contact_jid"'))
try: try:
id = self.sbus.add_signal_receiver(self.show_vcard_info, id = self.sbus.add_signal_receiver(self.show_vcard_info,
'VcardInfo', INTERFACE, SERVICE, OBJ_PATH) 'VcardInfo', INTERFACE, SERVICE, OBJ_PATH)
except: except:
self.send_error(_('Service not available')) self.send_error(_('Service not available'))
res = self.call_remote_method() res = self.call_remote_method()
self.print_result(res) self.print_result(res)
if self.command == 'contact_info': if self.command == 'contact_info':
gobject.timeout_add(10000, self.gtk_quit) # wait 10 sec for response gobject.timeout_add(10000, self.gtk_quit) # wait 10 sec for response
gtk.main() gtk.main()
def print_result(self, res): def print_result(self, res):
''' Print retrieved result to the output ''' ''' Print retrieved result to the output '''
if res is not None: if res is not None:
@ -210,61 +209,61 @@ Please specify account for sending the message.') % sys.argv[2])
send_error(_('Unknow dbus version: %s') % _version) send_error(_('Unknow dbus version: %s') % _version)
# get the function asked # get the function asked
self.method = self.interface.__getattr__(self.command) self.method = self.interface.__getattr__(self.command)
def make_arguments_row(self, args): def make_arguments_row(self, args):
''' return arguments list. Mandatory arguments are enclosed with: ''' return arguments list. Mandatory arguments are enclosed with:
'<', '>', optional arguments - with '[', ']' ''' '<', '>', optional arguments - with '[', ']' '''
str = '' str = ''
for argument in args: for argument in args:
str += ' ' str += ' '
if argument[2]: if argument[2]:
str += '<' str += '<'
else: else:
str += '[' str += '['
str += argument[0] str += argument[0]
if argument[2]: if argument[2]:
str += '>' str += '>'
else: else:
str += ']' str += ']'
return str return str
def help_on_command(self, command): def help_on_command(self, command):
''' return help message for a given command ''' ''' return help message for a given command '''
if command in self.commands: if command in self.commands:
command_props = self.commands[command] command_props = self.commands[command]
arguments_str = self.make_arguments_row(command_props[1]) arguments_str = self.make_arguments_row(command_props[1])
str = _('Usage: %s %s %s \n\t') % (BASENAME, command, str = _('Usage: %s %s %s \n\t') % (BASENAME, command,
arguments_str) arguments_str)
str += command_props[0] + '\n\nArguments:\n' str += command_props[0] + '\n\nArguments:\n'
for argument in command_props[1]: for argument in command_props[1]:
str += ' ' + argument[0] + ' - ' + argument[1] + '\n' str += ' ' + argument[0] + ' - ' + argument[1] + '\n'
return str return str
self.send_error(_(' %s not found') % command) self.send_error(_(' %s not found') % command)
def compose_help(self): def compose_help(self):
''' print usage, and list available commands ''' ''' print usage, and list available commands '''
str = _('Usage: %s command [arguments]\nCommand is one of:\n' ) % BASENAME str = _('Usage: %s command [arguments]\nCommand is one of:\n' ) % BASENAME
for command in self.commands.keys(): for command in self.commands.keys():
str += ' ' + command str += ' ' + command
for argument in self.commands[command][1]: for argument in self.commands[command][1]:
str += ' ' str += ' '
if argument[2]: if argument[2]:
str += '<' str += '<'
else: else:
str += '[' str += '['
str += argument[0] str += argument[0]
if argument[2]: if argument[2]:
str += '>' str += '>'
else: else:
str += ']' str += ']'
str += '\n' str += '\n'
return str return str
def print_info(self, level, prop_dict): def print_info(self, level, prop_dict):
''' return formated string from serialized vcard data ''' ''' return formated string from serialized vcard data '''
if prop_dict is None or type(prop_dict) \ if prop_dict is None or type(prop_dict) \
not in [dict, list, tuple]: not in [dict, list, tuple]:
return '' return ''
ret_str = '' ret_str = ''
if type(prop_dict) in [list, tuple]: if type(prop_dict) in [list, tuple]:
ret_str = '' ret_str = ''
@ -283,28 +282,28 @@ Please specify account for sending the message.') % sys.argv[2])
ret_str += '\t' + res ret_str += '\t' + res
ret_str = '%s(%s)\n' % (spacing, ret_str[1:]) ret_str = '%s(%s)\n' % (spacing, ret_str[1:])
elif type(prop_dict) is dict: elif type(prop_dict) is dict:
for key in prop_dict.keys(): for key in prop_dict.keys():
val = prop_dict[key] val = prop_dict[key]
spacing = ' ' * level * 4 spacing = ' ' * level * 4
if type(val) == unicode or type(val) == int or \ if type(val) == unicode or type(val) == int or \
type(val) == str: type(val) == str:
if val is not None: if val is not None:
val = val.strip() val = val.strip()
ret_str += '%s%-10s: %s\n' % (spacing, key, val) ret_str += '%s%-10s: %s\n' % (spacing, key, val)
elif type(val) == list or type(val) == tuple: elif type(val) == list or type(val) == tuple:
res = '' res = ''
for items in val: for items in val:
res += self.print_info(level+1, items) res += self.print_info(level+1, items)
if res != '': if res != '':
ret_str += '%s%s: \n%s' % (spacing, key, res) ret_str += '%s%s: \n%s' % (spacing, key, res)
elif type(val) == dict: elif type(val) == dict:
res = self.print_info(level+1, val) res = self.print_info(level+1, val)
if res != '': if res != '':
ret_str += '%s%s: \n%s' % (spacing, key, res) ret_str += '%s%s: \n%s' % (spacing, key, res)
else: else:
self.send_warning(_('Unknown type %s ') % type(val)) self.send_warning(_('Unknown type %s ') % type(val))
return ret_str return ret_str
def unrepr(self, serialized_data): def unrepr(self, serialized_data):
''' works the same as eval, but only for structural values, ''' works the same as eval, but only for structural values,
not functions! e.g. dicts, lists, strings, tuples ''' not functions! e.g. dicts, lists, strings, tuples '''
@ -418,66 +417,66 @@ Please specify account for sending the message.') % sys.argv[2])
elif next[0] in [']', ')']: elif next[0] in [']', ')']:
break break
return (_tuple, next[1:]) return (_tuple, next[1:])
def show_vcard_info(self, *args, **keyword): def show_vcard_info(self, *args, **keyword):
''' write user vcart in a formated output ''' ''' write user vcart in a formated output '''
props_dict = None props_dict = None
if _version[1] >= 30: if _version[1] >= 30:
props_dict = self.unrepr(args[0]) props_dict = self.unrepr(args[0])
else: else:
if args and len(args) >= 5: if args and len(args) >= 5:
props_dict = self.unrepr(args[4].get_args_list()[0]) props_dict = self.unrepr(args[4].get_args_list()[0])
if props_dict: if props_dict:
print self.print_info(0,props_dict[0]) print self.print_info(0,props_dict[0])
# remove_signal_receiver is broken in lower versions (< 0.35), # remove_signal_receiver is broken in lower versions (< 0.35),
# so we leave the leak - nothing can be done # so we leave the leak - nothing can be done
if _version[1] >= 41: if _version[1] >= 41:
self.sbus.remove_signal_receiver(show_vcard_info, 'VcardInfo', self.sbus.remove_signal_receiver(show_vcard_info, 'VcardInfo',
INTERFACE, SERVICE, OBJ_PATH) INTERFACE, SERVICE, OBJ_PATH)
gtk.main_quit() gtk.main_quit()
def check_arguments(self): def check_arguments(self):
''' Make check if all necessary arguments are given ''' ''' Make check if all necessary arguments are given '''
argv_len = self.argv_len - 2 argv_len = self.argv_len - 2
args = self.commands[self.command][1] args = self.commands[self.command][1]
if len(args) > argv_len: if len(args) > argv_len:
if args[argv_len][2]: if args[argv_len][2]:
self.send_error(_('Argument "%s" is not specified. \n\ self.send_error(_('Argument "%s" is not specified. \n\
Type "%s help %s" for more info') % (args[argv_len][0], BASENAME, self.command)) Type "%s help %s" for more info') % (args[argv_len][0], BASENAME, self.command))
def gtk_quit(self): def gtk_quit(self):
if _version[1] >= 41: if _version[1] >= 41:
self.sbus.remove_signal_receiver(show_vcard_info, 'VcardInfo', self.sbus.remove_signal_receiver(show_vcard_info, 'VcardInfo',
INTERFACE, SERVICE, OBJ_PATH) INTERFACE, SERVICE, OBJ_PATH)
gtk.main_quit() gtk.main_quit()
# FIXME - didn't find more clever way for the below method. # FIXME - didn't find more clever way for the below method.
# method(sys.argv[2:]) doesn't work, cos sys.argv[2:] is a tuple # method(sys.argv[2:]) doesn't work, cos sys.argv[2:] is a tuple
def call_remote_method(self): def call_remote_method(self):
''' calls self.method with arguments from sys.argv[2:] ''' ''' calls self.method with arguments from sys.argv[2:] '''
try: try:
if self.argv_len == 2: if self.argv_len == 2:
res = self.method() res = self.method()
elif self.argv_len == 3: elif self.argv_len == 3:
res = self.method(sys.argv[2]) res = self.method(sys.argv[2])
elif self.argv_len == 4: elif self.argv_len == 4:
res = self.method(sys.argv[2], sys.argv[3]) res = self.method(sys.argv[2], sys.argv[3])
elif self.argv_len == 5: elif self.argv_len == 5:
res = self.method(sys.argv[2], sys.argv[3], sys.argv[4]) res = self.method(sys.argv[2], sys.argv[3], sys.argv[4])
elif argv_len == 6: elif argv_len == 6:
res = self.method(sys.argv[2], sys.argv[3], sys.argv[4], res = self.method(sys.argv[2], sys.argv[3], sys.argv[4],
sys.argv[5]) sys.argv[5])
return res return res
except: except:
self.send_error(_('Service not available')) self.send_error(_('Service not available'))
return None return None
def send_error(self, error_message): def send_error(self, error_message):
''' Writes error message to stderr and exits ''' ''' Writes error message to stderr and exits '''
sys.stderr.write(error_message + '\n') sys.stderr.write(error_message + '\n')
sys.stderr.flush() sys.stderr.flush()
sys.exit(1) sys.exit(1)
if __name__ == '__main__': if __name__ == '__main__':