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]
17 if len(self.__args) == 1 \
18 and self.__file.startswith('!'):
19 self.__file = self.__file.lstrip('!')
20 self.__auto_args = True
22 self.__auto_args = False
24 def __call__(self, bus, message, environ):
25 args_list = map(dbus_to_str, message.get_args_list())
30 (('DBUS_ARG%d' % i, a) for i, a in enumerate(args_list)),
31 DBUS_ARGN = str(len(args_list)),
32 DBUS_SENDER = str(message.get_sender()),
33 DBUS_DEST = str(message.get_destination()),
34 DBUS_IFACE = str(message.get_interface()),
35 DBUS_PATH = str(message.get_path()),
36 DBUS_MEMBER = str(message.get_member()),
37 DBUS_BUS = bus.__class__.__name__.lower()[0:-3],
38 DBUS_TYPE = get_dbus_message_type(message)
42 log.error('environ exception', e)
46 args_list.insert(0, self.__file)
48 args_list = self.__args
50 result = os.spawnvpe(os.P_WAIT, self.__file, args_list, env)
52 log.warn('command returned non-zero status', self.__file, args_list, dbus_env, result)
56 def is_shell_cmd(self):
65 class Commands(object):
70 for m, c in self.__commands.iteritems():
73 def _get_environ(self):
76 def _set_environ(self, value):
77 self.__environ = dict()
78 self.__environ.update(os.environ)
79 self.__environ.update(value)
81 environ = property(_get_environ, _set_environ)
83 def handler(self, bus, message):
84 for rule, command in self.__commands.iteritems():
85 if rule.match(bus, message):
86 log('rule matched', rule, command)
87 command(bus, message, self.__environ)
90 def add(self, matcher, command):
91 self.__commands[matcher] = command