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