]> git.phdru.name Git - bookmarks_db.git/blobdiff - Storage/bkmk_stjson.py
Fix(Robot): Stop splitting and un-splitting URLs
[bookmarks_db.git] / Storage / bkmk_stjson.py
index 1a34e3a1a02658c75dbe3736e207261009893d4a..1d32fdc8af03de294ecc1eab4bad1f1a78ffaf10 100644 (file)
@@ -41,7 +41,7 @@ class storage_json(Walker):
         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["title"] = f.name
         dict["type"] = "text/x-moz-place-container"
         if root:
             self.dict["children"].append(dict)
@@ -69,9 +69,9 @@ class storage_json(Walker):
         keyword = getattr(b, 'keyword')
         if keyword: dict["keyword"] = keyword
         dict["lastModified"] = convert_date_to_json(b.last_modified)
-        dict["title"] = b.name.decode('utf-8')
+        dict["title"] = b.name
         dict["type"] = "text/x-moz-place"
-        dict["uri"] = uri = b.href
+        dict["uri"] = b.href
         self.folder_stack[-1].append(dict)
 
     def ruler(self, r, level):
@@ -84,20 +84,20 @@ class storage_json(Walker):
         if guid: dict["guid"] = guid
         dict["index"] = r.index
         dict["lastModified"] = convert_date_to_json(r.last_modified)
-        if r.name: dict["title"] = r.name.decode('utf-8')
+        if r.name: dict["title"] = r.name
         dict["type"] = "text/x-moz-place-separator"
         self.folder_stack[-1].append(dict)
 
     def store(self, root_folder):
         root_folder.walk_depth(self)
 
-        outfile = open(self.filename, 'wb')
+        outfile = open(self.filename, 'wt', encoding='utf-8')
         json.dump(self.dict, outfile)
         outfile.close()
         del self.dict
 
     def load(self):
-        infile = open(self.filename, 'rb')
+        infile = open(self.filename, 'rt', encoding='utf-8')
         bkmk_s = infile.read()
         infile.close()
 
@@ -108,9 +108,11 @@ class storage_json(Walker):
 
         root_folder = Folder()
         root_folder.header = ''
-        root_folder.add_date = convert_date_from_json(bookmarks_dict.get("dateAdded"))
+        root_folder.add_date = convert_date_from_json(
+            bookmarks_dict.get("dateAdded"))
         root_folder.comment = ''
-        root_folder.last_modified = convert_date_from_json(bookmarks_dict.get("lastModified"))
+        root_folder.last_modified = convert_date_from_json(
+            bookmarks_dict.get("lastModified"))
         self.folder_stack = [root_folder]
         self.current_folder = root_folder
 
@@ -120,7 +122,8 @@ class storage_json(Walker):
             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)
+            raise RuntimeError('Excessive folder stack: %s'
+                               % self.folder_stack)
 
         return root_folder
 
@@ -132,15 +135,17 @@ class storage_json(Walker):
         folder.guid = fdict.get("guid")
         folder.index = fdict.get("index")
         folder.root = fdict.get("root")
-        folder.name = encode_title(fdict["title"])
+        folder.name = 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")),
+                        add_date=convert_date_from_json(
+                            record.get("dateAdded")),
                         comment=get_comment(record.get("annos")),
-                        last_modified=convert_date_from_json(record.get("lastModified")))
+                        last_modified=convert_date_from_json(
+                            record.get("lastModified")))
                     folder.guid = record.get("guid")
                     self.current_folder.append(folder)
                     self.folder_stack.append(folder)
@@ -149,9 +154,11 @@ class storage_json(Walker):
 
                 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")),
+                        href=record["uri"],
+                        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"),
@@ -159,22 +166,25 @@ class storage_json(Walker):
                     bookmark.guid = record.get("guid")
                     bookmark.id = record["id"]
                     bookmark.index = record.get("index")
-                    bookmark.name = encode_title(record["title"])
+                    bookmark.name = 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.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.last_modified = convert_date_from_json(
+                        record.get("lastModified"))
+                    ruler.name = record.get("title")
                     ruler.comment = get_comment(record.get("annos"))
                     self.current_folder.append(ruler)
 
                 else:
-                    raise ValueError('Unknown record type "%s"' % record["type"])
+                    raise ValueError('Unknown record type "%s"'
+                                     % record["type"])
 
         del self.folder_stack[-1]
         if self.folder_stack:
@@ -197,15 +207,9 @@ def convert_date_from_json(date):
     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 record[name]
     return ''
 
 
@@ -215,7 +219,7 @@ def get_comment(annos):
 
     for a in annos:
         if a["name"] == "bookmarkProperties/description":
-            return a["value"].encode('utf-8')
+            return a["value"]
 
     return ''
 
@@ -225,4 +229,5 @@ def make_annos(value, name="bookmarkProperties/description"):
         "expires": 4,
         "flags": 0,
         "name": name,
-        "value": value.decode('utf-8')}]
+        "value": value,
+    }]