Mercurial > hg > Feedworm
diff backend/sqlalchemy/Preferences.py @ 119:04a730f9d07d backend
move all sqlalchemy related classes to the respective sub-package. use a backend to abstract from access to the data
author | Dirk Olmes <dirk@xanthippe.ping.de> |
---|---|
date | Sun, 21 Aug 2011 03:55:16 +0200 |
parents | Preferences.py@c17a224bc251 |
children | e830fa1cc7a2 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/backend/sqlalchemy/Preferences.py Sun Aug 21 03:55:16 2011 +0200 @@ -0,0 +1,100 @@ + +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 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