auto command args from message args
[dbuscron] / dbuscron / command.py
1 # encoding: utf-8
2
3 import os
4 from dbuscron.bus import get_dbus_message_type, dbus_to_str
5 from dbuscron.logger import Logger
6 log = Logger(__name__)
7
8 class Command(object):
9     def __init__(self, cmd):
10         self.__value = cmd
11         if self.is_shell_cmd:
12             self.__file = os.environ.get('SHELL', '/bin/sh')
13             self.__args = [self.__file, '-c', self.__value]
14         else:
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
21             else:
22                 self.__auto_args = False
23
24     def __call__(self, bus, message, environ):
25         args_list = map(dbus_to_str, message.get_args_list())
26         env = dict()
27         env.update(environ)
28         try:
29             dbus_env = dict(
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)
39                     )
40             env.update(dbus_env)
41         except Exception, e:
42             log.error('environ exception', e)
43             raise e
44
45         if self.__auto_args:
46             args_list.insert(0, self.__file)
47         else:
48             args_list = self.__args
49
50         result = os.spawnvpe(os.P_WAIT, self.__file, args_list, env)
51         if result != 0:
52             log.warn('command returned non-zero status', self.__file, args_list, dbus_env, result)
53         return result
54
55     @property
56     def is_shell_cmd(self):
57         for c in '|><$&;{}':
58             if c in self.__value:
59                 return True
60         return False
61
62     def __str__(self):
63         return self.__value
64
65 class Commands(object):
66     __commands = {}
67     __environ = {}
68
69     def __iter__(self):
70         for m, c in self.__commands.iteritems():
71             yield m, c
72
73     def _get_environ(self):
74         return self.__environ
75
76     def _set_environ(self, value):
77         self.__environ = dict()
78         self.__environ.update(os.environ)
79         self.__environ.update(value)
80
81     environ = property(_get_environ, _set_environ)
82
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)
88                 return
89
90     def add(self, matcher, command):
91         self.__commands[matcher] = command
92
93     def clear(self):
94         self.__commands = {}
95