Redirect output to log files.
[feedingit] / src / update_feeds.py
1 #!/usr/bin/env python2.5
2
3
4 # Copyright (c) 2007-2008 INdT.
5 # Copyright (c) 2011 Neal H. Walfield
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Lesser 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.
10 #
11 #  This program 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 Lesser General Public License for more details.
15 #
16 #  You should have received a copy of the GNU Lesser General Public License
17 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 # ============================================================================
21 # Name        : update_feeds.py
22 # Author      : Yves Marcoz
23 # Version     : 0.6.1
24 # Description : Simple RSS Reader
25 # ============================================================================
26
27 from rss_sqlite import Listing
28 from config import Config
29 from updatedbus import get_lock, UpdateServerObject
30
31 import os
32 import gobject
33 import traceback
34
35 from jobmanager import JobManager
36 import mainthread
37
38 import debugging
39 debugging.init(dot_directory=".feedingit", program_name="update_feeds")
40
41 CONFIGDIR="/home/user/.feedingit/"
42 #DESKTOP_FILE = "/usr/share/applications/hildon-status-menu/feedingit_status.desktop"
43 dbug = False
44
45 from socket import setdefaulttimeout
46 timeout = 5
47 setdefaulttimeout(timeout)
48 del timeout
49
50 from updatedbus import UpdateServerObject
51
52 class FeedUpdate():
53     def __init__(self):
54         self.config = Config(self, CONFIGDIR+"config.ini")
55         self.dbusHandler = UpdateServerObject(self)
56         self.listing = Listing(self.config, CONFIGDIR)
57         self.done = False
58
59         jm = JobManager(True)
60         jm.stats_hook_register (self.job_manager_update,
61                                 run_in_main_thread=True)
62
63         self.dbusHandler.UpdateStarted()
64         for k in self.listing.getListOfFeeds():
65             self.listing.updateFeed (k)
66         
67     def job_manager_update(self, jm, old_stats, new_stats, updated_feed):
68         if not new_stats['jobs-queued'] and not new_stats['jobs-in-progress']:
69             self.dbusHandler.UpdateFinished()
70             self.dbusHandler.ArticleCountUpdated()
71             self.done = True
72             mainloop.quit()
73
74     def stopUpdate(self):
75         print "Stop update called."
76         JobManager().quit()
77
78 import dbus.mainloop.glib
79 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
80
81 gobject.threads_init()
82 mainthread.init()
83 mainloop = gobject.MainLoop()
84
85 app_lock = get_lock("app_lock")
86
87 if app_lock != None:
88     feed = FeedUpdate()
89     while not feed.done:
90         try:
91             mainloop.run()
92         except KeyboardInterrupt:
93             print "Interrupted.  Quitting."
94             JobManager().quit()
95     del app_lock
96 else:
97     logger.error("Update in progress")