view backend/sqlalchemy/Preferences.py @ 160:86f828096aaf

Do not fetch and parse the feed twice when creating a new one. Pass the parsed info into the update method instead to reuse.
author dirk
date Mon, 29 Aug 2011 03:07:50 +0200
parents e830fa1cc7a2
children bb3c851b18b1
line wrap: on
line source


from Preference import Preference

DAYS_TO_KEEP_FEED_ENTRIES = "DAYS_TO_KEEP_FEED_ENTRIES"
HIDE_READ_ENTRIES = "HIDE_READ_FEED_ENTRIES"
PROXY_HOST = "PROXY_HOST"
PROXY_PORT = "PROXY_PORT"
SHOW_ONLY_UNREAD_FEEDS = "SHOW_ONLY_UNREAD_FEEDS"
START_MAXIMIZED = "START_MAXIMIZED"

def str2bool(string):
    return string.lower() in ["yes", "true", "t", "1"]

def bool2str(bool):
    if bool:
        return "True"
    else:
        return "False"

class Preferences(object):
    def __init__(self, session):
        self.session = session
        self.cache = {}

    def _cachedPreference(self, key, defaultValue=None, addIfMissing=True):
        if self.cache.has_key(key):
            return self.cache[key]
        else:
            pref = Preference.forKey(key, self.session)
            if pref is not None:
                self.cache[key] = pref
            elif pref is None and addIfMissing:
                pref = Preference(key, str(defaultValue))
                self.session.add(pref)
                self.cache[key] = pref
            return pref

    def commit(self):
        self.session.commit()

    def rollback(self):
        self.session.rollback()

    def startMaximized(self):
        pref = self._cachedPreference(START_MAXIMIZED, False)
        return str2bool(pref.value)

    def setStartMaximized(self, flag):
        pref = self._cachedPreference(START_MAXIMIZED)
        pref.value = bool2str(flag)

    def hideReadFeedEntries(self):
        pref = self._cachedPreference(HIDE_READ_ENTRIES, False)
        return str2bool(pref.value)

    def setHideReadFeedEntries(self, flag):
        pref = self._cachedPreference(HIDE_READ_ENTRIES)
        pref.value = bool2str(flag)

    def showOnlyUnreadFeeds(self):
        pref = self._cachedPreference(SHOW_ONLY_UNREAD_FEEDS, False)
        return str2bool(pref.value)

    def setShowOnlyUnreadFeeds(self, flag):
        pref = self._cachedPreference(SHOW_ONLY_UNREAD_FEEDS)
        pref.value = bool2str(flag)

    def proxyHost(self):
        pref = self._cachedPreference(PROXY_HOST)
        return pref.value

    def setProxyHost(self, hostname):
        if hostname is None:
            pref = self._cachedPreference(PROXY_HOST, addIfMissing=False)
            if pref is not None:
                self.session.delete(pref)
                del(self.cache[PROXY_HOST])
        else:
            pref = self._cachedPreference(PROXY_HOST)
            pref.value = str(hostname)

    def proxyPort(self):
        pref = self._cachedPreference(PROXY_PORT, 3128)
        return int(pref.value)

    def setProxyPort(self, port):
        if port is None:
            pref = self._cachedPreference(PROXY_PORT, addIfMissing=False)
            if pref is not None:
                self.session.delete(pref)
                del(self.cache[PROXY_PORT])
        else:
            pref = self._cachedPreference(PROXY_PORT)
            pref.value = str(port)

    def isProxyConfigured(self):
        pref = self._cachedPreference(PROXY_HOST, addIfMissing=False)
        return pref is not None

    def daysToKeepFeedEntries(self):
        pref = self._cachedPreference(DAYS_TO_KEEP_FEED_ENTRIES, 90, addIfMissing=True)
        return int(pref.value)

    def setDaysToKeepFeedEntries(self, dayString):
        pref = self._cachedPreference(DAYS_TO_KEEP_FEED_ENTRIES)
        pref.value = dayString