2 def __init__(self, _id, _title, _url, _parent):
7 def dump(self, _intent=' '):
8 print "%s-> %d, %s, %s, %d" % (_intent, self.mId, self.mTitle, self.mUrl, self.mParent)
11 def __init__(self, _id, _name):
16 def setBookmarks(self, _bookmarks):
17 self.mBookmarks = _bookmarks
19 def appendBookmark(self, _bookmark):
20 self.mBookmarks[_bookmark.mId] = _bookmark
23 print " -> %d, %s" % (self.mId, self.mName)
24 for key in self.mBookmarks.iterkeys():
25 self.mBookmarks[key].dump(' ')
28 class SimpleConfigParser:
32 self.mCategoryCurrentIdx = 0
33 self.mBookmarkCurrentIdx = 0
34 self.mDataValid = False
35 self.mPopulateValid = False
41 self.mConfig.read(self.mFileName)
43 self.mCategoryCurrentIdx = self.getNumber('__SYS__', 'category_current_idx')
44 self.mBookmarkCurrentIdx = self.getNumber('__SYS__', 'bookmark_current_idx')
45 self.mDataValid = True
48 with open(self.mFileName, 'wb') as bookmarkFile:
49 self.mConfig.write(bookmarkFile)
50 self.mDataValid = False
51 self.mPopulateValid = False
53 def _del(self, _section, _option=None):
54 #print _section, ' :', _option
56 if not self.exist(_section):
58 self.mConfig.remove_section(_section)
60 if not self.exist(_section, _option):
62 self.mConfig.remove_option(_section, _option)
64 def _get(self, _section, _option, _default):
66 data = self.mConfig.get(_section, _option)
72 def _set(self, _section, _option, _value):
73 self.mConfig.set(_section, _option, _value)
75 def exist(self, _section, _option=None):
77 return self.mConfig.has_section(_section)
78 return self.mConfig.has_option(_section, _option)
80 def setNumber(self, _section, _option, _value):
81 self._set(_section, _option, str(_value))
83 def setString(self, _section, _option, _value):
84 self._set(_section, _option, _value)
86 def getNumber(self, _section, _option, _default=0):
87 return int(self._get(_section, _option, _default))
89 def getString(self, _section, _option, _default=''):
90 return self._get(_section, _option, _default)
92 def delOption(self, _section, _option):
93 self._del(_section, _option)
95 def addSection(self, _section):
96 self.mConfig.add_section(_section)
98 def delSection(self, _section):
101 def init(self, _fileName):
102 self.mFileName = _fileName
103 self.mConfig = ConfigParser.RawConfigParser()
104 if self.mConfig is None:
109 class BookmarkManager(SimpleConfigParser):
111 def __new__(cls, *args, **kwargs):
112 if not cls._instance:
113 cls._instance = super(Singleton, cls).__new__(
114 cls, *args, **kwargs)
117 def __init__(self, _dbFileName):
118 SimpleConfigParser.__init__(self)
120 self.mBookmarkRoot = None
121 self.mDebugEnable = False
124 if not os.path.exists(_dbFileName):
125 os.system('echo "[__SYS__]" > %s'%(_dbFileName))
126 os.system('echo "category_current_idx = 0" >> %s'%(_dbFileName))
127 os.system('echo "bookmark_current_idx = 0" >> %s'%(_dbFileName))
129 self.init(_dbFileName)
131 def message(self, format, params=None):
132 if not self.mDebugEnable:
136 else: print format % (params)
138 def getBookmark(self, _title):
140 for key in self.mBookmarkRoot.iterkeys():
141 for key2 in self.mBookmarkRoot[key].mBookmarks.iterkeys():
142 if self.mBookmarkRoot[key].mBookmarks[key2].mTitle == _title:
143 return 'b-%d' % (self.mBookmarkRoot[key].mBookmarks[key2].mId)
146 def addBookmark(self, _title, _url, _parent):
147 if self.getBookmark(_title) is not None:
149 i = self.mBookmarkCurrentIdx + 1
151 self.message("add bookmark : %s, %s, %d", (_title, _url, _parent,))
153 self.mConfig.add_section(s)
154 self.setNumber(s, 'id', i)
155 self.setString(s, 'title', _title)
156 self.setString(s, 'url', _url)
157 self.setNumber(s, 'parent', _parent)
158 self.setNumber('__SYS__', 'bookmark_current_idx', i)
163 def deleteBookmark(self, _id):
165 self.message("delete bookmark : %d", (_id,))
166 self.delSection('b-%d' % (_id,))
169 def updateBookmark(self, _bookmark):
171 s = "b-%d" % (_bookmark.mId)
172 self.message("update bookmark : %s, %s, %d", (_bookmark.mTitle, _bookmark.mUrl, _bookmark.mParent,))
173 self.setString(s, 'title', _bookmark.mTitle)
174 self.setString(s, 'url', _bookmark.mUrl)
175 self.setNumber(s, 'parent', _bookmark.mParent)
178 def getCategory(self, _name):
180 for key in self.mBookmarkRoot.iterkeys():
181 if self.mBookmarkRoot[key].mName == _name:
182 return 'c-%d' % (self.mBookmarkRoot[key].mId)
185 def addCategory(self, _name):
186 if self.getCategory(_name) is not None:
188 self.message("add category : %s", (_name,))
189 i = self.mCategoryCurrentIdx + 1
192 self.mConfig.add_section(s)
193 self.setNumber(s, 'id', i)
194 self.setNumber(s, 'name', _name)
195 self.setNumber('__SYS__', 'category_current_idx', i)
200 def deleteCategory(self, _id):
202 self.message("delete category : %d", (_id,))
204 for key in self.mBookmarkRoot[_id].mBookmarks.iterkeys():
205 self.delSection('b-%d' % (key,))
207 self.delSection('c-%d' % (_id,))
210 def updateCategory(self, _category):
212 self.message("update category : %s", (_category.mName,))
213 s = "c-%d" % (_category.mId)
214 self.setNumber(s, 'name', _category.mName)
220 self.message("populate : %d, %d", (self.mPopulateValid, self.mDataValid))
223 if self.mPopulateValid:
226 while cx <= self.mCategoryCurrentIdx:
228 i = self.getNumber(s, 'id', -1)
230 n = self.getString(s, 'name')
231 categoryList[i] = CategoryData(i, n)
234 while bx <= self.mBookmarkCurrentIdx:
236 i = self.getNumber(s, 'id', -1)
238 t = self.getString(s, 'title')
239 u = self.getString(s, 'url')
240 p = self.getNumber(s, 'parent')
242 categoryList[p].appendBookmark(BookmarkData(i, t, u, p))
243 except Exception, e: self._del(s)
245 for key in categoryList.iterkeys():
246 sorted(categoryList[key].mBookmarks)
247 self.mBookmarkRoot = categoryList
248 self.mPopulateValid = True
251 def getBookmarkRoot(self):
253 return self.mBookmarkRoot
256 if not self.mDebugEnable:
259 print "-- snapshot --"
260 for key in self.mBookmarkRoot.iterkeys():
261 self.mBookmarkRoot[key].dump()
262 print "--------------"
266 return BookmarkManager('/usr/lib/enigma2/python/Plugins/Extensions/HbbTV/bookmark.ini')