comparison 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
comparison
equal deleted inserted replaced
118:0e73adb2dec4 119:04a730f9d07d
1
2 from Preference import Preference
3
4 DAYS_TO_KEEP_FEED_ENTRIES = "DAYS_TO_KEEP_FEED_ENTRIES"
5 HIDE_READ_ENTRIES = "HIDE_READ_FEED_ENTRIES"
6 PROXY_HOST = "PROXY_HOST"
7 PROXY_PORT = "PROXY_PORT"
8 SHOW_ONLY_UNREAD_FEEDS = "SHOW_ONLY_UNREAD_FEEDS"
9 START_MAXIMIZED = "START_MAXIMIZED"
10
11 def str2bool(string):
12 return string.lower() in ["yes", "true", "t", "1"]
13
14 def bool2str(bool):
15 if bool:
16 return "True"
17 else:
18 return "False"
19
20 class Preferences(object):
21 def __init__(self, session):
22 self.session = session
23 self.cache = {}
24
25 def _cachedPreference(self, key, defaultValue=None, addIfMissing=True):
26 if self.cache.has_key(key):
27 return self.cache[key]
28 else:
29 pref = Preference.forKey(key, self.session)
30 if pref is not None:
31 self.cache[key] = pref
32 elif pref is None and addIfMissing:
33 pref = Preference(key, str(defaultValue))
34 self.session.add(pref)
35 self.cache[key] = pref
36 return pref
37
38 def startMaximized(self):
39 pref = self._cachedPreference(START_MAXIMIZED, False)
40 return str2bool(pref.value)
41
42 def setStartMaximized(self, flag):
43 pref = self._cachedPreference(START_MAXIMIZED)
44 pref.value = bool2str(flag)
45
46 def hideReadFeedEntries(self):
47 pref = self._cachedPreference(HIDE_READ_ENTRIES, False)
48 return str2bool(pref.value)
49
50 def setHideReadFeedEntries(self, flag):
51 pref = self._cachedPreference(HIDE_READ_ENTRIES)
52 pref.value = bool2str(flag)
53
54 def showOnlyUnreadFeeds(self):
55 pref = self._cachedPreference(SHOW_ONLY_UNREAD_FEEDS, False)
56 return str2bool(pref.value)
57
58 def setShowOnlyUnreadFeeds(self, flag):
59 pref = self._cachedPreference(SHOW_ONLY_UNREAD_FEEDS)
60 pref.value = bool2str(flag)
61
62 def proxyHost(self):
63 pref = self._cachedPreference(PROXY_HOST)
64 return pref.value
65
66 def setProxyHost(self, hostname):
67 if hostname is None:
68 pref = self._cachedPreference(PROXY_HOST, addIfMissing=False)
69 if pref is not None:
70 self.session.delete(pref)
71 del(self.cache[PROXY_HOST])
72 else:
73 pref = self._cachedPreference(PROXY_HOST)
74 pref.value = str(hostname)
75
76 def proxyPort(self):
77 pref = self._cachedPreference(PROXY_PORT, 3128)
78 return int(pref.value)
79
80 def setProxyPort(self, port):
81 if port is None:
82 pref = self._cachedPreference(PROXY_PORT, addIfMissing=False)
83 if pref is not None:
84 self.session.delete(pref)
85 del(self.cache[PROXY_PORT])
86 else:
87 pref = self._cachedPreference(PROXY_PORT)
88 pref.value = str(port)
89
90 def isProxyConfigured(self):
91 pref = self._cachedPreference(PROXY_HOST, addIfMissing=False)
92 return pref is not None
93
94 def daysToKeepFeedEntries(self):
95 pref = self._cachedPreference(DAYS_TO_KEEP_FEED_ENTRIES, 90, addIfMissing=True)
96 return int(pref.value)
97
98 def setDaysToKeepFeedEntries(self, dayString):
99 pref = self._cachedPreference(DAYS_TO_KEEP_FEED_ENTRIES)
100 pref.value = dayString