summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornikosapi <me@nikosapi.org>2010-02-01 17:17:22 (GMT)
committer nikosapi <me@nikosapi.org>2010-02-01 17:17:22 (GMT)
commitf90f0117702ded503719707bd9217f654dec22d3 (patch)
tree1256101471bc58715ca409bf97f55b394b89970a
parent22f53582cecc1a6982b1d55ebf994d1e0f8e14e4 (diff)
downloadpanucci-f90f0117702ded503719707bd9217f654dec22d3.zip
panucci-f90f0117702ded503719707bd9217f654dec22d3.tar.gz
panucci-f90f0117702ded503719707bd9217f654dec22d3.tar.bz2
Implement a Forwarding ObservableService class
-rw-r--r--src/panucci/player.py12
-rw-r--r--src/panucci/services.py56
2 files changed, 62 insertions, 6 deletions
diff --git a/src/panucci/player.py b/src/panucci/player.py
index 5e685eb..9aee1f7 100644
--- a/src/panucci/player.py
+++ b/src/panucci/player.py
@@ -21,14 +21,14 @@ import logging
from playlist import Playlist
from settings import settings
-from services import ObservableService
+from services import ForwardingObservableService
from dbusinterface import interface
from backends import osso, gstplaybin
import util
-class PanucciPlayer(ObservableService):
+class PanucciPlayer(ForwardingObservableService):
"""
A proxy object which adds a layer of abstraction between all the different
backends. It picks the right backend for the job and forwards signals from
@@ -38,14 +38,14 @@ class PanucciPlayer(ObservableService):
def __init__(self, player):
self.__log = logging.getLogger('panucci.player.PanucciPlayer')
- ObservableService.__init__(self, self.signals, self.__log)
+ ForwardingObservableService.__init__(self, self.signals, self.__log)
self.__player = player
self.__initialized = False
# Forward the following signals
- def n(sig): return lambda *y: self.notify(sig, caller=PanucciPlayer, *y)
- for signal in [ "playing", "paused", "stopped", "eof" ]:
- self.__player.register( signal, n(signal) )
+ self.forward( self.__player,
+ [ "playing", "paused", "stopped", "eof" ],
+ PanucciPlayer )
self.__player.register( "playing", self.on_playing )
self.__player.register( "error", self.on_player_error )
diff --git a/src/panucci/services.py b/src/panucci/services.py
index 4701e35..489ef59 100644
--- a/src/panucci/services.py
+++ b/src/panucci/services.py
@@ -88,6 +88,62 @@ class ObservableService(object):
return rtn_value
+class ForwardingObservableService(ObservableService):
+ """ An object that builds on ObservableService which provides a simple
+ way to forward/proxy a bunch of signals through an object of this
+ type. Ie: This object will call it's notify() method whenever a
+ forwarded signal from another object is emitted. """
+
+ def forward( self, from_object, signal_names, caller=None ):
+ """ Register signals to be forwarded
+ from_object: the object from which the signals will be emitted
+ signal_names: a string, list, or dict of signal names
+ caller: the caller to be passed to notify()
+
+ To forward a single signal, signal_names can just be a string
+ containing the name of the signal.
+
+ To forward many signals, signal_names can be a list of strings.
+
+ If you wish to change the name of the emitted signal (ie. if the
+ original object's signal is named "foo-bar" but you want this
+ object to emit "foo") you can do so by passing a dict to
+ signal_names. This dict must contain string pairs:
+ { "from-object-name" : "new name" }
+ """
+
+ # bail if the from object isn't an ObservableService
+ if not isinstance( from_object, ObservableService ):
+ self.__log.error( "Can't forward signals for "
+ "non-ObservableService type objects")
+ return
+
+ signals = {} # final list of signals to registered
+
+ if isinstance( signal_names, str ):
+ signals[signal_names] = signal_names
+
+ elif isinstance( signal_names, list ):
+ for name in signal_names:
+ signals[name] = name
+
+ elif isinstance( signal_names, dict ):
+ signals = signal_names
+
+ for from_name, to_name in signals.iteritems():
+ from_object.register( from_name, self._forward( to_name, caller ))
+
+ def _forward( self, emitted_signal_name, caller ):
+ """ Returns a function which calls notify() with the appropriate
+ parameters. """
+
+ def _callback( *args, **kwargs ):
+ kwargs["caller"] = caller
+ self.notify( emitted_signal_name, *args, **kwargs )
+
+ return _callback
+
+
HEADPHONE_SYS = "/sys/devices/platform/gpio-switch/headphone/state"
class __headphone_watcher(ObservableService):