2 Bookmarks storage manager - json.
4 Written by Broytman, Jul 2010. Copyright (C) 2010 PhiloSoft Design
11 import simplejson as json
13 from bkmk_objects import Folder, Bookmark, Ruler, Walker
16 class storage_json(Walker):
17 filename = "bookmarks_db.json"
19 def root_folder(self, f):
21 dict["children"] = children = []
22 self.folder_stack = [children]
23 dict["dateAdded"] = f.add_date
25 dict["lastModified"] = f.last_modified
26 dict["root"] = "placesRoot"
28 dict["type"] = "text/x-moz-place-container"
30 def start_folder(self, f, level):
32 comment = getattr(f, 'comment')
33 if comment: dict["annos"] = make_annos(comment)
34 dict["children"] = children = []
35 dict["dateAdded"] = f.add_date
37 index = getattr(f, 'index')
38 if index: dict["index"] = index
39 dict["lastModified"] = f.last_modified
40 parent_idx = getattr(f, 'parent_idx')
41 if parent_idx: dict["parent"] = parent_idx
42 dict["title"] = f.name.decode('utf-8')
43 dict["type"] = "text/x-moz-place-container"
44 self.folder_stack[-1].append(dict)
45 self.folder_stack.append(children)
47 def end_folder(self, f, level):
48 del self.folder_stack[-1]
50 def bookmark(self, b, level):
52 comment = getattr(b, 'comment')
53 if comment: dict["annos"] = make_annos(comment)
54 charset = getattr(b, 'charset')
55 if charset: dict["charset"] = charset
56 dict["dateAdded"] = b.add_date
58 index = getattr(b, 'index')
59 if index: dict["index"] = index
60 keyword = getattr(b, 'keyword')
61 if keyword: dict["keyword"] = keyword
62 dict["lastModified"] = b.last_modified
63 dict["parent"] = b.parent_idx
64 dict["title"] = b.name.decode('utf-8')
65 dict["type"] = "text/x-moz-place"
67 self.folder_stack[-1].append(dict)
69 def ruler(self, r, level):
71 comment = getattr(r, 'comment')
72 if comment: dict["annos"] = make_annos(comment)
73 dict["dateAdded"] = r.add_date
75 dict["index"] = r.index
76 dict["lastModified"] = r.last_modified
77 dict["parent"] = r.parent_idx
78 dict["title"] = r.name.decode('utf-8')
79 dict["type"] = "text/x-moz-place-separator"
80 self.folder_stack[-1].append(dict)
82 def store(self, root_folder):
83 root_folder.walk_depth(self)
85 outfile = open(self.filename, 'wb')
86 json.dump(self.dict, outfile)
92 infile = open(self.filename, 'rb')
93 bkmk_s = infile.read()
96 # Work around a bug in Mozilla - remove the trailing comma
97 bkmk_s = bkmk_s.strip().replace(',]', ']')
98 bookmarks_dict = json.loads(bkmk_s)
100 root_folder = Folder()
101 root_folder.header = ''
102 root_folder.add_date = bookmarks_dict["dateAdded"]
103 root_folder.comment = ''
104 root_folder.last_modified = bookmarks_dict["lastModified"]
105 self.folder_stack = [root_folder]
106 self.current_folder = root_folder
108 self.load_folder(root_folder, bookmarks_dict)
109 if self.folder_stack:
110 raise RuntimeError('Excessive folder stack: %s' % self.folder_stack)
114 def load_folder(self, folder, fdict):
115 if fdict["type"] != "text/x-moz-place-container":
116 raise ValueError("Root object is not a Mozilla container")
118 folder.id = fdict["id"]
119 folder.index = fdict.get("index")
120 folder.parent_idx = fdict.get("parent")
121 folder.name = encode(fdict["title"])
123 for record in fdict["children"]:
124 if record["type"] == "text/x-moz-place-container":
126 add_date=record["dateAdded"],
127 comment=get_comment(record.get("annos")),
128 last_modified=record["lastModified"])
129 self.current_folder.append(folder)
130 self.folder_stack.append(folder)
131 self.current_folder = folder
132 self.load_folder(folder, record)
134 elif record["type"] == "text/x-moz-place":
137 add_date=record.get("dateAdded"),
138 last_modified=record.get("lastModified"),
139 keyword=record.get("keyword"),
140 comment=get_comment(record.get("annos")),
141 charset=record.get("charset"))
142 bookmark.id = record["id"]
143 bookmark.index = record.get("index")
144 bookmark.parent_idx = record["parent"]
145 bookmark.name = encode(record["title"])
146 self.current_folder.append(bookmark)
148 elif record["type"] == "text/x-moz-place-separator":
150 ruler.add_date = record["dateAdded"]
151 ruler.id = record["id"]
152 ruler.index = record["index"]
153 ruler.last_modified = record["lastModified"]
154 ruler.parent_idx = record["parent"]
155 ruler.name = encode(record["title"])
156 ruler.comment = get_comment(record.get("annos"))
157 self.current_folder.append(ruler)
160 raise ValueError('Unknown record type "%s"' % record["type"])
162 del self.folder_stack[-1]
163 if self.folder_stack:
164 self.current_folder = self.folder_stack[-1]
166 self.current_folder = None
169 return title.encode("UTF-8", "xmlcharrefreplace")
171 def get_comment(annos):
176 if a["name"] == "bookmarkProperties/description" and \
178 return a["value"].encode('utf-8')
182 def make_annos(comment):
187 "name": "bookmarkProperties/description",
189 "value": comment.decode('utf-8')}]