]> git.phdru.name Git - bookmarks_db.git/blobdiff - Storage/bkmk_stjson.py
Load/store keywords.
[bookmarks_db.git] / Storage / bkmk_stjson.py
index 32efdff1d24dc41962b9b2eae0b14d967dbda18f..85eb84cc661f03319f0074007d33e8b30b915b0c 100644 (file)
@@ -17,16 +17,67 @@ class storage_json(Walker):
     filename = "bookmarks_db.json"
 
     def root_folder(self, f):
-        self.dict = {}
+        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):
-        pass
+        dict = {}
+        comment = getattr(f, 'comment')
+        if comment: dict["annos"] = make_annos(comment)
+        dict["children"] = children = []
+        dict["dateAdded"] = f.add_date
+        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
+        dict["title"] = f.name.decode('utf-8')
+        dict["type"] = "text/x-moz-place-container"
+        self.folder_stack[-1].append(dict)
+        self.folder_stack.append(children)
+
+    def end_folder(self, f, level):
+        del self.folder_stack[-1]
 
     def bookmark(self, b, level):
-        pass
+        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
+        self.folder_stack[-1].append(dict)
 
     def ruler(self, r, level):
-        pass
+        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
+        dict["lastModified"] = r.last_modified
+        dict["parent"] = r.parent_idx
+        dict["title"] = r.name.decode('utf-8')
+        dict["type"] = "text/x-moz-place-separator"
+        self.folder_stack[-1].append(dict)
 
     def store(self, root_folder):
         root_folder.walk_depth(self)
@@ -66,26 +117,15 @@ class storage_json(Walker):
         folder.parent_idx = _dict.get("parent")
 
         folder.name = encode(_dict["title"])
-        folder.href = _dict.get("uri")
         folder.comment = ''
         folder.add_date = _dict["dateAdded"]
         folder.last_modified = _dict["lastModified"]
 
         for record in _dict["children"]:
-            if record["type"] == "text/x-moz-place":
-                bookmark = Bookmark(
-                    href=record["uri"],
-                    add_date=record.get("dateAdded"),
-                    last_modified=record.get("lastModified"),
-                    charset=record.get("charset"))
-                bookmark.id = record["id"]
-                bookmark.parent_idx = record["parent"]
-                bookmark.name = encode(record["title"])
-                self.current_folder.append(bookmark)
-
-            elif record["type"] == "text/x-moz-place-container":
+            if record["type"] == "text/x-moz-place-container":
                 folder = Folder(
-                    add_date=record["dateAdded"], comment=None,
+                    add_date=record["dateAdded"],
+                    comment=get_comment(record.get("annos")),
                     last_modified=record["lastModified"])
                 folder.id = record["id"]
                 folder.parent_idx = record["parent"]
@@ -95,6 +135,20 @@ class storage_json(Walker):
                 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"]
@@ -103,6 +157,7 @@ class storage_json(Walker):
                 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:
@@ -116,3 +171,23 @@ class storage_json(Walker):
 
 def encode(title):
     return title.encode("UTF-8", "xmlcharrefreplace")
+
+def get_comment(annos):
+    if not annos:
+        return None
+
+    for a in annos:
+        if a["name"] == "bookmarkProperties/description" and \
+                a["type"] == 3:
+            return a["value"].encode('utf-8')
+
+    return None
+
+def make_annos(comment):
+    return [{
+        "expires": 4,
+        "flags": 0,
+        "mimeType": None,
+        "name": "bookmarkProperties/description",
+        "type": 3,
+        "value": comment.decode('utf-8')}]