Details dialog for PlaylistItem objects
[panucci.git] / src / panucci / services.py
blob8c9fc55c841d0e2622c14b96645d85d264afdf49
1 #!/usr/bin/env python
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
31 self.observers = {}
32 for signal in signal_names:
33 self.observers[signal] = []
35 if not signal_names:
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 )
44 else:
45 self.__log.warning(
46 'Observer "%s" is already added to signal "%s"',
47 observer.__name__, signal_name )
48 else:
49 self.__log.warning(
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 )
58 else:
59 self.__log.warning(
60 'Observer "%s" could not be removed from signal "%s"',
61 observer.__name__, signal_name )
62 else:
63 self.__log.warning(
64 'Signal "%s" is not available for un-registration.',
65 signal_name )
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:
72 self.__log.debug(
73 'Sending signal "%s" for caller "%s"', signal_name, caller )
75 for observer in self.observers[signal_name]:
76 observer( *args )
77 else:
78 self.__log.warning(
79 'Signal "%s" (from caller "%s") is not available '
80 'for notification', signal_name, caller )