4 from dbuscron.bus import get_dbus_message_type, dbus_to_str
5 from dbuscron.logger import Logger
9 def __init__(self, cmd):
12 self.__file = os.environ.get('SHELL', '/bin/sh')
13 self.__args = [self.__file, '-c', self.__value]
15 self.__args = cmd.split(' ')
16 self.__file = self.__args[0]
18 def __call__(self, bus, message, environ):
19 args_list = message.get_args_list()
24 (('DBUS_ARG%d' % i, dbus_to_str(a)) for i, a in enumerate(args_list)),
25 DBUS_ARGN = str(len(args_list)),
26 DBUS_SENDER = str(message.get_sender()),
27 DBUS_DEST = str(message.get_destination()),
28 DBUS_IFACE = str(message.get_interface()),
29 DBUS_PATH = str(message.get_path()),
30 DBUS_MEMBER = str(message.get_member()),
31 DBUS_BUS = bus.__class__.__name__.lower()[0:-3],
32 DBUS_TYPE = get_dbus_message_type(message)
36 log.error('environ exception', e)
39 result = os.spawnvpe(os.P_WAIT, self.__file, self.__args, env)
41 log.warn('command returned non-zero status', self.__file, self.__args, dbus_env, result)
45 def is_shell_cmd(self):
54 class Commands(object):
59 for m, c in self.__commands.iteritems():
62 def _get_environ(self):
65 def _set_environ(self, value):
66 self.__environ = dict()
67 self.__environ.update(os.environ)
68 self.__environ.update(value)
70 environ = property(_get_environ, _set_environ)
72 def handler(self, bus, message):
73 for rule, command in self.__commands.iteritems():
74 if rule.match(bus, message):
75 log('rule matched', rule, command)
76 command(bus, message, self.__environ)
79 def add(self, matcher, command):
80 self.__commands[matcher] = command