X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=Storage%2Fbkmk_stjson.py;h=89ee94a631f4c16ba17a33659fc8237f6a26dad0;hb=e7ce4407105621c9e19276a31b4fb87867e858f3;hp=fd77d59f0dcfb6db12d68cb21e78427561588f3f;hpb=3bc7824ef023e60e84ba4402e658836304c248e3;p=bookmarks_db.git diff --git a/Storage/bkmk_stjson.py b/Storage/bkmk_stjson.py index fd77d59..89ee94a 100644 --- a/Storage/bkmk_stjson.py +++ b/Storage/bkmk_stjson.py @@ -1,9 +1,17 @@ -""" - Bookmarks storage manager - json. +"""Bookmarks storage manager - json - Written by Broytman, Jul 2010. Copyright (C) 2010 PhiloSoft Design +This file is a part of Bookmarks database and Internet robot. """ +__version__ = "$Revision$"[11:-2] +__revision__ = "$Id$"[5:-2] +__date__ = "$Date$"[7:-2] +__author__ = "Oleg Broytman " +__copyright__ = "Copyright (C) 2010, 2011 PhiloSoft Design" +__license__ = "GNU GPL" + +__all__ = ['storage_json'] + try: import json @@ -29,6 +37,8 @@ class storage_json(Walker): def start_folder(self, f, level): dict = {} + comment = getattr(f, 'comment') + if comment: dict["annos"] = make_annos(comment) dict["children"] = children = [] dict["dateAdded"] = f.add_date dict["id"] = f.id @@ -37,6 +47,8 @@ class storage_json(Walker): dict["lastModified"] = f.last_modified parent_idx = getattr(f, 'parent_idx') if parent_idx: dict["parent"] = parent_idx + root = getattr(f, 'root') + if root: dict["root"] = root dict["title"] = f.name.decode('utf-8') dict["type"] = "text/x-moz-place-container" self.folder_stack[-1].append(dict) @@ -47,21 +59,40 @@ class storage_json(Walker): def bookmark(self, b, level): dict = {} + comment = getattr(b, 'comment') + if comment: dict["annos"] = make_annos(comment) charset = getattr(b, 'charset') if charset: dict["charset"] = charset dict["dateAdded"] = b.add_date dict["id"] = b.id index = getattr(b, 'index') if index: dict["index"] = index + keyword = getattr(b, 'keyword') + if keyword: dict["keyword"] = keyword dict["lastModified"] = b.last_modified dict["parent"] = b.parent_idx dict["title"] = b.name.decode('utf-8') dict["type"] = "text/x-moz-place" - dict["uri"] = b.href + dict["uri"] = uri = b.href + if uri.startswith('place:'): + if uri.startswith('place:sort=8') \ + or uri.startswith('place://sort=8'): + value = 'MostVisited' + elif uri.startswith('place:folder=BOOKMARKS_MENU') \ + or uri.startswith('place://folder=BOOKMARKS_MENU'): + value = 'RecentlyBookmarked' + elif uri.startswith('place:sort=14') \ + or uri.startswith('place://sort=14'): + value = 'RecentTags' + dict["annos"] = make_annos(value, name='Places/SmartBookmark') + del dict["dateAdded"] + del dict["lastModified"] self.folder_stack[-1].append(dict) def ruler(self, r, level): dict = {} + comment = getattr(r, 'comment') + if comment: dict["annos"] = make_annos(comment) dict["dateAdded"] = r.add_date dict["id"] = r.id dict["index"] = r.index @@ -79,7 +110,6 @@ class storage_json(Walker): outfile.close() del self.dict - def load(self): infile = open(self.filename, 'rb') bkmk_s = infile.read() @@ -88,9 +118,13 @@ class storage_json(Walker): # Work around a bug in Mozilla - remove the trailing comma bkmk_s = bkmk_s.strip().replace(',]', ']') bookmarks_dict = json.loads(bkmk_s) + del bkmk_s root_folder = Folder() root_folder.header = '' + root_folder.add_date = bookmarks_dict["dateAdded"] + root_folder.comment = '' + root_folder.last_modified = bookmarks_dict["lastModified"] self.folder_stack = [root_folder] self.current_folder = root_folder @@ -100,44 +134,41 @@ class storage_json(Walker): return root_folder - def load_folder(self, folder, _dict): - if _dict["type"] != "text/x-moz-place-container": - raise ValueError("Root object is not a Mozilla container") + def load_folder(self, folder, fdict): + if fdict["type"] != "text/x-moz-place-container": + raise ValueError("The object is not a Mozilla container") - folder.id = _dict["id"] - folder.index = _dict.get("index") - folder.parent_idx = _dict.get("parent") + folder.id = fdict["id"] + folder.index = fdict.get("index") + folder.parent_idx = fdict.get("parent") + folder.root = fdict.get("root") + folder.name = encode_title(fdict["title"]) - folder.name = encode(_dict["title"]) - folder.comment = '' - folder.add_date = _dict["dateAdded"] - folder.last_modified = _dict["lastModified"] + for record in fdict["children"]: + if record["type"] == "text/x-moz-place-container": + folder = Folder( + add_date=record["dateAdded"], + comment=get_comment(record.get("annos")), + last_modified=record["lastModified"]) + self.current_folder.append(folder) + self.folder_stack.append(folder) + self.current_folder = folder + self.load_folder(folder, record) - for record in _dict["children"]: - if record["type"] == "text/x-moz-place": + elif record["type"] == "text/x-moz-place": bookmark = Bookmark( - href=record["uri"], + href=record["uri"].encode('utf-8'), add_date=record.get("dateAdded"), last_modified=record.get("lastModified"), - charset=record.get("charset")) + keyword=get_str(record, "keyword"), + comment=get_comment(record.get("annos")), + charset=get_str(record, "charset")) bookmark.id = record["id"] bookmark.index = record.get("index") bookmark.parent_idx = record["parent"] - bookmark.name = encode(record["title"]) + bookmark.name = encode_title(record["title"]) self.current_folder.append(bookmark) - elif record["type"] == "text/x-moz-place-container": - folder = Folder( - add_date=record["dateAdded"], comment=None, - last_modified=record["lastModified"]) - folder.id = record["id"] - folder.parent_idx = record["parent"] - folder.name = encode(record["title"]) - self.current_folder.append(folder) - self.folder_stack.append(folder) - self.current_folder = folder - self.load_folder(folder, record) - elif record["type"] == "text/x-moz-place-separator": ruler = Ruler() ruler.add_date = record["dateAdded"] @@ -145,7 +176,8 @@ class storage_json(Walker): ruler.index = record["index"] ruler.last_modified = record["lastModified"] ruler.parent_idx = record["parent"] - ruler.name = encode(record["title"]) + ruler.name = encode_title(record["title"]) + ruler.comment = get_comment(record.get("annos")) self.current_folder.append(ruler) else: @@ -157,5 +189,30 @@ class storage_json(Walker): else: self.current_folder = None -def encode(title): +def encode_title(title): return title.encode("UTF-8", "xmlcharrefreplace") + +def get_str(record, name): + if name in record: + return record[name].encode('utf-8') + return '' + +def get_comment(annos): + if not annos: + return '' + + for a in annos: + if a["name"] == "bookmarkProperties/description" and \ + a["type"] == 3: + return a["value"].encode('utf-8') + + return '' + +def make_annos(value, name="bookmarkProperties/description"): + return [{ + "expires": 4, + "flags": 0, + "mimeType": None, + "name": name, + "type": 3, + "value": value.decode('utf-8')}]