When creating a closure, correctly capture any required local state.
authorNeal H. Walfield <neal@walfield.org>
Mon, 5 Sep 2011 21:57:16 +0000 (23:57 +0200)
committerNeal H. Walfield <neal@walfield.org>
Mon, 5 Sep 2011 21:57:16 +0000 (23:57 +0200)
src/rss_sqlite.py

index 444c918..98c414a 100644 (file)
@@ -223,8 +223,6 @@ class Feed:
     
             currentTime = 0
     
     
             currentTime = 0
     
-            have_woodchuck = mainthread.execute (wc().available)
-
             def wc_success():
                 try:
                     wc().stream_register (self.key, "", 6 * 60 * 60)
             def wc_success():
                 try:
                     wc().stream_register (self.key, "", 6 * 60 * 60)
@@ -255,7 +253,7 @@ class Feed:
             # this first.
             if http_status == 304:
                 logger.debug("%s: No changes to feed." % (self.key,))
             # this first.
             if http_status == 304:
                 logger.debug("%s: No changes to feed." % (self.key,))
-                mainthread.execute (wc_success, async=True)
+                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
                 success = True
             elif len(tmp["entries"])==0 and not tmp.version:
                 # An error occured fetching or parsing the feed.  (Version
@@ -266,8 +264,8 @@ class Feed:
                     % (url, str (tmp.version),
                        str (tmp.get ('bozo_exception', 'Unknown error'))))
                 logger.debug(tmp)
                     % (url, str (tmp.version),
                        str (tmp.get ('bozo_exception', 'Unknown error'))))
                 logger.debug(tmp)
-                if have_woodchuck:
-                    def e():
+                def register_stream_update_failed(http_status):
+                    def doit():
                         logger.debug("%s: stream update failed!" % self.key)
     
                         try:
                         logger.debug("%s: stream update failed!" % self.key)
     
                         try:
@@ -285,7 +283,12 @@ class Feed:
                         if 500 <= http_status and http_status < 600:
                             ec = woodchuck.TransferStatus.TransientNetwork
                         wc()[self.key].update_failed(ec)
                         if 500 <= http_status and http_status < 600:
                             ec = woodchuck.TransferStatus.TransientNetwork
                         wc()[self.key].update_failed(ec)
-                    mainthread.execute (e, async=True)
+                    return doit
+                if wc().available:
+                    mainthread.execute(
+                        register_stream_update_failed(
+                            http_status=http_status),
+                        async=True)
             else:
                currentTime = time.time()
                # The etag and modified value should only be updated if the content was not null
             else:
                currentTime = time.time()
                # The etag and modified value should only be updated if the content was not null
@@ -409,31 +412,49 @@ class Feed:
     
                    # Register the object with Woodchuck and mark it as
                    # downloaded.
     
                    # Register the object with Woodchuck and mark it as
                    # downloaded.
-                   if have_woodchuck:
-                       def e():
+                   def register_object_transferred(
+                           id, title, publication_time,
+                           sent, received, object_size):
+                       def doit():
+                           logger.debug("Registering transfer of object %s"
+                                        % title)
                            try:
                                obj = wc()[self.key].object_register(
                                    object_identifier=id,
                            try:
                                obj = wc()[self.key].object_register(
                                    object_identifier=id,
-                                   human_readable_name=tmpEntry["title"])
+                                   human_readable_name=title)
                            except woodchuck.ObjectExistsError:
                                obj = wc()[self.key][id]
                            else:
                            except woodchuck.ObjectExistsError:
                                obj = wc()[self.key][id]
                            else:
-                               # If the entry does not contain a publication
-                               # time, the attribute won't exist.
-                               pubtime = entry.get ('date_parsed', None)
-                               if pubtime:
-                                   obj.publication_time = time.mktime (pubtime)
-        
-                               received = (progress_handler.stats['received']
-                                           - received_base)
-                               sent = progress_handler.stats['sent'] - sent_base
-                               obj.transferred (
-                                   indicator=(woodchuck.Indicator.ApplicationVisual
-                                              |woodchuck.Indicator.StreamWide),
+                               obj.publication_time = publication_time
+                               obj.transferred(
+                                   indicator=(
+                                       woodchuck.Indicator.ApplicationVisual
+                                       |woodchuck.Indicator.StreamWide),
                                    transferred_down=received,
                                    transferred_up=sent,
                                    object_size=object_size)
                                    transferred_down=received,
                                    transferred_up=sent,
                                    object_size=object_size)
-                       mainthread.execute(e, async=True)
+                       return doit
+                   if wc().available:
+                       # If the entry does not contain a publication
+                       # time, the attribute won't exist.
+                       pubtime = entry.get('date_parsed', None)
+                       if pubtime:
+                           publication_time = time.mktime (pubtime)
+                       else:
+                           publication_time = None
+
+                       sent = progress_handler.stats['sent'] - sent_base
+                       received = (progress_handler.stats['received']
+                                   - received_base)
+
+                       mainthread.execute(
+                           register_object_transferred(
+                               id=id,
+                               title=tmpEntry["title"],
+                               publication_time=publication_time,
+                               sent=sent, received=received,
+                               object_size=object_size),
+                           async=True)
                self.db.commit()
 
                logger.debug (
                self.db.commit()
 
                logger.debug (
@@ -511,12 +532,13 @@ class Feed:
         self.db.execute("UPDATE feed SET read=1 WHERE id=?;", (id,) )
         self.db.commit()
 
         self.db.execute("UPDATE feed SET read=1 WHERE id=?;", (id,) )
         self.db.commit()
 
-        def e():
-            if wc().available():
-                try:
-                    wc()[self.key][id].used()
-                except KeyError:
-                    pass
+        def doit():
+            try:
+                wc()[self.key][id].used()
+            except KeyError:
+                pass
+        if wc().available():
+            mainthread.execute(doit, async=True)
 
     def setEntryUnread(self, id):
         self.db.execute("UPDATE feed SET read=0 WHERE id=?;", (id,) )
 
     def setEntryUnread(self, id):
         self.db.execute("UPDATE feed SET read=0 WHERE id=?;", (id,) )
@@ -670,15 +692,15 @@ class Feed:
         self.db.execute("DELETE FROM images WHERE id=?;", (id,) )
         self.db.commit()
 
         self.db.execute("DELETE FROM images WHERE id=?;", (id,) )
         self.db.commit()
 
-        def e():
-            if wc().available():
-                try:
-                    wc()[self.key][id].files_deleted (
-                        woodchuck.DeletionResponse.Deleted)
-                    del wc()[self.key][id]
-                except KeyError:
-                    pass
-        mainthread.execute (e, async=True)
+        def doit():
+            try:
+                wc()[self.key][id].files_deleted (
+                    woodchuck.DeletionResponse.Deleted)
+                del wc()[self.key][id]
+            except KeyError:
+                pass
+        if wc().available():
+            mainthread.execute (doit, async=True)
  
 class ArchivedArticles(Feed):    
     def addArchivedArticle(self, title, link, date, configdir):
  
 class ArchivedArticles(Feed):    
     def addArchivedArticle(self, title, link, date, configdir):