support \xXX escape characters in args field
[dbuscron] / dbuscron / command.py
index e7d8c68..74a1c46 100644 (file)
@@ -1,5 +1,9 @@
+# encoding: utf-8
 
 import os
+from dbuscron.bus import get_dbus_message_type, dbus_to_str
+from dbuscron.logger import Logger
+log = Logger(__name__)
 
 class Command(object):
     def __init__(self, cmd):
@@ -12,22 +16,29 @@ class Command(object):
             self.__file = self.__args[0]
 
     def __call__(self, bus, message, environ):
-
         args_list = message.get_args_list()
         env = dict()
         env.update(environ)
-        env.update(dict(
-                (('DBUS_ARG%d' % i, str(args_list[i])) for i in range(0, len(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)
-                ))
+        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('command returned non-zero status', self.__file, self.__args, dbus_env, result)
         return result
 
     @property
@@ -44,6 +55,10 @@ class Commands(object):
     __commands = {}
     __environ = {}
 
+    def __iter__(self):
+        for m, c in self.__commands.iteritems():
+            yield m, c
+
     def _get_environ(self):
         return self.__environ
 
@@ -57,9 +72,13 @@ class Commands(object):
     def handler(self, bus, message):
         for rule, command in self.__commands.iteritems():
             if rule.match(bus, message):
+                log('rule matched', rule, command)
                 command(bus, message, self.__environ)
                 return
 
     def add(self, matcher, command):
         self.__commands[matcher] = command
 
+    def clear(self):
+        self.__commands = {}
+