importsubprocess# Helpers for process outputdefprocess_stdout(args,input=None):""" Executes the process with the commandline specified in 'args' and returns the standard output """p=subprocess.Popen(args,stdout=subprocess.PIPE)retval=p.communicate(input=input)[0]try:p.terminate()exceptOSError:passreturnretvaldefprocess_retval(args,input=None):""" Executes the process with the commandline specified in 'args' and returns the return value """p=subprocess.Popen(args,stdout=subprocess.PIPE)p.communicate(input=input)returnp.returncodedefprocess_true(args,input=None):""" Executes a process and returns True if the process has a zero return code """returnprocess_retval(args,input=input)==0defprogram_running(progam):returnprocess_true(["pidof",progam])# DBus helpersclassDBusObject(object):""" Wrapper for a dbus object, so that the bus name and object name only needs to be specified once. """def__init__(self,bus_name,object_name,interface=None):importdbusbus=dbus.SessionBus()self._bus=busobj=bus.get_object(bus_name,object_name)ifinterfaceisnotNone:obj=dbus.Interface(obj,interface)self._obj=objdef__getattr__(self,name):deff(*args,**kwargs):returngetattr(self._obj,name)(*args,**kwargs)returnfclassDBusProperties(object):# I can't find a decent way of getting properties in python dbus, this is my# wrapper.def__init__(self,bus_name,object_name,interface_name):# dbus_objectimportdbusbus=dbus.SessionBus()self.dbus_object=bus.get_object(bus_name,object_name)self.interface_name=interface_nameself.dbus_props=dbus.Interface(self.dbus_object,"org.freedesktop.DBus.Properties")defget(self,property_name):returnself.dbus_props.Get(self.interface_name,property_name)defset(self,property_name,value):self.dbus_props.Set(self.interface_name,property_name,value)# Misc helpersdefcatch_unimplemented(c,replacement=None):""" Execute a callable c, returning replacement if it throws NotImplementedError """try:returnc()exceptNotImplementedError:returnreplacementclassPlayerException(Exception):""" An exception that should be handled nicely by the front end """def__init__(self,message):self.message=message