X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=Storage%2Fbkmk_stjson.py;h=6053bd1d360f0fda72991ab53b360406df6beba1;hb=9e3b27b8e808eb8d1453f25ef733efb8cbc41d31;hp=00daa60361710e5c330cb51b62bc952c279bc757;hpb=b3939ade7f545a07b93188543761ada36e996d11;p=bookmarks_db.git diff --git a/Storage/bkmk_stjson.py b/Storage/bkmk_stjson.py index 00daa60..6053bd1 100644 --- a/Storage/bkmk_stjson.py +++ b/Storage/bkmk_stjson.py @@ -1,14 +1,20 @@ -""" - Bookmarks storage manager - json. +"""Bookmarks storage manager - json + +This file is a part of Bookmarks database and Internet robot. - Written by Broytman, Jul 2010. Copyright (C) 2010 PhiloSoft Design """ +__author__ = "Oleg Broytman " +__copyright__ = "Copyright (C) 2010-2017 PhiloSoft Design" +__license__ = "GNU GPL" + +__all__ = ['storage_json'] + try: - import json + import json except ImportError: - import simplejson as json + import simplejson as json from bkmk_objects import Folder, Bookmark, Ruler, Walker @@ -20,30 +26,27 @@ class storage_json(Walker): self.dict = dict = {} dict["children"] = children = [] self.folder_stack = [children] - dict["dateAdded"] = f.add_date - dict["id"] = f.id - dict["lastModified"] = f.last_modified - dict["root"] = "placesRoot" - dict["title"] = "" - dict["type"] = "text/x-moz-place-container" 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["dateAdded"] = convert_date_to_json(f.add_date) + guid = getattr(f, 'guid') + if guid: dict["guid"] = guid dict["id"] = f.id index = getattr(f, 'index') - if index: dict["index"] = index - dict["lastModified"] = f.last_modified - parent_idx = getattr(f, 'parent_idx') - if parent_idx: dict["parent"] = parent_idx + if index is not None: dict["index"] = index + dict["lastModified"] = convert_date_to_json(f.last_modified) 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) + if root: + self.dict["children"].append(dict) + else: + self.folder_stack[-1].append(dict) self.folder_stack.append(children) def end_folder(self, f, level): @@ -55,29 +58,33 @@ class storage_json(Walker): if comment: dict["annos"] = make_annos(comment) charset = getattr(b, 'charset') if charset: dict["charset"] = charset - dict["dateAdded"] = b.add_date + dict["dateAdded"] = convert_date_to_json(b.add_date) + guid = getattr(b, 'guid') + if guid: dict["guid"] = guid dict["id"] = b.id + iconuri = getattr(b, 'icon_href') + if iconuri: dict["iconuri"] = iconuri index = getattr(b, 'index') - if index: dict["index"] = index + if index is not None: dict["index"] = index keyword = getattr(b, 'keyword') if keyword: dict["keyword"] = keyword - dict["lastModified"] = b.last_modified - dict["parent"] = b.parent_idx + dict["lastModified"] = convert_date_to_json(b.last_modified) dict["title"] = b.name.decode('utf-8') dict["type"] = "text/x-moz-place" - dict["uri"] = b.href + dict["uri"] = uri = b.href 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["dateAdded"] = convert_date_to_json(r.add_date) dict["id"] = r.id + guid = getattr(r, 'guid') + if guid: dict["guid"] = guid dict["index"] = r.index - dict["lastModified"] = r.last_modified - dict["parent"] = r.parent_idx - dict["title"] = r.name.decode('utf-8') + dict["lastModified"] = convert_date_to_json(r.last_modified) + if r.name: dict["title"] = r.name.decode('utf-8') dict["type"] = "text/x-moz-place-separator" self.folder_stack[-1].append(dict) @@ -89,7 +96,6 @@ class storage_json(Walker): outfile.close() del self.dict - def load(self): infile = open(self.filename, 'rb') bkmk_s = infile.read() @@ -98,15 +104,20 @@ 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.add_date = convert_date_from_json(bookmarks_dict.get("dateAdded")) root_folder.comment = '' - root_folder.last_modified = bookmarks_dict["lastModified"] + root_folder.last_modified = convert_date_from_json(bookmarks_dict.get("lastModified")) self.folder_stack = [root_folder] self.current_folder = root_folder + if "type" not in bookmarks_dict: + bookmarks_dict["id"] = "0" + bookmarks_dict["title"] = "" + bookmarks_dict["type"] = "text/x-moz-place-container" self.load_folder(root_folder, bookmarks_dict) if self.folder_stack: raise RuntimeError('Excessive folder stack: %s' % self.folder_stack) @@ -115,52 +126,55 @@ class storage_json(Walker): def load_folder(self, folder, fdict): if fdict["type"] != "text/x-moz-place-container": - raise ValueError("Root object is not a Mozilla container") + raise ValueError("The object is not a Mozilla container") folder.id = fdict["id"] + folder.guid = fdict.get("guid") folder.index = fdict.get("index") - folder.parent_idx = fdict.get("parent") folder.root = fdict.get("root") - folder.name = encode(fdict["title"]) - - 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) - - elif record["type"] == "text/x-moz-place": - bookmark = Bookmark( - href=record["uri"], - add_date=record.get("dateAdded"), - last_modified=record.get("lastModified"), - keyword=record.get("keyword"), - comment=get_comment(record.get("annos")), - charset=record.get("charset")) - bookmark.id = record["id"] - bookmark.index = record.get("index") - bookmark.parent_idx = record["parent"] - bookmark.name = encode(record["title"]) - self.current_folder.append(bookmark) - - elif record["type"] == "text/x-moz-place-separator": - ruler = Ruler() - ruler.add_date = record["dateAdded"] - ruler.id = record["id"] - ruler.index = record["index"] - ruler.last_modified = record["lastModified"] - ruler.parent_idx = record["parent"] - ruler.name = encode(record["title"]) - ruler.comment = get_comment(record.get("annos")) - self.current_folder.append(ruler) - - else: - raise ValueError('Unknown record type "%s"' % record["type"]) + folder.name = encode_title(fdict["title"]) + + if "children" in fdict: + for record in fdict["children"]: + if record["type"] == "text/x-moz-place-container": + folder = Folder( + add_date=convert_date_from_json(record.get("dateAdded")), + comment=get_comment(record.get("annos")), + last_modified=convert_date_from_json(record.get("lastModified"))) + folder.guid = record.get("guid") + 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": + bookmark = Bookmark( + href=record["uri"].encode('utf-8'), + add_date=convert_date_from_json(record.get("dateAdded")), + last_modified=convert_date_from_json(record.get("lastModified")), + keyword=get_str(record, "keyword"), + comment=get_comment(record.get("annos")), + icon_href=record.get("iconuri"), + charset=get_str(record, "charset")) + bookmark.guid = record.get("guid") + bookmark.id = record["id"] + bookmark.index = record.get("index") + bookmark.name = encode_title(record["title"]) + self.current_folder.append(bookmark) + + elif record["type"] == "text/x-moz-place-separator": + ruler = Ruler() + ruler.add_date = convert_date_from_json(record.get("dateAdded")) + ruler.guid = record.get("guid") + ruler.id = record["id"] + ruler.index = record["index"] + ruler.last_modified = convert_date_from_json(record.get("lastModified")) + ruler.name = encode_title(record.get("title")) + ruler.comment = get_comment(record.get("annos")) + self.current_folder.append(ruler) + + else: + raise ValueError('Unknown record type "%s"' % record["type"]) del self.folder_stack[-1] if self.folder_stack: @@ -168,25 +182,42 @@ class storage_json(Walker): else: self.current_folder = None -def encode(title): - return title.encode("UTF-8", "xmlcharrefreplace") + +def convert_date_to_json(date): + if date: + date = int(float(date) * 10**6) + return date + +def convert_date_from_json(date): + if date: + date = float(date) + if date > 10**10: + date /= 10.0**6 + return date + +def encode_title(title): + if title: + return title.encode("UTF-8", "xmlcharrefreplace") + return title + +def get_str(record, name): + if name in record: + return record[name].encode('utf-8') + return '' def get_comment(annos): if not annos: - return None + return '' for a in annos: - if a["name"] == "bookmarkProperties/description" and \ - a["type"] == 3: + if a["name"] == "bookmarkProperties/description": return a["value"].encode('utf-8') - return None + return '' -def make_annos(comment): +def make_annos(value, name="bookmarkProperties/description"): return [{ "expires": 4, "flags": 0, - "mimeType": None, - "name": "bookmarkProperties/description", - "type": 3, - "value": comment.decode('utf-8')}] + "name": name, + "value": value.decode('utf-8')}]