1 """Bookmarks storage manager - json
3 This file is a part of Bookmarks database and Internet robot.
7 __author__ = "Oleg Broytman <phd@phdru.name>"
8 __copyright__ = "Copyright (C) 2010-2014 PhiloSoft Design"
9 __license__ = "GNU GPL"
11 __all__ = ['storage_json']
17 import simplejson as json
19 from bkmk_objects import Folder, Bookmark, Ruler, Walker
22 class storage_json(Walker):
23 filename = "bookmarks_db.json"
25 def root_folder(self, f):
27 dict["children"] = children = []
28 self.folder_stack = [children]
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"] = convert_date_to_json(f.add_date)
36 guid = getattr(f, 'guid')
37 if guid: dict["guid"] = guid
39 index = getattr(f, 'index')
40 if index is not None: dict["index"] = index
41 dict["lastModified"] = convert_date_to_json(f.last_modified)
42 root = getattr(f, 'root')
43 if root: dict["root"] = root
44 dict["title"] = f.name.decode('utf-8')
45 dict["type"] = "text/x-moz-place-container"
47 self.dict["children"].append(dict)
49 self.folder_stack[-1].append(dict)
50 self.folder_stack.append(children)
52 def end_folder(self, f, level):
53 del self.folder_stack[-1]
55 def bookmark(self, b, level):
57 comment = getattr(b, 'comment')
58 if comment: dict["annos"] = make_annos(comment)
59 charset = getattr(b, 'charset')
60 if charset: dict["charset"] = charset
61 dict["dateAdded"] = convert_date_to_json(b.add_date)
62 guid = getattr(b, 'guid')
63 if guid: dict["guid"] = guid
65 iconuri = getattr(b, 'icon_href')
66 if iconuri: dict["iconuri"] = iconuri
67 index = getattr(b, 'index')
68 if index is not None: dict["index"] = index
69 keyword = getattr(b, 'keyword')
70 if keyword: dict["keyword"] = keyword
71 dict["lastModified"] = convert_date_to_json(b.last_modified)
72 dict["title"] = b.name.decode('utf-8')
73 dict["type"] = "text/x-moz-place"
74 dict["uri"] = uri = b.href
75 self.folder_stack[-1].append(dict)
77 def ruler(self, r, level):
79 comment = getattr(r, 'comment')
80 if comment: dict["annos"] = make_annos(comment)
81 dict["dateAdded"] = convert_date_to_json(r.add_date)
83 guid = getattr(r, 'guid')
84 if guid: dict["guid"] = guid
85 dict["index"] = r.index
86 dict["lastModified"] = convert_date_to_json(r.last_modified)
87 dict["title"] = r.name.decode('utf-8')
88 dict["type"] = "text/x-moz-place-separator"
89 self.folder_stack[-1].append(dict)
91 def store(self, root_folder):
92 root_folder.walk_depth(self)
94 outfile = open(self.filename, 'wb')
95 json.dump(self.dict, outfile)
100 infile = open(self.filename, 'rb')
101 bkmk_s = infile.read()
104 # Work around a bug in Mozilla - remove the trailing comma
105 bkmk_s = bkmk_s.strip().replace(',]', ']')
106 bookmarks_dict = json.loads(bkmk_s)
109 root_folder = Folder()
110 root_folder.header = ''
111 root_folder.add_date = convert_date_from_json(bookmarks_dict.get("dateAdded"))
112 root_folder.comment = ''
113 root_folder.last_modified = convert_date_from_json(bookmarks_dict.get("lastModified"))
114 self.folder_stack = [root_folder]
115 self.current_folder = root_folder
117 if "type" not in bookmarks_dict:
118 bookmarks_dict["id"] = "0"
119 bookmarks_dict["title"] = ""
120 bookmarks_dict["type"] = "text/x-moz-place-container"
121 self.load_folder(root_folder, bookmarks_dict)
122 if self.folder_stack:
123 raise RuntimeError('Excessive folder stack: %s' % self.folder_stack)
127 def load_folder(self, folder, fdict):
128 if fdict["type"] != "text/x-moz-place-container":
129 raise ValueError("The object is not a Mozilla container")
131 folder.id = fdict["id"]
132 folder.guid = fdict.get("guid")
133 folder.index = fdict.get("index")
134 folder.root = fdict.get("root")
135 folder.name = encode_title(fdict["title"])
137 for record in fdict["children"]:
138 if record["type"] == "text/x-moz-place-container":
140 add_date=convert_date_from_json(record.get("dateAdded")),
141 comment=get_comment(record.get("annos")),
142 last_modified=convert_date_from_json(record.get("lastModified")))
143 folder.guid = record.get("guid")
144 self.current_folder.append(folder)
145 self.folder_stack.append(folder)
146 self.current_folder = folder
147 self.load_folder(folder, record)
149 elif record["type"] == "text/x-moz-place":
151 href=record["uri"].encode('utf-8'),
152 add_date=convert_date_from_json(record.get("dateAdded")),
153 last_modified=convert_date_from_json(record.get("lastModified")),
154 keyword=get_str(record, "keyword"),
155 comment=get_comment(record.get("annos")),
156 icon_href=record.get("iconuri"),
157 charset=get_str(record, "charset"))
158 bookmark.guid = record.get("guid")
159 bookmark.id = record["id"]
160 bookmark.index = record.get("index")
161 bookmark.name = encode_title(record["title"])
162 self.current_folder.append(bookmark)
164 elif record["type"] == "text/x-moz-place-separator":
166 ruler.add_date = convert_date_from_json(record.get("dateAdded"))
167 ruler.guid = record.get("guid")
168 ruler.id = record["id"]
169 ruler.index = record["index"]
170 ruler.last_modified = convert_date_from_json(record.get("lastModified"))
171 ruler.name = encode_title(record["title"])
172 ruler.comment = get_comment(record.get("annos"))
173 self.current_folder.append(ruler)
176 raise ValueError('Unknown record type "%s"' % record["type"])
178 del self.folder_stack[-1]
179 if self.folder_stack:
180 self.current_folder = self.folder_stack[-1]
182 self.current_folder = None
185 def convert_date_to_json(date):
187 date = int(float(date) * 10**6)
190 def convert_date_from_json(date):
197 def encode_title(title):
198 return title.encode("UTF-8", "xmlcharrefreplace")
200 def get_str(record, name):
202 return record[name].encode('utf-8')
205 def get_comment(annos):
210 if a["name"] == "bookmarkProperties/description":
211 return a["value"].encode('utf-8')
215 def make_annos(value, name="bookmarkProperties/description"):
220 "value": value.decode('utf-8')}]