Include NEWS file.
[laditools.git] / laditools / controller.py
blobd987dfb6b04fa808d4bceff23b7b6030ad494da2
1 #!/usr/bin/python
2 # LADITools - Linux Audio Desktop Integration Tools
3 # Copyright (C) 2012 Alessio Treglia <quadrispro@ubuntu.com>
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 import dbus
19 import sys
21 class LadiController(object):
22 """Wrap common routines used by D-Bus objects.
24 It is recommended to use one of the available implementations
25 instead of creating instances of this class.
26 """
28 def __init__(self, dbus_type, service_name, obj_path, iface_name, args = None):
29 # Connect to the bus
30 self.bus = getattr(dbus, dbus_type)()
31 self.controller_obj = self.bus.get_object (service_name, obj_path)
32 self.controller_iface = dbus.Interface (self.controller_obj, iface_name)
34 def is_available (self):
35 """Check if the service is available."""
36 try:
37 self.is_started ()
38 return True
39 except Exception, err:
40 sys.stderr.write(str(err) + '\n')
41 sys.stderr.flush()
42 return False
44 def is_started (self):
45 """Check if the service is running."""
46 return self.controller_iface.is_started ()
48 def start(self):
49 """Start the service."""
50 self.controller_iface.start()
52 def stop(self):
53 """Stop the service."""
54 self.controller_iface.stop()
56 def kill(self):
57 """Kill the service."""
58 self.controller_iface.exit()