dbuscrontab utility: implemented -e/-l/-k actions, syntax checking
[dbuscron] / dbuscron.py
1 #!/usr/bin/python
2 #
3 # bus type sender interface path member destination args command
4 #
5 # Examples for N900:
6 #
7 # Headphones unplugged:
8 # S signal * org.freedesktop.Hal.Manager /org/freedesktop/Hal/Manager DeviceRemoved * * echo Headphones unplugged;
9 #
10 # Call recieved:
11 # S signal * com.nokia.csd.Call /com/nokia/csd/call Coming * * echo $DBUS_ARG1 is calling
12 #
13
14 import sys
15
16 if __name__ == '__main__':
17
18     from dbuscron import Logger, OptionsParser
19
20     options = OptionsParser('fvc:l:')
21     daemon = not options.f
22
23     logout = sys.stderr
24     if options.l:
25         logout = open(options.l, 'wb')
26
27     log = Logger(__name__, out=logout)
28     log.level = options.v + Logger.ERROR
29
30     if daemon:
31         from dbuscron.daemonize import daemonize
32         daemonize(
33             pidfile='/var/run/dbuscron.pid',
34             logfile='/var/log/dbuscron.log'
35             )
36
37     from dbuscron import DbusBus, DbusRule, Command, Commands, CrontabParser
38
39     bus = DbusBus()
40     commands = Commands()
41
42     crontab = CrontabParser(options.c or '/etc/dbuscrontab')
43
44     def load_config(parser):
45         for rule, cmd in parser:
46             matcher = DbusRule(**rule)
47             command = Command(cmd)
48             matcher.register()
49             log.info('%s %s' % (matcher, command))
50             commands.add(matcher, command)
51
52     load_config(crontab)
53
54     def reload_config_on_signal(sig_no, stack):
55         log.info('Signal #%d received: reloading config...' % (sig_no))
56         commands.clear()
57         load_config(crontab)
58         log.info('Done config reloading.')
59
60     import signal
61     signal.signal(signal.SIGHUP, reload_config_on_signal)
62
63     commands.environ = crontab.environ
64     bus.attach_handler(commands.handler)
65
66     try:
67         bus.listen()
68     except KeyboardInterrupt:
69         sys.exit(2)
70
71 # vim: ts=8 sts=4 sw=4 et
72