]> git.phdru.name Git - bookmarks_db.git/blob - Storage/bkmk_stjson.py
Fix(storage): Adapt to the latest Mozilla format
[bookmarks_db.git] / Storage / bkmk_stjson.py
1 """Bookmarks storage manager - json
2
3 This file is a part of Bookmarks database and Internet robot.
4
5 """
6
7 __author__ = "Oleg Broytman <phd@phdru.name>"
8 __copyright__ = "Copyright (C) 2010-2017 PhiloSoft Design"
9 __license__ = "GNU GPL"
10
11 __all__ = ['storage_json']
12
13
14 try:
15     import json
16 except ImportError:
17     import simplejson as json
18
19 from bkmk_objects import Folder, Bookmark, Ruler, Walker
20
21
22 class storage_json(Walker):
23     filename = "bookmarks_db.json"
24
25     def root_folder(self, f):
26         self.dict = dict = {}
27         dict["children"] = children = []
28         self.folder_stack = [children]
29
30     def start_folder(self, f, level):
31         dict = {}
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
38         dict["id"] = f.id
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"
46         if root:
47             self.dict["children"].append(dict)
48         else:
49             self.folder_stack[-1].append(dict)
50         self.folder_stack.append(children)
51
52     def end_folder(self, f, level):
53         del self.folder_stack[-1]
54
55     def bookmark(self, b, level):
56         dict = {}
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
64         dict["id"] = b.id
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)
76
77     def ruler(self, r, level):
78         dict = {}
79         comment = getattr(r, 'comment')
80         if comment: dict["annos"] = make_annos(comment)
81         dict["dateAdded"] = convert_date_to_json(r.add_date)
82         dict["id"] = r.id
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         if r.name: dict["title"] = r.name.decode('utf-8')
88         dict["type"] = "text/x-moz-place-separator"
89         self.folder_stack[-1].append(dict)
90
91     def store(self, root_folder):
92         root_folder.walk_depth(self)
93
94         outfile = open(self.filename, 'wb')
95         json.dump(self.dict, outfile)
96         outfile.close()
97         del self.dict
98
99     def load(self):
100         infile = open(self.filename, 'rb')
101         bkmk_s = infile.read()
102         infile.close()
103
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)
107         del bkmk_s
108
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
116
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)
124
125         return root_folder
126
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")
130
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"])
136
137         if "children" in fdict:
138             for record in fdict["children"]:
139                 if record["type"] == "text/x-moz-place-container":
140                     folder = Folder(
141                         add_date=convert_date_from_json(record.get("dateAdded")),
142                         comment=get_comment(record.get("annos")),
143                         last_modified=convert_date_from_json(record.get("lastModified")))
144                     folder.guid = record.get("guid")
145                     self.current_folder.append(folder)
146                     self.folder_stack.append(folder)
147                     self.current_folder = folder
148                     self.load_folder(folder, record)
149
150                 elif record["type"] == "text/x-moz-place":
151                     bookmark = Bookmark(
152                         href=record["uri"].encode('utf-8'),
153                         add_date=convert_date_from_json(record.get("dateAdded")),
154                         last_modified=convert_date_from_json(record.get("lastModified")),
155                         keyword=get_str(record, "keyword"),
156                         comment=get_comment(record.get("annos")),
157                         icon_href=record.get("iconuri"),
158                         charset=get_str(record, "charset"))
159                     bookmark.guid = record.get("guid")
160                     bookmark.id = record["id"]
161                     bookmark.index = record.get("index")
162                     bookmark.name = encode_title(record["title"])
163                     self.current_folder.append(bookmark)
164
165                 elif record["type"] == "text/x-moz-place-separator":
166                     ruler = Ruler()
167                     ruler.add_date = convert_date_from_json(record.get("dateAdded"))
168                     ruler.guid = record.get("guid")
169                     ruler.id = record["id"]
170                     ruler.index = record["index"]
171                     ruler.last_modified = convert_date_from_json(record.get("lastModified"))
172                     ruler.name = encode_title(record.get("title"))
173                     ruler.comment = get_comment(record.get("annos"))
174                     self.current_folder.append(ruler)
175
176                 else:
177                     raise ValueError('Unknown record type "%s"' % record["type"])
178
179         del self.folder_stack[-1]
180         if self.folder_stack:
181             self.current_folder = self.folder_stack[-1]
182         else:
183             self.current_folder = None
184
185
186 def convert_date_to_json(date):
187     if date:
188         date = int(float(date) * 10**6)
189     return date
190
191 def convert_date_from_json(date):
192     if date:
193         date = float(date)
194         if date > 10**10:
195             date /= 10.0**6
196     return date
197
198 def encode_title(title):
199     if title:
200         return title.encode("UTF-8", "xmlcharrefreplace")
201     return title
202
203 def get_str(record, name):
204     if name in record:
205         return record[name].encode('utf-8')
206     return ''
207
208 def get_comment(annos):
209     if not annos:
210         return ''
211
212     for a in annos:
213         if a["name"] == "bookmarkProperties/description":
214             return a["value"].encode('utf-8')
215
216     return ''
217
218 def make_annos(value, name="bookmarkProperties/description"):
219     return [{
220         "expires": 4,
221         "flags": 0,
222         "name": name,
223         "value": value.decode('utf-8')}]