]> git.phdru.name Git - bookmarks_db.git/blobdiff - Storage/bkmk_stjson.py
Adapted to different Mozilla place URIs.
[bookmarks_db.git] / Storage / bkmk_stjson.py
index 437ca6d414687e19f0e2a5f8504b4106f4f8691b..ede71f38502aeba7314cabe05a44ce6d2a6586bc 100644 (file)
@@ -10,6 +10,9 @@ __author__ = "Oleg Broytman <phd@phdru.name>"
 __copyright__ = "Copyright (C) 2010, 2011 PhiloSoft Design"
 __license__ = "GNU GPL"
 
+__all__ = ['storage_json']
+
+
 try:
    import json
 except ImportError:
@@ -72,12 +75,23 @@ class storage_json(Walker):
         dict["type"] = "text/x-moz-place"
         dict["uri"] = uri = b.href
         if uri.startswith('place:'):
-            if uri.startswith('place:sort=8'):
+            if uri.startswith('place:sort=8') \
+                    or uri.startswith('place://sort=8'):
                 value = 'MostVisited'
-            elif uri.startswith('place:folder=BOOKMARKS_MENU'):
+            elif uri.startswith('place://redirectsMode'):
+                value = 'Most Visited'
+            elif uri.startswith('place:folder=BOOKMARKS_MENU') \
+                    or uri.startswith('place://folder=BOOKMARKS_MENU'):
                 value = 'RecentlyBookmarked'
-            elif uri.startswith('place:sort=14'):
+            elif uri.startswith('place://folder'):
+                value = 'Recently Bookmarked'
+            elif uri.startswith('place:sort=14') \
+                    or uri.startswith('place://sort=14'):
                 value = 'RecentTags'
+            elif uri.startswith('place://type'):
+                value = 'Recent Tags'
+            else:
+                raise ValueError('UNKNOWN place URI: %s' % uri)
             dict["annos"] = make_annos(value, name='Places/SmartBookmark')
             del dict["dateAdded"]
             del dict["lastModified"]
@@ -104,7 +118,6 @@ class storage_json(Walker):
         outfile.close()
         del self.dict
 
-
     def load(self):
         infile = open(self.filename, 'rb')
         bkmk_s = infile.read()
@@ -137,7 +150,7 @@ class storage_json(Walker):
         folder.index = fdict.get("index")
         folder.parent_idx = fdict.get("parent")
         folder.root = fdict.get("root")
-        folder.name = encode(fdict["title"])
+        folder.name = encode_title(fdict["title"])
 
         for record in fdict["children"]:
             if record["type"] == "text/x-moz-place-container":
@@ -152,16 +165,16 @@ class storage_json(Walker):
 
             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"),
-                    keyword=record.get("keyword"),
+                    keyword=get_str(record, "keyword"),
                     comment=get_comment(record.get("annos")),
-                    charset=record.get("charset"))
+                    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-separator":
@@ -171,7 +184,7 @@ 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)
 
@@ -184,9 +197,14 @@ 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 ''