changeset 121:510a5d00e98a backend

re-enabled AddFeed - does not work yet
author Dirk Olmes <dirk@xanthippe.ping.de>
date Sun, 21 Aug 2011 04:17:13 +0200
parents e830fa1cc7a2
children f5afe0c1f4d2
files AddFeed.py FeedUpdater.py MainWindow.py backend/sqlalchemy/FeedUpdater.py backend/sqlalchemy/SqlAlchemyBackend.py
diffstat 5 files changed, 112 insertions(+), 107 deletions(-) [+]
line wrap: on
line diff
--- a/AddFeed.py	Sun Aug 21 04:04:37 2011 +0200
+++ b/AddFeed.py	Sun Aug 21 04:17:13 2011 +0200
@@ -1,36 +1,34 @@
 
 from PyQt4 import QtGui
 from Ui_AddFeed import Ui_AddFeed
-import FeedUpdater
 import logging
 
 class AddFeed(QtGui.QDialog):
-    def __init__(self, session):
+    def __init__(self, backend=None):
         QtGui.QWidget.__init__(self, None)
-        self.session = session
+        self.backend = backend
         self.exception = None
         self.ui = Ui_AddFeed()
         self.ui.setupUi(self)
         self.ui.url.setFocus()
-    
+
     def accept(self):
         try:
             self.createFeed()
-            self.session.commit()
+            self.session.commit() # TODO this should be the responsibility of the FeedUpdater
         except AttributeError as ae:
             logging.getLogger("AddFeed").info(ae)
-            
+
             self.exception = ae
-            self.session.rollback()
+            self.session.rollback() # TODO this should be the responsibility of the FeedUpdater
         QtGui.QDialog.accept(self)
 
     def createFeed(self):
         url = self.getUrl()
-        # TODO get status from feedUpdater and display in status area of the main window
-        FeedUpdater.createNewFeed(url, self.session)
-    
+        # TODO get status and display in status area of the main window
+        self.backend.feedUpdater().createNewFeed(url)
+
     def getUrl(self):
         text = self.ui.url.text()
         # the content of a QLineEdit is a QString, convert it to a Python string
         return str(text)
-    
\ No newline at end of file
--- a/FeedUpdater.py	Sun Aug 21 04:04:37 2011 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,83 +0,0 @@
-
-from datetime import datetime
-from Feed import Feed
-from FeedEntry import FeedEntry
-import feedparser
-import logging
-
-STATUS_ERROR = 400
-log = logging.getLogger("FeedUpdater")
-
-def updateAllFeeds(session):
-    allFeeds = findFeedsToUpdate(session)
-    for feed in allFeeds:
-        try:
-            FeedUpdater(session, feed).update()
-        except FeedUpdateException, fue:
-            log.warn("problems while updating feed " + feed.rss_url + ": " + str(fue))
-        session.commit()
-
-def findFeedsToUpdate(session):
-    return session.query(Feed).filter(Feed.next_update < datetime.now())
-
-def createNewFeed(url, session):
-    # when updating to python3 see http://code.google.com/p/feedparser/issues/detail?id=260
-    result = feedparser.parse(url)
-    if result.has_key("title"):
-        title = result["feed"].title
-    else:
-        title = url
-    newFeed = Feed(title, url)
-    session.add(newFeed)
-
-    FeedUpdater(session, newFeed).update()
-
-def normalize(entry):
-    if not hasattr(entry, "id"):
-        entry.id = entry.link
-    if not hasattr(entry, "updated_parsed"):
-        entry.updated_parsed = datetime.today()
-    else:
-        entry.updated_parsed = datetime(*entry.updated_parsed[:6])
-    if not hasattr(entry, "summary"):
-        if hasattr(entry, "content"):
-            entry.summary = entry.content[0].value
-        else:
-            entry.summary = ""
-
-class FeedUpdater(object):
-    def __init__(self, session, feed):
-        self.session = session
-        self.feed = feed
-
-    def update(self):
-        log.info("updating " + self.feed.rss_url)
-        result = self.getFeed()
-        for entry in result.entries:
-            self.processEntry(entry)
-        self.feed.incrementNextUpdateDate()
-
-    def getFeed(self):
-        result = feedparser.parse(self.feed.rss_url)
-        # bozo flags if a feed is well-formed.
-#        if result["bozo"] > 0:
-#            raise FeedUpdateException()
-        status = result["status"]
-        if status >= STATUS_ERROR:
-            raise FeedUpdateException("HTTP status " + str(status))
-        return result
-
-    def processEntry(self, entry):
-        normalize(entry)
-        feedEntry = FeedEntry.findById(entry.id, self.session)
-        if feedEntry is None:
-            self.createFeedEntry(entry)
-
-    def createFeedEntry(self, entry):
-        new = FeedEntry.create(entry)
-        new.feed = self.feed
-        self.session.add(new)
-        log.info("new feed entry: " + entry.title)
-
-class FeedUpdateException(Exception):
-    pass
--- a/MainWindow.py	Sun Aug 21 04:04:37 2011 +0200
+++ b/MainWindow.py	Sun Aug 21 04:17:13 2011 +0200
@@ -1,5 +1,5 @@
 
-#from AddFeed import AddFeed
+from AddFeed import AddFeed
 from DisplayModel import DisplayModel
 from FeedEntryItemDelegate import FeedEntryItemDelegate
 from FeedEntryTableModel import FeedEntryTableModel
@@ -142,18 +142,17 @@
         self.ui.feedEntryList.doItemsLayout()
 
     def addFeed(self):
-        pass
-#        addFeed = AddFeed(self.session)
-#        success = addFeed.exec_()
-#        if not success:
-#            return
-#
-#        if addFeed.exception is not None:
-#            message = "Error while adding feed: " + str(addFeed.exception)
-#            self._updateStatusBar(message)
-#        else:
-#            self.session.commit()
-#            self.updateFeedList()
+        addFeed = AddFeed(self.backend)
+        success = addFeed.exec_()
+        if not success:
+            return
+
+        if addFeed.exception is not None:
+            message = "Error while adding feed: " + str(addFeed.exception)
+            self._updateStatusBar(message)
+        else:
+            self.session.commit()
+            self.updateFeedList()
 
     def deleteFeed(self):
         try:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/backend/sqlalchemy/FeedUpdater.py	Sun Aug 21 04:17:13 2011 +0200
@@ -0,0 +1,84 @@
+
+from datetime import datetime
+from Feed import Feed
+from FeedEntry import FeedEntry
+import feedparser
+import logging
+
+STATUS_ERROR = 400
+log = logging.getLogger("FeedUpdater")
+
+def updateAllFeeds(session):
+    allFeeds = findFeedsToUpdate(session)
+    for feed in allFeeds:
+        try:
+            FeedUpdater(session, feed).update()
+        except FeedUpdateException, fue:
+            log.warn("problems while updating feed " + feed.rss_url + ": " + str(fue))
+        session.commit()
+
+def findFeedsToUpdate(session):
+    return session.query(Feed).filter(Feed.next_update < datetime.now())
+
+def normalize(entry):
+    if not hasattr(entry, "id"):
+        entry.id = entry.link
+    if not hasattr(entry, "updated_parsed"):
+        entry.updated_parsed = datetime.today()
+    else:
+        entry.updated_parsed = datetime(*entry.updated_parsed[:6])
+    if not hasattr(entry, "summary"):
+        if hasattr(entry, "content"):
+            entry.summary = entry.content[0].value
+        else:
+            entry.summary = ""
+
+class FeedUpdater(object):
+    def __init__(self, session, feed):
+        self.session = session
+        self.feed = feed
+
+    # TODO this is a HACK! creating new instances from itself is bad but required due to the storage of the session.
+    def createNewFeed(self, url):
+        # when updating to python3 see http://code.google.com/p/feedparser/issues/detail?id=260
+        result = feedparser.parse(url)
+        if result.has_key("title"):
+            title = result["feed"].title
+        else:
+            title = url
+        newFeed = Feed(title, url)
+        self.session.add(newFeed)
+
+        FeedUpdater(self.session, newFeed).update()
+
+    def update(self):
+        log.info("updating " + self.feed.rss_url)
+        result = self.getFeed()
+        for entry in result.entries:
+            self.processEntry(entry)
+        self.feed.incrementNextUpdateDate()
+
+    def getFeed(self):
+        result = feedparser.parse(self.feed.rss_url)
+        # bozo flags if a feed is well-formed.
+#        if result["bozo"] > 0:
+#            raise FeedUpdateException()
+        status = result["status"]
+        if status >= STATUS_ERROR:
+            raise FeedUpdateException("HTTP status " + str(status))
+        return result
+
+    def processEntry(self, entry):
+        normalize(entry)
+        feedEntry = FeedEntry.findById(entry.id, self.session)
+        if feedEntry is None:
+            self.createFeedEntry(entry)
+
+    def createFeedEntry(self, entry):
+        new = FeedEntry.create(entry)
+        new.feed = self.feed
+        self.session.add(new)
+        log.info("new feed entry: " + entry.title)
+
+class FeedUpdateException(Exception):
+    pass
--- a/backend/sqlalchemy/SqlAlchemyBackend.py	Sun Aug 21 04:04:37 2011 +0200
+++ b/backend/sqlalchemy/SqlAlchemyBackend.py	Sun Aug 21 04:17:13 2011 +0200
@@ -1,5 +1,6 @@
 
 from Preferences import Preferences
+from FeedUpdater import FeedUpdater
 import Database
 import logging
 import util
@@ -14,6 +15,7 @@
         self._initLogging()
         self.session = Database.createSession()
         self.prefs = Preferences(self.session)
+        self.updater = None
 
     def _initLogging(self):
         logging.getLogger("sqlalchemy.orm").setLevel(logging.WARN)
@@ -26,6 +28,11 @@
     def preferences(self):
         return self.prefs
 
+    def feedUpdater(self):
+        if self.updater is None:
+            self.updater = FeedUpdater(self.session)
+        return self.updater
+
     def getFeeds(self):
         return FeedList.getFeeds(self.session)