view backend/sqlalchemy/SqlAlchemyBackend.py @ 122:f5afe0c1f4d2 backend

move more operations to the backend
author Dirk Olmes <dirk@xanthippe.ping.de>
date Sun, 21 Aug 2011 18:39:02 +0200
parents 510a5d00e98a
children 862760b161b4
line wrap: on
line source


from Preferences import Preferences
from FeedUpdater import FeedUpdater
import Database
import logging
import util
import FeedList

class SqlAlchemyBackend(object):
    '''
    Backend that uses sqlalchemy for persistence
    '''

    def __init__(self):
        self._initLogging()
        self.session = Database.createSession()
        self.prefs = Preferences(self.session)
        self.updater = None

    def _initLogging(self):
        logging.getLogger("sqlalchemy.orm").setLevel(logging.WARN)

        sqlalchemyLogLevel = logging.ERROR
        if util.databaseLoggingEnabled():
            sqlalchemyLogLevel = logging.INFO
        logging.getLogger("sqlalchemy").setLevel(sqlalchemyLogLevel)

    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)

    def toggleRead(self, feedEntry):
        feedEntry.toggleRead()
        self.session.commit()

    def markAllEntriesRead(self, feed):
        feed.markAllEntriesRead()
        self.session.commit()

    def dispose(self):
        # save all uncommitted state, just in case
        self.session.commit()