3 # This file is part of Panucci.
4 # Copyright (c) 2008-2009 The Panucci Audiobook and Podcast Player Project
6 # Panucci is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # Panucci is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Panucci. If not, see <http://www.gnu.org/licenses/>.
19 # from services.py -- Core Services for gPodder
20 # Thomas Perl <thp@perli.net> 2007-08-24
22 # 2009-02-13 - nikosapi: made ObservableService more Panucci-esque
25 from logging
import getLogger
27 class ObservableService(object):
28 def __init__(self
, signal_names
=[], log
=None):
29 self
.__log
= getLogger('ObservableService') if log
is None else log
32 for signal
in signal_names
:
33 self
.observers
[signal
] = []
36 self
.__log
.warning('No signal names defined...')
38 def register(self
, signal_name
, observer
):
39 if signal_name
in self
.observers
:
40 if not observer
in self
.observers
[signal_name
]:
41 self
.observers
[signal_name
].append(observer
)
42 self
.__log
.debug( 'Registered "%s" as an observer for "%s"',
43 observer
.__name
__, signal_name
)
46 'Observer "%s" is already added to signal "%s"',
47 observer
.__name
__, signal_name
)
50 'Signal "%s" is not available for registration', signal_name
)
52 def unregister(self
, signal_name
, observer
):
53 if signal_name
in self
.observers
:
54 if observer
in self
.observers
[signal_name
]:
55 self
.observers
[signal_name
].remove(observer
)
56 self
.__log
.debug( 'Unregistered "%s" as an observer for "%s"',
57 observer
.__name
__, signal_name
)
60 'Observer "%s" could not be removed from signal "%s"',
61 observer
.__name
__, signal_name
)
64 'Signal "%s" is not available for un-registration.',
67 def notify(self
, signal_name
, *args
, **kwargs
):
68 caller
= kwargs
.get('caller')
69 caller
= 'UnknownCaller' if caller
is None else caller
.__name
__
71 if signal_name
in self
.observers
:
73 'Sending signal "%s" for caller "%s"', signal_name
, caller
)
75 for observer
in self
.observers
[signal_name
]:
79 'Signal "%s" (from caller "%s") is not available '
80 'for notification', signal_name
, caller
)