]> git.phdru.name Git - bookmarks_db.git/blob - Storage/bkmk_stjson.py
Fix(Py3): Stop using module `string`
[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-2023 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"] = 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, 'wt')
95         json.dump(self.dict, outfile)
96         outfile.close()
97         del self.dict
98
99     def load(self):
100         infile = open(self.filename, 'rt')
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(
112             bookmarks_dict.get("dateAdded"))
113         root_folder.comment = ''
114         root_folder.last_modified = convert_date_from_json(
115             bookmarks_dict.get("lastModified"))
116         self.folder_stack = [root_folder]
117         self.current_folder = root_folder
118
119         if "type" not in bookmarks_dict:
120             bookmarks_dict["id"] = "0"
121             bookmarks_dict["title"] = ""
122             bookmarks_dict["type"] = "text/x-moz-place-container"
123         self.load_folder(root_folder, bookmarks_dict)
124         if self.folder_stack:
125             raise RuntimeError('Excessive folder stack: %s'
126                                % self.folder_stack)
127
128         return root_folder
129
130     def load_folder(self, folder, fdict):
131         if fdict["type"] != "text/x-moz-place-container":
132             raise ValueError("The object is not a Mozilla container")
133
134         folder.id = fdict["id"]
135         folder.guid = fdict.get("guid")
136         folder.index = fdict.get("index")
137         folder.root = fdict.get("root")
138         folder.name = encode_title(fdict["title"])
139
140         if "children" in fdict:
141             for record in fdict["children"]:
142                 if record["type"] == "text/x-moz-place-container":
143                     folder = Folder(
144                         add_date=convert_date_from_json(
145                             record.get("dateAdded")),
146                         comment=get_comment(record.get("annos")),
147                         last_modified=convert_date_from_json(
148                             record.get("lastModified")))
149                     folder.guid = record.get("guid")
150                     self.current_folder.append(folder)
151                     self.folder_stack.append(folder)
152                     self.current_folder = folder
153                     self.load_folder(folder, record)
154
155                 elif record["type"] == "text/x-moz-place":
156                     bookmark = Bookmark(
157                         href=record["uri"].encode('utf-8'),
158                         add_date=convert_date_from_json(
159                             record.get("dateAdded")),
160                         last_modified=convert_date_from_json(
161                             record.get("lastModified")),
162                         keyword=get_str(record, "keyword"),
163                         comment=get_comment(record.get("annos")),
164                         icon_href=record.get("iconuri"),
165                         charset=get_str(record, "charset"))
166                     bookmark.guid = record.get("guid")
167                     bookmark.id = record["id"]
168                     bookmark.index = record.get("index")
169                     bookmark.name = encode_title(record["title"])
170                     self.current_folder.append(bookmark)
171
172                 elif record["type"] == "text/x-moz-place-separator":
173                     ruler = Ruler()
174                     ruler.add_date = convert_date_from_json(
175                         record.get("dateAdded"))
176                     ruler.guid = record.get("guid")
177                     ruler.id = record["id"]
178                     ruler.index = record["index"]
179                     ruler.last_modified = convert_date_from_json(
180                         record.get("lastModified"))
181                     ruler.name = encode_title(record.get("title"))
182                     ruler.comment = get_comment(record.get("annos"))
183                     self.current_folder.append(ruler)
184
185                 else:
186                     raise ValueError('Unknown record type "%s"'
187                                      % record["type"])
188
189         del self.folder_stack[-1]
190         if self.folder_stack:
191             self.current_folder = self.folder_stack[-1]
192         else:
193             self.current_folder = None
194
195
196 def convert_date_to_json(date):
197     if date:
198         date = int(float(date) * 10**6)
199     return date
200
201
202 def convert_date_from_json(date):
203     if date:
204         date = float(date)
205         if date > 10**10:
206             date /= 10.0**6
207     return date
208
209
210 def encode_title(title):
211     if title:
212         return title.encode("UTF-8", "xmlcharrefreplace")
213     return title
214
215
216 def get_str(record, name):
217     if name in record:
218         return record[name].encode('utf-8')
219     return ''
220
221
222 def get_comment(annos):
223     if not annos:
224         return ''
225
226     for a in annos:
227         if a["name"] == "bookmarkProperties/description":
228             return a["value"].encode('utf-8')
229
230     return ''
231
232
233 def make_annos(value, name="bookmarkProperties/description"):
234     return [{
235         "expires": 4,
236         "flags": 0,
237         "name": name,
238         "value": value.decode('utf-8')}]