From: Konstantin Stepanov Date: Tue, 14 Dec 2010 18:10:46 +0000 (+0200) Subject: more robust and unicode safe dbus string handling X-Git-Tag: v1.2.0~7 X-Git-Url: https://vcs.maemo.org/git/?p=dbuscron;a=commitdiff_plain;h=d9cbb54c14651fded4a8a1166e15ad288f347a92 more robust and unicode safe dbus string handling --- diff --git a/dbuscron/bus.py b/dbuscron/bus.py index 2bf9116..8ab3cf0 100644 --- a/dbuscron/bus.py +++ b/dbuscron/bus.py @@ -5,17 +5,24 @@ from dbuscron.logger import Logger log = Logger(__name__) def dbus_to_str(value): - log('converting', value, 'of type', type(value)) - if isinstance(value, dbus.Byte): - return str(int(value)) - elif isinstance(value, dbus.ByteArray): - return ','.join(str(ord(v)) for v in value) - elif isinstance(value, dbus.Array): - return ','.join(dbus_to_str(v) for v in value) - elif isinstance(value, dbus.Dictionary): - return ','.join('%s:%s' % (k, dbus_to_str(v)) for k, v in value.iteritems()) - else: - return str(value) + try: + if isinstance(value, dbus.Byte): + result = str(int(value)) + elif isinstance(value, dbus.ByteArray): + result = ','.join(str(ord(v)) for v in value) + elif isinstance(value, dbus.Array): + result = ','.join(dbus_to_str(v) for v in value) + elif isinstance(value, dbus.Dictionary): + result = ','.join('%s:%s' % (k, dbus_to_str(v)) for k, v in value.iteritems()) + elif isinstance(value, dbus.String): + result = value.encode('utf-8') + else: + result = str(value) + return result + except Exception, e: + log.error('convert exception', e) + raise e + def get_dbus_message_type(message): result = message.__class__.__name__[0:-7] diff --git a/dbuscron/command.py b/dbuscron/command.py index 675309f..790ccf0 100644 --- a/dbuscron/command.py +++ b/dbuscron/command.py @@ -1,3 +1,4 @@ +# encoding: utf-8 import os from dbuscron.bus import get_dbus_message_type, dbus_to_str @@ -18,21 +19,26 @@ class Command(object): args_list = message.get_args_list() env = dict() env.update(environ) - dbus_env = dict( - (('DBUS_ARG%d' % i, dbus_to_str(a)) for i, a in enumerate(args_list)), - DBUS_ARGN = str(len(args_list)), - DBUS_SENDER = str(message.get_sender()), - DBUS_DEST = str(message.get_destination()), - DBUS_IFACE = str(message.get_interface()), - DBUS_PATH = str(message.get_path()), - DBUS_MEMBER = str(message.get_member()), - DBUS_BUS = bus.__class__.__name__.lower()[0:-3], - DBUS_TYPE = get_dbus_message_type(message) - ) - env.update(dbus_env) + try: + dbus_env = dict( + (('DBUS_ARG%d' % i, dbus_to_str(a)) for i, a in enumerate(args_list)), + DBUS_ARGN = str(len(args_list)), + DBUS_SENDER = str(message.get_sender()), + DBUS_DEST = str(message.get_destination()), + DBUS_IFACE = str(message.get_interface()), + DBUS_PATH = str(message.get_path()), + DBUS_MEMBER = str(message.get_member()), + DBUS_BUS = bus.__class__.__name__.lower()[0:-3], + DBUS_TYPE = get_dbus_message_type(message) + ) + env.update(dbus_env) + except Exception, e: + log.error('environ exception', e) + raise e + result = os.spawnvpe(os.P_WAIT, self.__file, self.__args, env) if result != 0: - log.warn('run', self.__file, self.__args, dbus_env, result) + log.warn('command returned non-zero status', self.__file, self.__args, dbus_env, result) return result @property