]> git.phdru.name Git - bookmarks_db.git/commitdiff
Convert date to/from mozilla-specific json format.
authorOleg Broytman <phd@phdru.name>
Tue, 20 Dec 2011 04:50:24 +0000 (04:50 +0000)
committerOleg Broytman <phd@phdru.name>
Tue, 20 Dec 2011 04:50:24 +0000 (04:50 +0000)
git-svn-id: file:///home/phd/archive/SVN/bookmarks_db/trunk@356 fdd5c36f-1aea-0310-aeeb-c58d7e2b6c23

Storage/bkmk_stjson.py
Writers/bkmk_wflad.py
Writers/bkmk_whtml.py

index ede71f38502aeba7314cabe05a44ce6d2a6586bc..1a7ee16c4f8c48e0ede0ab56f25ebe7b2d107bdb 100644 (file)
@@ -28,9 +28,9 @@ class storage_json(Walker):
         self.dict = dict = {}
         dict["children"] = children = []
         self.folder_stack = [children]
-        dict["dateAdded"] = f.add_date
+        dict["dateAdded"] = convert_date_to_json(f.add_date)
         dict["id"] = f.id
-        dict["lastModified"] = f.last_modified
+        dict["lastModified"] = convert_date_to_json(f.last_modified)
         dict["root"] = "placesRoot"
         dict["title"] = ""
         dict["type"] = "text/x-moz-place-container"
@@ -40,11 +40,11 @@ class storage_json(Walker):
         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)
         dict["id"] = f.id
         index = getattr(f, 'index')
         if index: dict["index"] = index
-        dict["lastModified"] = f.last_modified
+        dict["lastModified"] = convert_date_to_json(f.last_modified)
         parent_idx = getattr(f, 'parent_idx')
         if parent_idx: dict["parent"] = parent_idx
         root = getattr(f, 'root')
@@ -63,13 +63,13 @@ 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)
         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["lastModified"] = convert_date_to_json(b.last_modified)
         dict["parent"] = b.parent_idx
         dict["title"] = b.name.decode('utf-8')
         dict["type"] = "text/x-moz-place"
@@ -101,10 +101,10 @@ class storage_json(Walker):
         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
         dict["index"] = r.index
-        dict["lastModified"] = r.last_modified
+        dict["lastModified"] = convert_date_to_json(r.last_modified)
         dict["parent"] = r.parent_idx
         dict["title"] = r.name.decode('utf-8')
         dict["type"] = "text/x-moz-place-separator"
@@ -130,9 +130,9 @@ class storage_json(Walker):
 
         root_folder = Folder()
         root_folder.header = ''
-        root_folder.add_date = bookmarks_dict["dateAdded"]
+        root_folder.add_date = convert_date_from_json(bookmarks_dict["dateAdded"])
         root_folder.comment = ''
-        root_folder.last_modified = bookmarks_dict["lastModified"]
+        root_folder.last_modified = convert_date_from_json(bookmarks_dict["lastModified"])
         self.folder_stack = [root_folder]
         self.current_folder = root_folder
 
@@ -155,9 +155,9 @@ class storage_json(Walker):
         for record in fdict["children"]:
             if record["type"] == "text/x-moz-place-container":
                 folder = Folder(
-                    add_date=record["dateAdded"],
+                    add_date=convert_date_from_json(record["dateAdded"]),
                     comment=get_comment(record.get("annos")),
-                    last_modified=record["lastModified"])
+                    last_modified=convert_date_from_json(record["lastModified"]))
                 self.current_folder.append(folder)
                 self.folder_stack.append(folder)
                 self.current_folder = folder
@@ -166,8 +166,8 @@ class storage_json(Walker):
             elif record["type"] == "text/x-moz-place":
                 bookmark = Bookmark(
                     href=record["uri"].encode('utf-8'),
-                    add_date=record.get("dateAdded"),
-                    last_modified=record.get("lastModified"),
+                    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")),
                     charset=get_str(record, "charset"))
@@ -179,10 +179,10 @@ class storage_json(Walker):
 
             elif record["type"] == "text/x-moz-place-separator":
                 ruler = Ruler()
-                ruler.add_date = record["dateAdded"]
+                ruler.add_date = convert_date_from_json(record["dateAdded"])
                 ruler.id = record["id"]
                 ruler.index = record["index"]
-                ruler.last_modified = record["lastModified"]
+                ruler.last_modified = convert_date_from_json(record["lastModified"])
                 ruler.parent_idx = record["parent"]
                 ruler.name = encode_title(record["title"])
                 ruler.comment = get_comment(record.get("annos"))
@@ -197,6 +197,19 @@ class storage_json(Walker):
         else:
             self.current_folder = None
 
+
+def convert_date_to_json(date):
+    if date:
+        date = int(date) * 10**6
+    return date
+
+def convert_date_from_json(date):
+    if date:
+        date = int(date)
+        if date > 10**10:
+            date /= 10**6
+    return date
+
 def encode_title(title):
     return title.encode("UTF-8", "xmlcharrefreplace")
 
index a82c52ca586244e7024bc18ede089afb775d5ab8..6b6d17458c0e1013b80c4f9d8eae28f5ba45c111 100644 (file)
@@ -18,13 +18,8 @@ from bkmk_objects import Writer
 
 
 def strftime(s):
-   if s is None:
-      return s
-   n = int(s)
-   if n > 10**10:
-      n /= 10**6
    try:
-      return time.strftime("%a %d %b %Y %T", time.localtime(n))
+      return time.strftime("%a %d %b %Y %T", time.localtime(int(s)))
    except (TypeError, ValueError): # s is None or is already formatted
       return s
 
index 1e933faf0904a3a4314e2ce4f836da47ad76f295..dd71cb9fb2610ebeb5bd18d5e3bb81893bcd80cf 100644 (file)
@@ -23,12 +23,6 @@ def dump_comment(comment):
       comment = comment.replace("\n", "<BR>\n")
    return comment
 
-def netscape_date(date):
-   if not date or (BKMK_FORMAT == "MOZILLA"):
-      return date
-   else:
-      return int(date)/(10**6)
-
 ind_s = " "*4
 
 class writer_html(Writer):
@@ -45,8 +39,8 @@ class writer_html(Writer):
       self._folder(f, 0)
 
    def start_folder(self, f, level):
-      self.outfile.write(ind_s*level + '<DT><H3 ADD_DATE="%s"' % netscape_date(f.add_date))
-      if (BKMK_FORMAT == "MOZILLA") and f.last_modified: self.outfile.write(' LAST_MODIFIED="%s"' % netscape_date(f.last_modified))
+      self.outfile.write(ind_s*level + '<DT><H3 ADD_DATE="%s"' % f.add_date)
+      if (BKMK_FORMAT == "MOZILLA") and f.last_modified: self.outfile.write(' LAST_MODIFIED="%s"' % f.last_modified)
       self.outfile.write('>%s</H3>\n' % quote_title(f.name))
       self._folder(f, level)
 
@@ -54,9 +48,9 @@ class writer_html(Writer):
       self.outfile.write(ind_s*level + "</DL><p>\n")
 
    def bookmark(self, b, level):
-      self.outfile.write(ind_s*(level+1) + '<DT><A HREF="%s" ADD_DATE="%s"' % (b.href, netscape_date(b.add_date)))
-      if b.last_visit: self.outfile.write(' LAST_VISIT="%s"' % netscape_date(b.last_visit))
-      self.outfile.write(' LAST_MODIFIED="%s"' % netscape_date(b.last_modified))
+      self.outfile.write(ind_s*(level+1) + '<DT><A HREF="%s" ADD_DATE="%s"' % (b.href, b.add_date))
+      if b.last_visit: self.outfile.write(' LAST_VISIT="%s"' % b.last_visit)
+      self.outfile.write(' LAST_MODIFIED="%s"' % b.last_modified)
       if BKMK_FORMAT == "MOZILLA":
          if b.keyword: self.outfile.write(' SHORTCUTURL="%s"' % b.keyword)
          if b.icon_href: self.outfile.write(' ICON_URI="%s"' % b.icon_href)