Import woodchuck from wc.
[feedingit] / src / wc.py
1 # Copyright (c) 2011 Neal H. Walfield
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 import traceback
17
18 # Don't fail if the Woodchuck modules are not available.  Just disable
19 # Woodchuck's functionality.
20
21 # Whether we imported the woodchuck modules successfully.
22 woodchuck_imported = True
23 try:
24     import pywoodchuck
25     from pywoodchuck import PyWoodchuck
26     from pywoodchuck import woodchuck
27 except ImportError, exception:
28     print ("Unable to load Woodchuck modules: disabling Woodchuck support: %s"
29            % traceback.format_exc ())
30     woodchuck_imported = False
31     class PyWoodchuck (object):
32         def available(self):
33             return False
34     woodchuck = None
35
36 # The default channel refresh interval: 6 hours.
37 refresh_interval = 6 * 60 * 60
38
39 class mywoodchuck (PyWoodchuck):
40     def __init__(self, listing, *args):
41         PyWoodchuck.__init__ (self, *args)
42
43         self.listing = listing
44
45     # Woodchuck upcalls.
46     def stream_update_cb(self, stream):
47         print ("stream update called on %s (%s)"
48                % (stream.human_readable_name, stream.identifier,))
49
50         # Make sure no one else is concurrently updating this
51         # feed.
52         try:
53             self.listing.updateFeed(stream.identifier)
54         except:
55             print ("Updating %s: %s"
56                    % (stream.identifier, traceback.format_exc ()))
57
58     def object_transfer_cb(self, stream, object,
59                            version, filename, quality):
60         log ("object transfer called on %s (%s) in stream %s (%s)"
61              % (object.human_readable_name, object.identifier,
62                 stream.human_readable_name, stream.identifier))
63
64 _w = None
65 def wc_init(listing):
66     global _w
67     assert _w is None
68     
69     _w = mywoodchuck (listing, "FeedingIt", "org.maemo.feedingit")
70
71     if not woodchuck_imported or not _w.available ():
72         print "Unable to contact Woodchuck server."
73     else:
74         print "Woodchuck appears to be available."
75
76 def wc():
77     """Connect to the woodchuck server and initialize any state."""
78     global _w
79     assert _w is not None
80     return _w