Handle missing PyMTP library (bug 924)
[gpodder.git] / src / gpodder / services.py
blob4a12bb385d8371e2bd0c587628ad8143f5ca4ee0
1 # -*- coding: utf-8 -*-
3 # gPodder - A media aggregator and podcast client
4 # Copyright (c) 2005-2010 Thomas Perl and the gPodder Team
6 # gPodder 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 # gPodder 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 this program. If not, see <http://www.gnu.org/licenses/>.
22 # services.py -- Core Services for gPodder
23 # Thomas Perl <thp@perli.net> 2007-08-24
27 import gpodder
28 from gpodder.liblogger import log
30 from gpodder import util
31 from gpodder import download
33 import threading
34 import time
35 import urllib2
36 import os
38 _ = gpodder.gettext
40 class ObservableService(object):
41 def __init__(self, signal_names=[]):
42 self.observers = {}
43 for signal in signal_names:
44 self.observers[signal] = []
46 def register(self, signal_name, observer):
47 if signal_name in self.observers:
48 if not observer in self.observers[signal_name]:
49 self.observers[signal_name].append(observer)
50 else:
51 log('Observer already added to signal "%s".', signal_name, sender=self)
52 else:
53 log('Signal "%s" is not available for registration.', signal_name, sender=self)
55 def unregister(self, signal_name, observer):
56 if signal_name in self.observers:
57 if observer in self.observers[signal_name]:
58 self.observers[signal_name].remove(observer)
59 else:
60 log('Observer could not be removed from signal "%s".', signal_name, sender=self)
61 else:
62 log('Signal "%s" is not available for un-registration.', signal_name, sender=self)
64 def notify(self, signal_name, *args):
65 if signal_name in self.observers:
66 for observer in self.observers[signal_name]:
67 util.idle_add(observer, *args)
68 else:
69 log('Signal "%s" is not available for notification.', signal_name, sender=self)
72 class DependencyManager(object):
73 def __init__(self):
74 self.dependencies = []
76 def depend_on(self, feature_name, description, modules, tools):
77 self.dependencies.append([feature_name, description, modules, tools])
79 def modules_available(self, modules):
80 """
81 Receives a list of modules and checks if each
82 of them is available. Returns a tuple with the
83 first item being a boolean variable that is True
84 when all required modules are available and False
85 otherwise. The second item is a dictionary that
86 lists every module as key with the available as
87 boolean value.
88 """
89 result = {}
90 all_available = True
91 for module in modules:
92 try:
93 __import__(module)
94 result[module] = True
95 except:
96 result[module] = False
97 all_available = False
99 return (all_available, result)
101 def tools_available(self, tools):
103 See modules_available.
105 result = {}
106 all_available = True
107 for tool in tools:
108 if util.find_command(tool):
109 result[tool] = True
110 else:
111 result[tool] = False
112 all_available = False
114 return (all_available, result)
117 dependency_manager = DependencyManager()
120 # Register non-module-specific dependencies here
121 dependency_manager.depend_on(_('Bluetooth file transfer'), _('Send podcast episodes to Bluetooth devices. Needs the bluetooth-sendto command from gnome-bluetooth.'), ['bluetooth'], ['bluetooth-sendto'])
122 dependency_manager.depend_on(_('HTML episode shownotes'), _('Display episode shownotes in HTML format using GTKHTML2.'), ['gtkhtml2'], [])