Replace use of print with Python's logging infrastructure.
authorNeal H. Walfield <neal@walfield.org>
Mon, 22 Aug 2011 19:28:36 +0000 (21:28 +0200)
committerNeal H. Walfield <neal@walfield.org>
Tue, 23 Aug 2011 16:10:49 +0000 (18:10 +0200)
12 files changed:
Makefile
src/FeedingIt-Web.py
src/FeedingIt.py
src/config.py
src/httpprogresshandler.py
src/jobmanager.py
src/mainthread.py
src/opml.py
src/rss.py
src/rss_sqlite.py
src/update_feeds.py
src/wc.py

index f322164..f79d67f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -35,6 +35,7 @@ install:
        install src/jobmanager.py ${DESTDIR}/opt/FeedingIt
        install src/httpprogresshandler.py ${DESTDIR}/opt/FeedingIt
        install src/wc.py ${DESTDIR}/opt/FeedingIt
+       install src/debugging.py ${DESTDIR}/opt/FeedingIt
        install -d ${DESTDIR}/usr/share/applications/hildon
        install src/FeedingIt.desktop ${DESTDIR}/usr/share/applications/hildon
        install -d ${DESTDIR}/usr/share/icons/hicolor/48x48/apps/
index af3395f..3ea91c5 100644 (file)
@@ -11,6 +11,9 @@ from threading import Thread
 from os.path import isfile, isdir, exists
 from os import mkdir, remove, stat, environ
 
+import logging
+logger = logging.getLogger(__name__)
+
 import debugging
 debugging.init(dot_directory=".feedingit", program_name="feedingit-web")
 
@@ -271,7 +274,7 @@ if not isdir(CONFIGDIR):
     try:
         mkdir(CONFIGDIR)
     except:
-        print "Error: Can't create configuration directory"
+        logger.error("Error: Can't create configuration directory")
         from sys import exit
         exit(1)
 
index a808c26..3fcf18a 100644 (file)
@@ -47,6 +47,8 @@ from config import Config
 from cgi import escape
 import weakref
 import debugging
+import logging
+logger = logging.getLogger(__name__)
 
 from rss_sqlite import Listing
 from opml import GetOpmlData, ExportOpmlData
@@ -1041,8 +1043,8 @@ class FeedingIt:
         try:
             self.orientation = FremantleRotation(__appname__, main_window=self.window, app=self)
             self.orientation.set_mode(self.config.getOrientation())
-        except:
-            print "Could not start rotation manager"
+        except Exception, e:
+            logger.warn("Could not start rotation manager: %s" % str(e))
         
         menu = hildon.AppMenu()
         # Create a button and add it to the menu
@@ -1132,7 +1134,8 @@ class FeedingIt:
             self.updateDbusHandler.ArticleCountUpdated()
 
         if not self.had_interaction and 'DBUS_STARTER_ADDRESS' in environ:
-            print "Update complete. No interaction, started by dbus: quitting."
+            logger.info(
+                "Update complete. No interaction, started by dbus: quitting.")
             self.quit()
     def stop_running_update(self, button):
         self.stopButton.set_sensitive(False)
@@ -1498,7 +1501,7 @@ if __name__ == "__main__":
         try:
             mkdir(CONFIGDIR)
         except:
-            print "Error: Can't create configuration directory"
+            logger.error("Error: Can't create configuration directory")
             from sys import exit
             exit(1)
     app = FeedingIt()
index 65fbddd..9f7ec81 100644 (file)
@@ -33,6 +33,8 @@ from ConfigParser import RawConfigParser
 from gconf import client_get_default
 from urllib2 import ProxyHandler
 from mainthread import mainthread
+import logging
+logger = logging.getLogger(__name__)
 
 VERSION = "52"
 
@@ -160,7 +162,7 @@ class Config():
         # When the dialog is closed without hitting
         # the "Save" button, restore the configuration
         if self.do_restore_backup:
-            print 'Restoring configuration'
+            logger.debug('Restoring configuration')
             self.config = self.config_backup
 
         self.saveConfig()
index 78eb39c..7c4fe63 100644 (file)
@@ -18,6 +18,8 @@
 import urllib2 
 import httplib
 import time
+import logging
+logger = logging.getLogger(__name__)
 
 class ProgressSocket(object):
     """
@@ -28,7 +30,8 @@ class ProgressSocket(object):
         self.connection = connection
 
     def __getattribute__(self, attr):
-        # print "%s.__getattribute__(%s)" % (self.__class__.__name__, attr)
+        # logger.debug("%s.__getattribute__(%s)"
+        #              % (self.__class__.__name__, attr))
 
         def send(data):
             # 100k at a time.
@@ -124,15 +127,16 @@ if __name__ == '__main__':
         cstats = connection.stats
         ostats = connection.opener.stats
 
-        print (("%s: connection: %d sent, %d received: %d kb/s; "
-                + "opener: %d sent, %d received, %d kb/s")
-               % (req,
-                  cstats['sent'], cstats['received'],
-                  ((cstats['sent'] + cstats['received'])
-                   / (time.time() - cstats['started']) / 1024),
-                  ostats['sent'], ostats['received'],
-                  ((ostats['sent'] + ostats['received'])
-                   / (time.time() - ostats['started']) / 1024)))
+        print(
+            ("%s: connection: %d sent, %d received: %d kb/s; "
+             + "opener: %d sent, %d received, %d kb/s")
+            % (req,
+               cstats['sent'], cstats['received'],
+               ((cstats['sent'] + cstats['received'])
+                / (time.time() - cstats['started']) / 1024),
+               ostats['sent'], ostats['received'],
+               ((ostats['sent'] + ostats['received'])
+                / (time.time() - ostats['started']) / 1024)))
 
     opener = urllib2.build_opener(HTTPProgressHandler(callback))
 
index 42c7441..8c20fe5 100644 (file)
@@ -21,11 +21,12 @@ import traceback
 import heapq
 import sys
 import mainthread
+import logging
+logger = logging.getLogger(__name__)
 
 def debug(*args):
     if False:
-        sys.stdout.write(*args)
-        sys.stdout.write("\n")
+        logger.debug(' '.join(args))
 
 # The default priority.  Like nice(), a smaller numeric priority
 # corresponds to a higher priority class.
index a85cf5a..6d8246a 100644 (file)
@@ -17,6 +17,8 @@
 
 import threading
 import traceback
+import logging
+logger = logging.getLogger(__name__)
 
 _run_in_main_thread = None
 _main_thread = None
@@ -63,8 +65,8 @@ def execute(func, *args, **kwargs):
             try:
                 func (*args, **kwargs)
             except:
-                print ("mainthread.execute: Executing %s: %s"
-                       % (func, traceback.format_exc ()))
+                logger.debug("mainthread.execute: Executing %s: %s"
+                             % (func, traceback.format_exc ()))
             return
         else:
             return func (*args, **kwargs)
@@ -86,8 +88,8 @@ def execute(func, *args, **kwargs):
             try:
                 result['result'] = func (*args, **kwargs)
             except:
-                print ("mainthread.execute: Executing %s: %s"
-                       % (func, traceback.format_exc ()))
+                logger.debug("mainthread.execute: Executing %s: %s"
+                             % (func, traceback.format_exc ()))
 
             if not async:
                 cond.acquire ()
index 3b22459..914fd38 100644 (file)
@@ -31,6 +31,8 @@ import gobject
 import time
 from os.path import isfile, dirname
 import gobject
+import logging
+logger = logging.getLogger(__name__)
 
 class ExportOpmlData():
     def __init__(self, parent, listing):
@@ -51,7 +53,7 @@ class ExportOpmlData():
         dialog.hide()
         if response == gtk.RESPONSE_OK:
                 filename = dialog.get_filename()
-                print filename
+                logger.debug("ExportOpmlData: %s" % filename)
             #try:
 
                 cont = True
@@ -273,7 +275,7 @@ class OpmlDialog(gtk.Dialog):
 
 def showOpmlData(widget, parent, button):
     dialog = GetOpmlData(parent)
-    print dialog.getData()
+    logger.debug("showOpmlData: %s" % dialog.getData())
     #dialog.destroy()
 
 if __name__ == "__main__":
index e82a4b5..66149d4 100644 (file)
@@ -33,6 +33,8 @@ import time
 import urllib2
 from BeautifulSoup import BeautifulSoup
 from urlparse import urljoin
+import logging
+logger = logging.getLogger(__name__)
 
 #CONFIGDIR="/home/user/.feedingit/"
 
@@ -88,7 +90,7 @@ class Feed:
                 f.close()
                 outf.close()
             except:
-                print "Could not download " + url
+                logger.error("Could not download " + url)
         else:
             #open(filename,"a").close()  # "Touch" the file
             file = open(filename,"a")
@@ -195,7 +197,7 @@ class Feed:
                             img['src']=filename
                             tmpEntry["images"].append(filename)
                           except:
-                              print "Error downloading image %s" % img
+                              logger.error("Error downloading image %s" % img)
                    tmpEntry["contentLink"] = configdir+self.uniqueId+".d/"+id+".html"
                    file = open(tmpEntry["contentLink"], "w")
                    file.write(soup.prettify())
@@ -234,7 +236,7 @@ class Feed:
                         tmpEntries[entryId] = self.entries[entryId]
                         tmpIds.append(entryId)
                     except:
-                        print "Error purging old articles %s" % entryId
+                        logger.error("Error purging old articles %s" % entryId)
                         self.removeEntry(entryId)
 
            self.entries = tmpEntries
@@ -280,7 +282,7 @@ class Feed:
                         #
                     except OSError:
                         #
-                        print 'Could not remove', file
+                        logger.error('Could not remove %s' % file)
            
 
     def extractContent(self, entry):
@@ -399,20 +401,21 @@ class Feed:
                 try:
                     remove(entry["contentLink"])  #os.remove
                 except:
-                    print "File not found for deletion: %s" % entry["contentLink"]
+                    logger.error("File not found for deletion: %s"
+                                 % entry["contentLink"])
             del self.entries[id]
         else:
-            print "Entries has no %s key" % id
+            logger.error("Entries has no %s key" % id)
         if id in self.ids:
             self.ids.remove(id)
         else:
-            print "Ids has no %s key" % id
+            logger.error("Ids has no %s key" % id)
         if self.readItems.has_key(id):
             if self.readItems[id]==False:
                 self.countUnread = self.countUnread - 1
             del self.readItems[id]
         else:
-            print "ReadItems has no %s key" % id
+            logger.error("ReadItems has no %s key" % id)
         #except:
         #    print "Error removing entry %s" %id
     
@@ -608,7 +611,7 @@ class Listing:
         except:
             # If the feed file gets corrupted, we need to reset the feed.
             import traceback
-            traceback.print_exc()
+            logger.error("getFeed: %s" % traceback.format_exc())
             import dbus
             bus = dbus.SessionBus()
             remote_object = bus.get_object("org.freedesktop.Notifications", # Connection name
index ecc2ed0..e989a1a 100644 (file)
@@ -46,6 +46,8 @@ import mainthread
 from httpprogresshandler import HTTPProgressHandler
 import random
 import sys
+import logging
+logger = logging.getLogger(__name__)
 
 def getId(string):
     return md5.new(string).hexdigest()
@@ -106,15 +108,14 @@ class Feed:
                 f.close()
                 outf.close()
             except (urllib2.HTTPError, urllib2.URLError, IOError), exception:
-                print ("Could not download image %s: %s"
-                       % (abs_url, str (exception)))
+                logger.info("Could not download image %s: %s"
+                            % (abs_url, str (exception)))
                 return None
             except:
                 exception = sys.exc_info()[0]
 
-                print "Downloading image: %s" % abs_url
-                traceback.print_exc()
-
+                logger.info("Downloading image %s: %s" %
+                            (abs_url, traceback.format_exc()))
                 try:
                     remove(filename)
                 except OSError:
@@ -187,8 +188,9 @@ class Feed:
                         new_objects=len (tmp.entries),
                         objects_inline=len (tmp.entries))
                 except KeyError:
-                    print "Failed to register update with woodchuck!"
-                    pass
+                    logger.warn(
+                        "Failed to register update of %s with woodchuck!"
+                        % (self.key))
     
             http_status = tmp.get ('status', 200)
     
@@ -199,20 +201,21 @@ class Feed:
             # parse fails.  But really, everything went great!  Check for
             # this first.
             if http_status == 304:
-                print "%s: No changes to feed." % (self.key,)
+                logger.debug("%s: No changes to feed." % (self.key,))
                 mainthread.execute (wc_success, async=True)
                 success = True
             elif len(tmp["entries"])==0 and not tmp.version:
                 # An error occured fetching or parsing the feed.  (Version
                 # will be either None if e.g. the connection timed our or
                 # '' if the data is not a proper feed)
-                print ("Error fetching %s: version is: %s: error: %s"
-                       % (url, str (tmp.version),
-                          str (tmp.get ('bozo_exception', 'Unknown error'))))
-                print tmp
+                logger.error(
+                    "Error fetching %s: version is: %s: error: %s"
+                    % (url, str (tmp.version),
+                       str (tmp.get ('bozo_exception', 'Unknown error'))))
+                logger.debug(tmp)
                 if have_woodchuck:
                     def e():
-                        print "%s: stream update failed!" % self.key
+                        logger.debug("%s: stream update failed!" % self.key)
     
                         try:
                             # It's not easy to get the feed's title from here.
@@ -251,8 +254,8 @@ class Feed:
                    outf.close()
                    del data
                except (urllib2.HTTPError, urllib2.URLError), exception:
-                   print ("Could not download favicon %s: %s"
-                          % (abs_url, str (exception)))
+                   logger.debug("Could not download favicon %s: %s"
+                                % (abs_url, str (exception)))
     
                self.serial_execution_lock.acquire ()
                have_serial_execution_lock = True
@@ -317,9 +320,8 @@ class Feed:
                                 try:
                                     object_size += os.path.getsize (filename)
                                 except os.error, exception:
-                                    print ("Error getting size of %s: %s"
-                                           % (filename, exception))
-                                    pass
+                                    logger.error ("Error getting size of %s: %s"
+                                                  % (filename, exception))
                        self.serial_execution_lock.acquire ()
                        have_serial_execution_lock = True
     
@@ -379,11 +381,12 @@ class Feed:
                        mainthread.execute(e, async=True)
                self.db.commit()
 
-               print ("%s: Update successful: transferred: %d/%d; objects: %d)"
-                      % (self.key,
-                         progress_handler.stats['sent'],
-                         progress_handler.stats['received'],
-                         len (tmp.entries)))
+               logger.debug (
+                   "%s: Update successful: transferred: %d/%d; objects: %d)"
+                   % (self.key,
+                      progress_handler.stats['sent'],
+                      progress_handler.stats['received'],
+                      len (tmp.entries)))
                mainthread.execute (wc_success, async=True)
                success = True
 
@@ -415,13 +418,13 @@ class Feed:
                         #
                     except OSError, exception:
                         #
-                        print 'Could not remove %s: %s' % (file, str (exception))
-            print ("updated %s: %fs in download, %fs in processing"
-                   % (self.key, download_duration,
-                      time.time () - process_start))
+                        logger.error('Could not remove %s: %s'
+                                     % (file, str (exception)))
+            logger.debug("updated %s: %fs in download, %fs in processing"
+                         % (self.key, download_duration,
+                            time.time () - process_start))
         except:
-            print "Updating %s: %s" % (self.key, sys.exc_info()[0])
-            traceback.print_exc()
+            logger.error("Updating %s: %s" % (self.key, traceback.format_exc()))
         finally:
             self.db.commit ()
 
@@ -436,9 +439,9 @@ class Feed:
                 rows = self.db.execute("SELECT MAX(date) FROM feed;")
                 for row in rows:
                     updateTime=row[0]
-            except:
-                print "Fetching update time."
-                traceback.print_exc()
+            except Exception, e:
+                logger.error("Fetching update time: %s: %s"
+                             % (str(e), traceback.format_exc()))
             finally:
                 if not success:
                     etag = None
@@ -608,7 +611,7 @@ class Feed:
             try:
                 remove(contentLink)
             except OSError, exception:
-                print "Deleting %s: %s" % (contentLink, str (exception))
+                logger.error("Deleting %s: %s" % (contentLink, str (exception)))
         self.db.execute("DELETE FROM feed WHERE id=?;", (id,) )
         self.db.execute("DELETE FROM images WHERE id=?;", (id,) )
         self.db.commit()
@@ -732,8 +735,9 @@ class Listing:
                 # XXX: We should also check whether the list of
                 # articles/objects in each feed/stream is up to date.
                 if key not in stream_ids:
-                    print ("Registering previously unknown channel: %s (%s)"
-                           % (key, title,))
+                    logger.debug(
+                        "Registering previously unknown channel: %s (%s)"
+                        % (key, title,))
                     # Use a default refresh interval of 6 hours.
                     wc().stream_register (key, title, 6 * 60 * 60)
                 else:
@@ -745,7 +749,7 @@ class Listing:
 
             # Unregister any streams that are no longer subscribed to.
             for id in stream_ids:
-                print ("Unregistering %s" % (id,))
+                logger.debug("Unregistering %s" % (id,))
                 w.stream_unregister (id)
 
     def importOldFormatFeeds(self):
@@ -785,7 +789,8 @@ class Listing:
                             pass
                 self.updateUnread(id)
             except:
-                traceback.print_exc()
+                logger.error("importOldFormatFeeds: %s"
+                             % (traceback.format_exc(),))
         remove(self.configdir+"feeds.pickle")
                 
         
@@ -861,8 +866,7 @@ class Listing:
             try:
                 wc()[key].human_readable_name = title
             except KeyError:
-                print "Feed %s (%s) unknown." % (key, title)
-                pass
+                logger.debug("Feed %s (%s) unknown." % (key, title))
         
     def getFeedUpdateTime(self, key):
         return time.ctime(self.db.execute("SELECT updateTime FROM feeds WHERE id=?;", (key,)).fetchone()[0])
@@ -984,7 +988,7 @@ class Listing:
             try:
                 del wc()[key]
             except KeyError:
-                print "Removing unregistered feed %s failed" % (key,)
+                logger.debug("Removing unregistered feed %s failed" % (key,))
 
         rank = self.db.execute("SELECT rank FROM feeds WHERE id=?;", (key,) ).fetchone()[0]
         self.db.execute("DELETE FROM feeds WHERE id=?;", (key, ))
index 89d208b..8159498 100644 (file)
@@ -35,6 +35,8 @@ import traceback
 from jobmanager import JobManager
 import mainthread
 
+import logging
+logger = logging.getLogger(__name__)
 import debugging
 debugging.init(dot_directory=".feedingit", program_name="update_feeds")
 
@@ -72,7 +74,7 @@ class FeedUpdate():
             mainloop.quit()
 
     def stopUpdate(self):
-        print "Stop update called."
+        logger.info("Stop update called.")
         JobManager().quit()
 
 import dbus.mainloop.glib
@@ -90,7 +92,7 @@ if app_lock != None:
         try:
             mainloop.run()
         except KeyboardInterrupt:
-            print "Interrupted.  Quitting."
+            logger.error("Interrupted.  Quitting.")
             JobManager().quit()
     del app_lock
 else:
index 27787bc..d99eae6 100644 (file)
--- a/src/wc.py
+++ b/src/wc.py
@@ -13,6 +13,8 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import logging
+logger = logging.getLogger(__name__)
 import traceback
 
 # Don't fail if the Woodchuck modules are not available.  Just disable
@@ -25,8 +27,9 @@ try:
     from pywoodchuck import PyWoodchuck
     from pywoodchuck import woodchuck
 except ImportError, exception:
-    print ("Unable to load Woodchuck modules: disabling Woodchuck support: %s"
-           % traceback.format_exc ())
+    logger.info(
+        "Unable to load Woodchuck modules: disabling Woodchuck support: %s"
+        % traceback.format_exc ())
     woodchuck_imported = False
     class PyWoodchuck (object):
         def available(self):
@@ -44,16 +47,16 @@ class mywoodchuck (PyWoodchuck):
 
     # Woodchuck upcalls.
     def stream_update_cb(self, stream):
-        print ("stream update called on %s (%s)"
-               % (stream.human_readable_name, stream.identifier,))
+        logger.debug("stream update called on %s (%s)"
+                     % (stream.human_readable_name, stream.identifier,))
 
         # Make sure no one else is concurrently updating this
         # feed.
         try:
             self.listing.updateFeed(stream.identifier)
         except:
-            print ("Updating %s: %s"
-                   % (stream.identifier, traceback.format_exc ()))
+            logger.debug("Updating %s: %s"
+                         % (stream.identifier, traceback.format_exc ()))
 
     def object_transfer_cb(self, stream, object,
                            version, filename, quality):
@@ -69,9 +72,9 @@ def wc_init(listing):
     _w = mywoodchuck (listing, "FeedingIt", "org.maemo.feedingit")
 
     if not woodchuck_imported or not _w.available ():
-        print "Unable to contact Woodchuck server."
+        logger.info("Unable to contact Woodchuck server.")
     else:
-        print "Woodchuck appears to be available."
+        logger.debug("Woodchuck appears to be available.")
 
 def wc():
     """Connect to the woodchuck server and initialize any state."""