2017-06-13 23:58:06 +02:00
|
|
|
#!/usr/bin/env python3
|
2008-08-15 19:31:51 +02:00
|
|
|
# -*- coding:utf-8 -*-
|
2008-08-15 05:20:23 +02:00
|
|
|
## src/gajim-remote.py
|
2005-08-01 17:23:45 +02:00
|
|
|
##
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2005-2006 Dimitur Kirov <dkirov AT gmail.com>
|
|
|
|
## Nikos Kouremenos <kourem AT gmail.com>
|
2014-01-02 09:33:54 +01:00
|
|
|
## Copyright (C) 2005-2014 Yann Leboulanger <asterix AT lagaule.org>
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2006 Junglecow <junglecow AT gmail.com>
|
|
|
|
## Travis Shirk <travis AT pobox.com>
|
|
|
|
## Copyright (C) 2006-2008 Jean-Marie Traissard <jim AT lapin.org>
|
|
|
|
## Copyright (C) 2007 Julien Pivotto <roidelapluie AT gmail.com>
|
2005-08-01 17:23:45 +02:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
2005-08-01 17:23:45 +02:00
|
|
|
## it under the terms of the GNU General Public License as published
|
2007-10-22 13:13:13 +02:00
|
|
|
## by the Free Software Foundation; version 3 only.
|
2005-08-01 17:23:45 +02:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
2005-08-01 17:23:45 +02:00
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-08-15 05:20:23 +02:00
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2005-08-01 17:23:45 +02:00
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## You should have received a copy of the GNU General Public License
|
2008-08-15 05:20:23 +02:00
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2007-10-22 13:13:13 +02:00
|
|
|
##
|
2005-08-01 17:23:45 +02:00
|
|
|
|
2006-09-28 13:29:05 +02:00
|
|
|
# gajim-remote help will show you the D-BUS API of Gajim
|
2005-08-01 17:23:45 +02:00
|
|
|
|
|
|
|
import sys
|
2006-02-09 00:51:46 +01:00
|
|
|
import locale
|
2009-01-25 22:38:35 +01:00
|
|
|
import urllib
|
2005-08-01 17:23:45 +02:00
|
|
|
import signal
|
|
|
|
signal.signal(signal.SIGINT, signal.SIG_DFL) # ^C exits the application
|
|
|
|
|
2016-01-24 20:12:30 +01:00
|
|
|
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common import exceptions
|
|
|
|
from gajim.common import i18n # This installs _() function
|
|
|
|
from gajim.common.i18n import Q_
|
2005-08-01 17:23:45 +02:00
|
|
|
|
2006-03-13 19:20:08 +01:00
|
|
|
try:
|
2010-02-08 15:08:40 +01:00
|
|
|
PREFERRED_ENCODING = locale.getpreferredencoding()
|
2008-10-11 11:37:13 +02:00
|
|
|
except Exception:
|
2010-02-08 15:08:40 +01:00
|
|
|
PREFERRED_ENCODING = 'UTF-8'
|
2006-02-14 19:44:51 +01:00
|
|
|
|
2005-09-04 12:03:29 +02:00
|
|
|
def send_error(error_message):
|
2010-02-08 15:08:40 +01:00
|
|
|
'''Writes error message to stderr and exits'''
|
2013-01-02 17:53:42 +01:00
|
|
|
print(error_message, file=sys.stderr)
|
2010-02-08 15:08:40 +01:00
|
|
|
sys.exit(1)
|
2005-09-06 15:17:10 +02:00
|
|
|
|
2005-08-01 17:23:45 +02:00
|
|
|
try:
|
2010-02-08 15:08:40 +01:00
|
|
|
import dbus
|
|
|
|
import dbus.service
|
2016-07-07 22:41:35 +02:00
|
|
|
# import dbus.glib
|
2010-02-08 15:08:40 +01:00
|
|
|
# test if dbus-x11 is installed
|
|
|
|
bus = dbus.SessionBus()
|
2008-10-11 11:37:13 +02:00
|
|
|
except Exception:
|
2013-01-01 19:36:56 +01:00
|
|
|
print(_('D-Bus is not present on this machine or python module is missing'))
|
2010-02-08 15:08:40 +01:00
|
|
|
sys.exit(1)
|
2005-08-01 17:23:45 +02:00
|
|
|
|
|
|
|
OBJ_PATH = '/org/gajim/dbus/RemoteObject'
|
|
|
|
INTERFACE = 'org.gajim.dbus.RemoteInterface'
|
|
|
|
SERVICE = 'org.gajim.dbus'
|
2010-12-20 13:29:03 +01:00
|
|
|
BASENAME = 'gajim-remote'
|
2005-08-01 17:23:45 +02:00
|
|
|
|
|
|
|
class GajimRemote:
|
2006-08-03 12:47:14 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self):
|
|
|
|
self.argv_len = len(sys.argv)
|
|
|
|
# define commands dict. Prototype :
|
|
|
|
# {
|
|
|
|
# 'command': [comment, [list of arguments] ]
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# each argument is defined as a tuple:
|
|
|
|
# (argument name, help on argument, is mandatory)
|
|
|
|
#
|
|
|
|
self.commands = {
|
2010-02-10 19:24:11 +01:00
|
|
|
'help': [
|
2010-02-08 15:08:40 +01:00
|
|
|
_('Shows a help on specific command'),
|
|
|
|
[
|
|
|
|
#User gets help for the command, specified by this parameter
|
|
|
|
(_('command'),
|
|
|
|
_('show help on command'), False)
|
|
|
|
]
|
|
|
|
],
|
2010-02-10 19:24:11 +01:00
|
|
|
'toggle_roster_appearance': [
|
2010-02-08 15:08:40 +01:00
|
|
|
_('Shows or hides the roster window'),
|
|
|
|
[]
|
|
|
|
],
|
|
|
|
'show_next_pending_event': [
|
|
|
|
_('Pops up a window with the next pending event'),
|
|
|
|
[]
|
|
|
|
],
|
|
|
|
'list_contacts': [
|
2017-02-04 23:29:45 +01:00
|
|
|
_('Lists all contacts in roster, one for each line'),
|
2010-02-08 15:08:40 +01:00
|
|
|
[
|
2013-07-25 19:03:43 +02:00
|
|
|
(Q_('?CLI:account'), _('show only contacts of the given account'),
|
2010-02-08 15:08:40 +01:00
|
|
|
False)
|
|
|
|
]
|
|
|
|
|
|
|
|
],
|
|
|
|
'list_accounts': [
|
|
|
|
_('Prints a list of registered accounts'),
|
|
|
|
[]
|
|
|
|
],
|
|
|
|
'change_status': [
|
2017-02-04 23:29:45 +01:00
|
|
|
_('Changes the status of account(s)'),
|
2010-02-08 15:08:40 +01:00
|
|
|
[
|
2006-06-09 15:42:33 +02:00
|
|
|
#offline, online, chat, away, xa, dnd, invisible should not be translated
|
2013-07-25 19:03:43 +02:00
|
|
|
(Q_('?CLI:status'), _('one of: offline, online, chat, away, xa, dnd, invisible. If not set, use account\'s previous status'), False),
|
|
|
|
(Q_('?CLI:message'), _('status message'), False),
|
|
|
|
(Q_('?CLI:account'), _('change status of account "account". '
|
2010-02-08 15:08:40 +01:00
|
|
|
'If not specified, try to change status of all accounts that have '
|
|
|
|
'"sync with global status" option set'), False)
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'set_priority': [
|
2017-02-04 23:29:45 +01:00
|
|
|
_('Changes the priority of account(s)'),
|
2010-02-08 15:08:40 +01:00
|
|
|
[
|
2013-07-25 19:03:43 +02:00
|
|
|
(Q_('?CLI:priority'), _('priority you want to give to the account'),
|
2010-02-08 15:08:40 +01:00
|
|
|
True),
|
2013-07-25 19:03:43 +02:00
|
|
|
(Q_('?CLI:account'), _('change the priority of the given account. '
|
2010-02-08 15:08:40 +01:00
|
|
|
'If not specified, change status of all accounts that have'
|
|
|
|
' "sync with global status" option set'), False)
|
|
|
|
]
|
|
|
|
],
|
2010-02-10 19:24:11 +01:00
|
|
|
'send_chat_message': [
|
2010-02-08 15:08:40 +01:00
|
|
|
_('Sends new chat message to a contact in the roster. Both OpenPGP key '
|
|
|
|
'and account are optional. If you want to set only \'account\', '
|
|
|
|
'without \'OpenPGP key\', just set \'OpenPGP key\' to \'\'.'),
|
|
|
|
[
|
|
|
|
('jid', _('JID of the contact that will receive the message'), True),
|
2013-07-25 19:03:43 +02:00
|
|
|
(Q_('?CLI:message'), _('message contents'), True),
|
2017-02-04 23:29:45 +01:00
|
|
|
(_('PGP key'), _('if specified, the message will be encrypted '
|
2010-02-08 15:08:40 +01:00
|
|
|
'using this public key'), False),
|
2013-07-25 19:03:43 +02:00
|
|
|
(Q_('?CLI:account'), _('if specified, the message will be sent '
|
2010-02-08 15:08:40 +01:00
|
|
|
'using this account'), False),
|
|
|
|
]
|
|
|
|
],
|
2010-02-10 19:24:11 +01:00
|
|
|
'send_single_message': [
|
2017-02-04 23:29:45 +01:00
|
|
|
_('Sends a chat message to someone on your roster. '
|
|
|
|
'Optionally with OpenPGP key and account. If you want '
|
|
|
|
'to only set the latter, set OpenPGP key to "".'),
|
2010-02-08 15:08:40 +01:00
|
|
|
[
|
|
|
|
('jid', _('JID of the contact that will receive the message'), True),
|
|
|
|
(_('subject'), _('message subject'), True),
|
2013-07-25 19:03:43 +02:00
|
|
|
(Q_('?CLI:message'), _('message contents'), True),
|
2017-02-04 23:29:45 +01:00
|
|
|
(_('PGP key'), _('if specified, the message will be encrypted '
|
2010-02-08 15:08:40 +01:00
|
|
|
'using this public key'), False),
|
2013-07-25 19:03:43 +02:00
|
|
|
(Q_('?CLI:account'), _('if specified, the message will be sent '
|
2010-02-08 15:08:40 +01:00
|
|
|
'using this account'), False),
|
|
|
|
]
|
|
|
|
],
|
2010-02-10 19:24:11 +01:00
|
|
|
'send_groupchat_message': [
|
2010-02-08 15:08:40 +01:00
|
|
|
_('Sends new message to a groupchat you\'ve joined.'),
|
|
|
|
[
|
|
|
|
('room_jid', _('JID of the room that will receive the message'), True),
|
2013-07-25 19:03:43 +02:00
|
|
|
(Q_('?CLI:message'), _('message contents'), True),
|
|
|
|
(Q_('?CLI:account'), _('if specified, the message will be sent '
|
2010-02-08 15:08:40 +01:00
|
|
|
'using this account'), False),
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'contact_info': [
|
|
|
|
_('Gets detailed info on a contact'),
|
|
|
|
[
|
|
|
|
('jid', _('JID of the contact'), True)
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'account_info': [
|
|
|
|
_('Gets detailed info on a account'),
|
|
|
|
[
|
|
|
|
('account', _('Name of the account'), True)
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'send_file': [
|
|
|
|
_('Sends file to a contact'),
|
|
|
|
[
|
|
|
|
(_('file'), _('File path'), True),
|
|
|
|
('jid', _('JID of the contact'), True),
|
2013-07-25 19:03:43 +02:00
|
|
|
(Q_('?CLI:account'), _('if specified, file will be sent using this '
|
2010-02-08 15:08:40 +01:00
|
|
|
'account'), False)
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'prefs_list': [
|
|
|
|
_('Lists all preferences and their values'),
|
|
|
|
[ ]
|
|
|
|
],
|
|
|
|
'prefs_put': [
|
|
|
|
_('Sets value of \'key\' to \'value\'.'),
|
|
|
|
[
|
|
|
|
(_('key=value'), _('\'key\' is the name of the preference, '
|
2017-02-04 23:29:45 +01:00
|
|
|
'\'value\' is what to set it to'), True)
|
2010-02-08 15:08:40 +01:00
|
|
|
]
|
|
|
|
],
|
|
|
|
'prefs_del': [
|
|
|
|
_('Deletes a preference item'),
|
|
|
|
[
|
|
|
|
(_('key'), _('name of the preference to be deleted'), True)
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'prefs_store': [
|
|
|
|
_('Writes the current state of Gajim preferences to the .config '
|
|
|
|
'file'),
|
|
|
|
[ ]
|
|
|
|
],
|
|
|
|
'remove_contact': [
|
|
|
|
_('Removes contact from roster'),
|
|
|
|
[
|
|
|
|
('jid', _('JID of the contact'), True),
|
2013-07-25 19:03:43 +02:00
|
|
|
(Q_('?CLI:account'), _('if specified, contact is taken from the '
|
2010-02-08 15:08:40 +01:00
|
|
|
'contact list of this account'), False)
|
|
|
|
|
|
|
|
]
|
|
|
|
],
|
|
|
|
|
|
|
|
'get_status': [
|
|
|
|
_('Returns current status (the global one unless account is specified)'),
|
|
|
|
[
|
2013-07-25 19:03:43 +02:00
|
|
|
(Q_('?CLI:account'), '', False)
|
2010-02-08 15:08:40 +01:00
|
|
|
]
|
|
|
|
],
|
|
|
|
|
|
|
|
'get_status_message': [
|
|
|
|
_('Returns current status message (the global one unless account is specified)'),
|
|
|
|
[
|
2013-07-25 19:03:43 +02:00
|
|
|
(Q_('?CLI:account'), '', False)
|
2010-02-08 15:08:40 +01:00
|
|
|
]
|
|
|
|
],
|
|
|
|
|
|
|
|
'get_unread_msgs_number': [
|
|
|
|
_('Returns number of unread messages'),
|
|
|
|
[ ]
|
|
|
|
],
|
2017-12-04 17:06:24 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
'send_xml': [
|
|
|
|
_('Sends custom XML'),
|
|
|
|
[
|
|
|
|
('xml', _('XML to send'), True),
|
2017-02-04 23:29:45 +01:00
|
|
|
('account', _('Account to which the XML will be sent; '
|
|
|
|
'if not specified, XML will be sent to all accounts'),
|
2010-02-08 15:08:40 +01:00
|
|
|
False)
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'change_avatar': [
|
|
|
|
_('Change the avatar'),
|
|
|
|
[
|
|
|
|
('picture', _('Picture to use'), True),
|
|
|
|
('account', _('Account in which the avatar will be set; '
|
|
|
|
'if not specified, the avatar will be set for all accounts'),
|
|
|
|
False)
|
|
|
|
]
|
|
|
|
],
|
2017-12-04 17:06:24 +01:00
|
|
|
|
2010-02-10 19:24:11 +01:00
|
|
|
'check_gajim_running': [
|
2010-02-08 15:08:40 +01:00
|
|
|
_('Check if Gajim is running'),
|
|
|
|
[]
|
|
|
|
],
|
2010-02-10 19:24:11 +01:00
|
|
|
'toggle_ipython': [
|
2010-02-08 15:08:40 +01:00
|
|
|
_('Shows or hides the ipython window'),
|
|
|
|
[]
|
|
|
|
],
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
self.sbus = None
|
|
|
|
if self.argv_len < 2 or sys.argv[1] not in self.commands.keys():
|
|
|
|
# no args or bad args
|
|
|
|
send_error(self.compose_help())
|
|
|
|
self.command = sys.argv[1]
|
|
|
|
if self.command == 'help':
|
|
|
|
if self.argv_len == 3:
|
2013-01-01 19:36:56 +01:00
|
|
|
print(self.help_on_command(sys.argv[2]).encode(
|
|
|
|
PREFERRED_ENCODING))
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
2013-01-01 19:36:56 +01:00
|
|
|
print(self.compose_help().encode(PREFERRED_ENCODING))
|
2010-02-08 15:08:40 +01:00
|
|
|
sys.exit(0)
|
|
|
|
if self.command == 'check_gajim_running':
|
2013-01-01 19:36:56 +01:00
|
|
|
print(self.check_gajim_running())
|
2010-02-08 15:08:40 +01:00
|
|
|
sys.exit(0)
|
|
|
|
self.init_connection()
|
|
|
|
self.check_arguments()
|
|
|
|
|
|
|
|
if self.command == 'contact_info':
|
|
|
|
if self.argv_len < 3:
|
|
|
|
send_error(_('Missing argument "contact_jid"'))
|
|
|
|
|
|
|
|
try:
|
|
|
|
res = self.call_remote_method()
|
|
|
|
except exceptions.ServiceNotAvailable:
|
|
|
|
# At this point an error message has already been displayed
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
self.print_result(res)
|
|
|
|
|
|
|
|
def print_result(self, res):
|
|
|
|
"""
|
|
|
|
Print retrieved result to the output
|
|
|
|
"""
|
|
|
|
if res is not None:
|
2017-12-04 17:06:24 +01:00
|
|
|
if self.command in ('send_chat_message', 'send_single_message'):
|
|
|
|
self.argv_len -= 2
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
if res is False:
|
|
|
|
if self.argv_len < 4:
|
|
|
|
send_error(_('\'%s\' is not in your roster.\n'
|
|
|
|
'Please specify account for sending the message.') % sys.argv[2])
|
|
|
|
else:
|
|
|
|
send_error(_('You have no active account'))
|
|
|
|
elif self.command == 'list_accounts':
|
|
|
|
if isinstance(res, list):
|
|
|
|
for account in res:
|
2013-01-01 21:06:16 +01:00
|
|
|
print(account)
|
2010-02-08 15:08:40 +01:00
|
|
|
elif self.command == 'account_info':
|
|
|
|
if res:
|
2013-01-01 19:36:56 +01:00
|
|
|
print(self.print_info(0, res, True))
|
2010-02-08 15:08:40 +01:00
|
|
|
elif self.command == 'list_contacts':
|
|
|
|
for account_dict in res:
|
2013-01-01 19:36:56 +01:00
|
|
|
print(self.print_info(0, account_dict, True))
|
2010-02-08 15:08:40 +01:00
|
|
|
elif self.command == 'prefs_list':
|
|
|
|
pref_keys = sorted(res.keys())
|
|
|
|
for pref_key in pref_keys:
|
|
|
|
result = '%s = %s' % (pref_key, res[pref_key])
|
2013-01-01 21:06:16 +01:00
|
|
|
print(result)
|
2010-02-08 15:08:40 +01:00
|
|
|
elif self.command == 'contact_info':
|
2013-01-01 19:36:56 +01:00
|
|
|
print(self.print_info(0, res, True))
|
2010-02-08 15:08:40 +01:00
|
|
|
elif res:
|
2013-01-01 21:06:16 +01:00
|
|
|
print(res)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def check_gajim_running(self):
|
|
|
|
if not self.sbus:
|
|
|
|
try:
|
|
|
|
self.sbus = dbus.SessionBus()
|
|
|
|
except Exception:
|
|
|
|
raise exceptions.SessionBusNotPresent
|
|
|
|
|
|
|
|
test = False
|
|
|
|
if hasattr(self.sbus, 'name_has_owner'):
|
|
|
|
if self.sbus.name_has_owner(SERVICE):
|
|
|
|
test = True
|
|
|
|
elif dbus.dbus_bindings.bus_name_has_owner(self.sbus.get_connection(),
|
|
|
|
SERVICE):
|
|
|
|
test = True
|
|
|
|
return test
|
|
|
|
|
|
|
|
def init_connection(self):
|
|
|
|
"""
|
|
|
|
Create the onnection to the session dbus, or exit if it is not possible
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self.sbus = dbus.SessionBus()
|
|
|
|
except Exception:
|
|
|
|
raise exceptions.SessionBusNotPresent
|
|
|
|
|
|
|
|
if not self.check_gajim_running():
|
2017-02-04 23:29:45 +01:00
|
|
|
#Do not translate "gajim-remote"
|
2010-02-08 15:08:40 +01:00
|
|
|
send_error(_('It seems Gajim is not running. So you can\'t use gajim-remote.'))
|
|
|
|
obj = self.sbus.get_object(SERVICE, OBJ_PATH)
|
|
|
|
interface = dbus.Interface(obj, INTERFACE)
|
|
|
|
|
|
|
|
# get the function asked
|
|
|
|
self.method = interface.__getattr__(self.command)
|
|
|
|
|
|
|
|
def make_arguments_row(self, args):
|
|
|
|
"""
|
|
|
|
Return arguments list. Mandatory arguments are enclosed with:
|
|
|
|
'<', '>', optional arguments - with '[', ']'
|
|
|
|
"""
|
|
|
|
s = ''
|
|
|
|
for arg in args:
|
|
|
|
if arg[2]:
|
|
|
|
s += ' <' + arg[0] + '>'
|
|
|
|
else:
|
|
|
|
s += ' [' + arg[0] + ']'
|
|
|
|
return s
|
|
|
|
|
|
|
|
def help_on_command(self, command):
|
|
|
|
"""
|
|
|
|
Return help message for a given command
|
|
|
|
"""
|
|
|
|
if command in self.commands:
|
|
|
|
command_props = self.commands[command]
|
|
|
|
arguments_str = self.make_arguments_row(command_props[1])
|
|
|
|
str_ = _('Usage: %(basename)s %(command)s %(arguments)s \n\t %(help)s')\
|
|
|
|
% {'basename': BASENAME, 'command': command,
|
|
|
|
'arguments': arguments_str, 'help': command_props[0]}
|
|
|
|
if len(command_props[1]) > 0:
|
|
|
|
str_ += '\n\n' + _('Arguments:') + '\n'
|
|
|
|
for argument in command_props[1]:
|
|
|
|
str_ += ' ' + argument[0] + ' - ' + argument[1] + '\n'
|
|
|
|
return str_
|
|
|
|
send_error(_('%s not found') % command)
|
|
|
|
|
|
|
|
def compose_help(self):
|
|
|
|
"""
|
|
|
|
Print usage, and list available commands
|
|
|
|
"""
|
2013-08-26 07:16:51 +02:00
|
|
|
s = _('Usage:\n %s command [arguments]\n\nCommand is one of:\n' ) % (
|
|
|
|
BASENAME)
|
2010-02-08 15:08:40 +01:00
|
|
|
for command in sorted(self.commands):
|
|
|
|
s += ' ' + command
|
|
|
|
for arg in self.commands[command][1]:
|
|
|
|
if arg[2]:
|
|
|
|
s += ' <' + arg[0] + '>'
|
|
|
|
else:
|
|
|
|
s += ' [' + arg[0] + ']'
|
|
|
|
s += '\n'
|
|
|
|
return s
|
|
|
|
|
|
|
|
def print_info(self, level, prop_dict, encode_return = False):
|
|
|
|
"""
|
|
|
|
Return formated string from data structure
|
|
|
|
"""
|
|
|
|
if prop_dict is None or not isinstance(prop_dict, (dict, list, tuple)):
|
|
|
|
return ''
|
|
|
|
ret_str = ''
|
|
|
|
if isinstance(prop_dict, (list, tuple)):
|
|
|
|
ret_str = ''
|
|
|
|
spacing = ' ' * level * 4
|
|
|
|
for val in prop_dict:
|
|
|
|
if val is None:
|
|
|
|
ret_str +='\t'
|
|
|
|
elif isinstance(val, int):
|
|
|
|
ret_str +='\t' + str(val)
|
2013-01-01 21:06:16 +01:00
|
|
|
elif isinstance(val, str):
|
2010-02-08 15:08:40 +01:00
|
|
|
ret_str +='\t' + val
|
|
|
|
elif isinstance(val, (list, tuple)):
|
|
|
|
res = ''
|
|
|
|
for items in val:
|
|
|
|
res += self.print_info(level+1, items)
|
|
|
|
if res != '':
|
|
|
|
ret_str += '\t' + res
|
|
|
|
elif isinstance(val, dict):
|
|
|
|
ret_str += self.print_info(level+1, val)
|
|
|
|
ret_str = '%s(%s)\n' % (spacing, ret_str[1:])
|
|
|
|
elif isinstance(prop_dict, dict):
|
|
|
|
for key in prop_dict.keys():
|
|
|
|
val = prop_dict[key]
|
|
|
|
spacing = ' ' * level * 4
|
2013-01-01 21:06:16 +01:00
|
|
|
if isinstance(val, (int, str)):
|
2010-02-08 15:08:40 +01:00
|
|
|
if val is not None:
|
|
|
|
val = val.strip()
|
|
|
|
ret_str += '%s%-10s: %s\n' % (spacing, key, val)
|
|
|
|
elif isinstance(val, (list, tuple)):
|
|
|
|
res = ''
|
|
|
|
for items in val:
|
|
|
|
res += self.print_info(level+1, items)
|
|
|
|
if res != '':
|
|
|
|
ret_str += '%s%s: \n%s' % (spacing, key, res)
|
|
|
|
elif isinstance(val, dict):
|
|
|
|
res = self.print_info(level+1, val)
|
|
|
|
if res != '':
|
|
|
|
ret_str += '%s%s: \n%s' % (spacing, key, res)
|
|
|
|
if (encode_return):
|
|
|
|
try:
|
|
|
|
ret_str = ret_str.encode(PREFERRED_ENCODING)
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
return ret_str
|
|
|
|
|
|
|
|
def check_arguments(self):
|
|
|
|
"""
|
|
|
|
Make check if all necessary arguments are given
|
|
|
|
"""
|
|
|
|
argv_len = self.argv_len - 2
|
|
|
|
args = self.commands[self.command][1]
|
|
|
|
if len(args) < argv_len:
|
|
|
|
send_error(_('Too many arguments. \n'
|
|
|
|
'Type "%(basename)s help %(command)s" for more info') % {
|
|
|
|
'basename': BASENAME, 'command': self.command})
|
|
|
|
if len(args) > argv_len:
|
|
|
|
if args[argv_len][2]:
|
|
|
|
send_error(_('Argument "%(arg)s" is not specified. \n'
|
|
|
|
'Type "%(basename)s help %(command)s" for more info') %
|
|
|
|
{'arg': args[argv_len][0], 'basename': BASENAME,
|
|
|
|
'command': self.command})
|
|
|
|
self.arguments = []
|
|
|
|
i = 0
|
|
|
|
for arg in sys.argv[2:]:
|
|
|
|
i += 1
|
|
|
|
if i < len(args):
|
|
|
|
self.arguments.append(arg)
|
|
|
|
else:
|
|
|
|
# it's latest argument with spaces
|
|
|
|
self.arguments.append(' '.join(sys.argv[i+1:]))
|
|
|
|
break
|
|
|
|
# add empty string for missing args
|
|
|
|
self.arguments += ['']*(len(args)-i)
|
|
|
|
|
|
|
|
def call_remote_method(self):
|
|
|
|
"""
|
|
|
|
Calls self.method with arguments from sys.argv[2:]
|
|
|
|
"""
|
|
|
|
args = [i.decode(PREFERRED_ENCODING) for i in self.arguments]
|
|
|
|
args = [dbus.String(i) for i in args]
|
|
|
|
try:
|
|
|
|
res = self.method(*args)
|
|
|
|
return res
|
|
|
|
except Exception:
|
|
|
|
raise exceptions.ServiceNotAvailable
|
|
|
|
return None
|
2005-08-01 17:23:45 +02:00
|
|
|
|
2005-07-21 23:00:18 +02:00
|
|
|
if __name__ == '__main__':
|
2010-02-08 15:08:40 +01:00
|
|
|
GajimRemote()
|