3 # from services.py -- Core Services for gPodder
4 # Thomas Perl <thp@perli.net> 2007-08-24
6 # 2009-02-13 - nikosapi: made ObservableService more Panucci-esque
9 from logging
import getLogger
11 class ObservableService(object):
12 def __init__(self
, signal_names
=[], log
=None):
13 self
.__log
= getLogger('ObservableService') if log
is None else log
16 for signal
in signal_names
:
17 self
.observers
[signal
] = []
20 self
.__log
.warning('No signal names defined...')
22 def register(self
, signal_name
, observer
):
23 if signal_name
in self
.observers
:
24 if not observer
in self
.observers
[signal_name
]:
25 self
.observers
[signal_name
].append(observer
)
26 self
.__log
.debug( 'Registered "%s" as an observer for "%s"',
27 observer
.__name
__, signal_name
)
30 'Observer "%s" is already added to signal "%s"',
31 observer
.__name
__, signal_name
)
34 'Signal "%s" is not available for registration', signal_name
)
36 def unregister(self
, signal_name
, observer
):
37 if signal_name
in self
.observers
:
38 if observer
in self
.observers
[signal_name
]:
39 self
.observers
[signal_name
].remove(observer
)
40 self
.__log
.debug( 'Unregistered "%s" as an observer for "%s"',
41 observer
.__name
__, signal_name
)
44 'Observer "%s" could not be removed from signal "%s"',
45 observer
.__name
__, signal_name
)
48 'Signal "%s" is not available for un-registration.',
51 def notify(self
, signal_name
, *args
, **kwargs
):
52 caller
= kwargs
.get('caller')
53 caller
= 'UnknownCaller' if caller
is None else caller
.__name
__
55 if signal_name
in self
.observers
:
57 'Sending signal "%s" for caller "%s"', signal_name
, caller
)
59 for observer
in self
.observers
[signal_name
]:
63 'Signal "%s" (from caller "%s") is not available '
64 'for notification', signal_name
, caller
)