]> git.phdru.name Git - bookmarks_db.git/blob - Storage/bkmk_stjson.py
437ca6d414687e19f0e2a5f8504b4106f4f8691b
[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 __version__ = "$Revision$"[11:-2]
7 __revision__ = "$Id$"[5:-2]
8 __date__ = "$Date$"[7:-2]
9 __author__ = "Oleg Broytman <phd@phdru.name>"
10 __copyright__ = "Copyright (C) 2010, 2011 PhiloSoft Design"
11 __license__ = "GNU GPL"
12
13 try:
14    import json
15 except ImportError:
16    import simplejson as json
17
18 from bkmk_objects import Folder, Bookmark, Ruler, Walker
19
20
21 class storage_json(Walker):
22     filename = "bookmarks_db.json"
23
24     def root_folder(self, f):
25         self.dict = dict = {}
26         dict["children"] = children = []
27         self.folder_stack = [children]
28         dict["dateAdded"] = f.add_date
29         dict["id"] = f.id
30         dict["lastModified"] = f.last_modified
31         dict["root"] = "placesRoot"
32         dict["title"] = ""
33         dict["type"] = "text/x-moz-place-container"
34
35     def start_folder(self, f, level):
36         dict = {}
37         comment = getattr(f, 'comment')
38         if comment: dict["annos"] = make_annos(comment)
39         dict["children"] = children = []
40         dict["dateAdded"] = f.add_date
41         dict["id"] = f.id
42         index = getattr(f, 'index')
43         if index: dict["index"] = index
44         dict["lastModified"] = f.last_modified
45         parent_idx = getattr(f, 'parent_idx')
46         if parent_idx: dict["parent"] = parent_idx
47         root = getattr(f, 'root')
48         if root: dict["root"] = root
49         dict["title"] = f.name.decode('utf-8')
50         dict["type"] = "text/x-moz-place-container"
51         self.folder_stack[-1].append(dict)
52         self.folder_stack.append(children)
53
54     def end_folder(self, f, level):
55         del self.folder_stack[-1]
56
57     def bookmark(self, b, level):
58         dict = {}
59         comment = getattr(b, 'comment')
60         if comment: dict["annos"] = make_annos(comment)
61         charset = getattr(b, 'charset')
62         if charset: dict["charset"] = charset
63         dict["dateAdded"] = b.add_date
64         dict["id"] = b.id
65         index = getattr(b, 'index')
66         if index: dict["index"] = index
67         keyword = getattr(b, 'keyword')
68         if keyword: dict["keyword"] = keyword
69         dict["lastModified"] = b.last_modified
70         dict["parent"] = b.parent_idx
71         dict["title"] = b.name.decode('utf-8')
72         dict["type"] = "text/x-moz-place"
73         dict["uri"] = uri = b.href
74         if uri.startswith('place:'):
75             if uri.startswith('place:sort=8'):
76                 value = 'MostVisited'
77             elif uri.startswith('place:folder=BOOKMARKS_MENU'):
78                 value = 'RecentlyBookmarked'
79             elif uri.startswith('place:sort=14'):
80                 value = 'RecentTags'
81             dict["annos"] = make_annos(value, name='Places/SmartBookmark')
82             del dict["dateAdded"]
83             del dict["lastModified"]
84         self.folder_stack[-1].append(dict)
85
86     def ruler(self, r, level):
87         dict = {}
88         comment = getattr(r, 'comment')
89         if comment: dict["annos"] = make_annos(comment)
90         dict["dateAdded"] = r.add_date
91         dict["id"] = r.id
92         dict["index"] = r.index
93         dict["lastModified"] = r.last_modified
94         dict["parent"] = r.parent_idx
95         dict["title"] = r.name.decode('utf-8')
96         dict["type"] = "text/x-moz-place-separator"
97         self.folder_stack[-1].append(dict)
98
99     def store(self, root_folder):
100         root_folder.walk_depth(self)
101
102         outfile = open(self.filename, 'wb')
103         json.dump(self.dict, outfile)
104         outfile.close()
105         del self.dict
106
107
108     def load(self):
109         infile = open(self.filename, 'rb')
110         bkmk_s = infile.read()
111         infile.close()
112
113         # Work around a bug in Mozilla - remove the trailing comma
114         bkmk_s = bkmk_s.strip().replace(',]', ']')
115         bookmarks_dict = json.loads(bkmk_s)
116         del bkmk_s
117
118         root_folder = Folder()
119         root_folder.header = ''
120         root_folder.add_date = bookmarks_dict["dateAdded"]
121         root_folder.comment = ''
122         root_folder.last_modified = bookmarks_dict["lastModified"]
123         self.folder_stack = [root_folder]
124         self.current_folder = root_folder
125
126         self.load_folder(root_folder, bookmarks_dict)
127         if self.folder_stack:
128             raise RuntimeError('Excessive folder stack: %s' % self.folder_stack)
129
130         return root_folder
131
132     def load_folder(self, folder, fdict):
133         if fdict["type"] != "text/x-moz-place-container":
134             raise ValueError("The object is not a Mozilla container")
135
136         folder.id = fdict["id"]
137         folder.index = fdict.get("index")
138         folder.parent_idx = fdict.get("parent")
139         folder.root = fdict.get("root")
140         folder.name = encode(fdict["title"])
141
142         for record in fdict["children"]:
143             if record["type"] == "text/x-moz-place-container":
144                 folder = Folder(
145                     add_date=record["dateAdded"],
146                     comment=get_comment(record.get("annos")),
147                     last_modified=record["lastModified"])
148                 self.current_folder.append(folder)
149                 self.folder_stack.append(folder)
150                 self.current_folder = folder
151                 self.load_folder(folder, record)
152
153             elif record["type"] == "text/x-moz-place":
154                 bookmark = Bookmark(
155                     href=record["uri"],
156                     add_date=record.get("dateAdded"),
157                     last_modified=record.get("lastModified"),
158                     keyword=record.get("keyword"),
159                     comment=get_comment(record.get("annos")),
160                     charset=record.get("charset"))
161                 bookmark.id = record["id"]
162                 bookmark.index = record.get("index")
163                 bookmark.parent_idx = record["parent"]
164                 bookmark.name = encode(record["title"])
165                 self.current_folder.append(bookmark)
166
167             elif record["type"] == "text/x-moz-place-separator":
168                 ruler = Ruler()
169                 ruler.add_date = record["dateAdded"]
170                 ruler.id = record["id"]
171                 ruler.index = record["index"]
172                 ruler.last_modified = record["lastModified"]
173                 ruler.parent_idx = record["parent"]
174                 ruler.name = encode(record["title"])
175                 ruler.comment = get_comment(record.get("annos"))
176                 self.current_folder.append(ruler)
177
178             else:
179                 raise ValueError('Unknown record type "%s"' % record["type"])
180
181         del self.folder_stack[-1]
182         if self.folder_stack:
183             self.current_folder = self.folder_stack[-1]
184         else:
185             self.current_folder = None
186
187 def encode(title):
188     return title.encode("UTF-8", "xmlcharrefreplace")
189
190 def get_comment(annos):
191     if not annos:
192         return ''
193
194     for a in annos:
195         if a["name"] == "bookmarkProperties/description" and \
196                 a["type"] == 3:
197             return a["value"].encode('utf-8')
198
199     return ''
200
201 def make_annos(value, name="bookmarkProperties/description"):
202     return [{
203         "expires": 4,
204         "flags": 0,
205         "mimeType": None,
206         "name": name,
207         "type": 3,
208         "value": value.decode('utf-8')}]