Updated Arabic Translation by Djihed Afifi.
[straw.git] / src / lib / FeedDataRouter.py
blobc190300df24e7da1f3942308fcdb2b05bfee3282
1 """ FeedDataRouter.py
3 Module for routing feed data from the poller
4 """
5 __copyright__ = "Copyright (c) 2002-2005 Free Software Foundation, Inc."
6 __license__ = """
7 Straw is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 2 of the License, or (at your option) any later
10 version.
12 Straw is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 Place - Suite 330, Boston, MA 02111-1307, USA. """
21 import Event
22 import ImageCache
24 class FeedDataRouter(object):
25 def __init__(self, feed):
26 ImageCache.cache.signal_connect(Event.ImageUpdatedSignal,
27 self.image_updated)
28 self._feed = feed
29 self._stopper = None
30 # stuff we are potentially waiting for
31 self._wait_data = 0
32 self._wait_images = {}
34 # public parts
35 def start_polling(self):
36 self._wait_data = 1
37 self.update_process_status()
39 def route_no_data(self):
40 self._wait_data = 0
41 self._feed.error = None
42 self._stopper = None
43 self.update_process_status()
45 def route_all(self, header, parsed):
46 needed_loading = self._feed.load_contents()
47 self.route_data(header, parsed)
48 self.route_extras()
49 if needed_loading:
50 self._feed.unload_contents()
52 def route_data(self, header, parsed):
53 if header:
54 etag = header.getheader('etag', None)
55 if etag:
56 etag = etag.strip()
57 self._feed.previous_etag = etag
58 self._feed.channel_title = parsed.title
59 self._feed.channel_description = parsed.description
60 self._feed.channel_copyright = parsed.copyright
61 self._feed.channel_link = parsed.link
62 self._feed.channel_creator = parsed.creator
63 if len(parsed.items):
64 parsed.items.reverse()
65 self._feed.add_items(parsed.items)
66 self._feed.error = None
67 self._wait_data = 0
68 self._feed.poll_done()
70 def route_extras(self):
71 try:
72 for i in self._feed.items:
73 for name in i.image_keys():
74 image = i.get_image(name)
75 if image.status == image.WAITING:
76 self._wait_images[image.url] = 1
77 except Exception, ex:
78 # if we don't get the images, we can probably still show
79 # the text
80 pass
81 self.update_process_status()
83 def set_error(self, err):
84 self._wait_data = 0
85 self._feed.error = err
86 self.update_process_status()
87 return
89 def set_stopper(self, stopper):
90 self._stopper = stopper
91 stopper.signal_connect(Event.PollingStoppedSignal,
92 self._polling_stopped)
93 def get_stopper(self): return self._stopper
94 stopper = property(get_stopper, set_stopper)
96 def poll_done(self):
97 self._feed.poll_done()
98 self._stopper = None
100 def stop_polling(self):
101 if self._stopper:
102 self._stopper.stop()
103 self._stopper = None
104 for image in self._wait_images.keys():
105 ImageCache.cache.stop_transfer(image)
106 del self._wait_images[image]
107 self.update_process_status()
108 self._feed.poll_done()
110 def _polling_stopped(self, signal):
111 self.poll_done()
113 # private parts
115 def image_updated(self, signal):
116 if signal.url in self._wait_images:
117 del self._wait_images[signal.url]
118 self.update_process_status()
120 def update_process_status(self):
121 length_images = len(self._wait_images)
122 if self._wait_data or length_images:
123 self._feed.process_status = self._feed.STATUS_POLLING
124 else:
125 self._feed.process_status = self._feed.STATUS_IDLE