]> git.phdru.name Git - bookmarks_db.git/blob - Writers/bkmk_wflad.py
Fix(Py3): Stop encoding unicode to bytes
[bookmarks_db.git] / Writers / bkmk_wflad.py
1 """Dump bookmarks db to a more readable FLat Ascii Database
2
3 This file is a part of Bookmarks database and Internet robot.
4 """
5
6 __author__ = "Oleg Broytman <phd@phdru.name>"
7 __copyright__ = "Copyright (C) 2000-2023 PhiloSoft Design"
8 __license__ = "GNU GPL"
9
10 __all__ = ['writer_flad']
11
12
13 import time
14 from bkmk_objects import Writer
15 from compat import unicode
16
17
18 def strftime(s):
19     try:
20         return time.strftime("%a %d %b %Y %T", time.localtime(int(s)))
21     except (TypeError, ValueError):  # s is None or is already formatted
22         return s
23
24
25 class writer_flad(Writer):
26     filename = "bookmarks_db.flad"
27
28     def __init__(self, outfile, prune=None):
29         Writer.__init__(self, outfile, prune)
30         self.first_object = 1
31
32     def start_folder(self, f, level):
33         self.outfile.write("""
34 Level: %d
35 Folder: %s
36 AddDate: %s
37 Comment: %s
38 LastModified: %s
39 """ % (
40             level, f.name, strftime(f.add_date), f.comment,
41             strftime(f.last_modified))
42         )
43
44     def bookmark(self, b, level):
45         self.outfile.write("""
46 Level: %d
47 Title: %s
48 URL: %s
49 AddDate: %s
50 LastVisit: %s
51 LastModified: %s
52 Keyword: %s
53 Comment: %s""" % (
54             level+1, b.name, b.href, strftime(b.add_date),
55             strftime(b.last_visit), strftime(b.last_modified),
56             b.keyword, b.comment)
57         )
58
59         for attr_name, attr_out in (
60             ("error", "Error"), ("no_error", "NoError"),
61             ("moved", "Moved"), ("size", "Size"), ("md5", "Md5"),
62             ("real_title", "RealTitle"), ("test_time", "TestTime"),
63             ("icon_href", "IconURI"), ("icon", "Icon"), ("charset", "Charset"),
64         ):
65             if hasattr(b, attr_name):
66                 value = getattr(b, attr_name)
67                 #if isinstance(value, unicode):
68                 #    value = value.encode('utf-8')
69                 self.outfile.write("\n%s: %s" % (attr_out, value))
70
71         if hasattr(b, "last_tested"):
72             self.outfile.write("\nLastTested: %s" % strftime(b.last_tested))
73
74         self.outfile.write("\n")
75
76     def ruler(self, r, level):
77         self.outfile.write("\nLevel: %s\nRuler: YES\n" % (level+1))