-#! /usr/bin/env python
-"""
- Convert a bkmk database back to bookmarks.html (or other format defined by writer)
+#! /usr/bin/env python3
+"""Convert a bkmk database back to bookmarks.html
+(or other format defined by writer)
- Written by Broytman, Mar 2000 - Aug 2002. Copyright (C) 2000-2002 PhiloSoft Design
+This file is a part of Bookmarks database and Internet robot.
"""
+__author__ = "Oleg Broytman <phd@phdru.name>"
+__copyright__ = "Copyright (C) 2000-2023 PhiloSoft Design"
+__license__ = "GNU GPL"
import sys
def run():
- from getopt import getopt
- optlist, args = getopt(sys.argv[1:], "sp:o:t:r")
-
- report_stats = 1
- prune = None
-
- from writers import writer
- output_filename = writer.filename
-
- transl = 0
- transl_name = "" # dictionary translation; default is no translation
-
- for _opt, _arg in optlist:
- if _opt == '-s':
- report_stats = 0
- if _opt == '-p':
- prune = _arg
- if _opt == '-o':
- output_filename = _arg
- if _opt == '-t':
- transl = 1
- transl_name = _arg
- if _opt == '-r':
- transl = 2
- try:
- del _opt, _arg
- except NameError:
- pass
-
- if args:
- sys.stderr.write("db2bkmk: too many arguments\n")
- sys.stderr.write("Usage: db2bkmk [-s] [-p prune_folder] [-o filename] [-t trans] [-r]\n")
- sys.exit(1)
-
- from storage import storage
- storage = storage()
-
- if report_stats:
- sys.stdout.write("Loading %s: " % storage.filename)
- sys.stdout.flush()
-
- root_folder = storage.load()
-
- if report_stats:
- print "Ok"
- sys.stdout.write("Writing %s: " % output_filename)
- sys.stdout.flush()
-
-
- if transl:
- new_ext = str(transl)
- transl_d = {}
-
- from m_lib.flad import fladm
- transl_db = fladm.load_from_file(transl_name, fladm.check_record, ["URL1", "URL2"], [""])
- # This prevents any other key to appear in transl_db ^
-
- # Generate translation dictionary (hash table)
- if transl == 1:
- for record in transl_db:
- transl_d[record["URL1"]] = record["URL2"]
- elif transl == 2:
- for record in transl_db:
- transl_d[record["URL2"]] = record["URL1"]
- else:
- raise ValueError, "transl (%d) must be 1 or 2" % transl
-
- del transl_db # Save few bytes of memory
-
- from bkmk_objects import Walker
- class Transl(Walker):
- def __init__(self, transl_d):
- self.transl_d = transl_d
-
- def bookmark(self, b, level):
- href = b.href
- transl_d = self.transl_d
-
- if transl_d.has_key(href):
- b.href = transl_d[href]
-
- root_folder.walk_depth(Transl(transl_d))
-
-
- outfile = open(output_filename, 'w')
- root_folder.walk_depth(writer(outfile, prune))
- outfile.close()
-
- if report_stats:
- print "Ok"
+ from getopt import getopt
+ optlist, args = getopt(sys.argv[1:], "sp:o:t:r")
+
+ report_stats = 1
+ prune = None
+
+ from writers import writer
+ output_filename = writer.filename
+
+ transl = 0
+ transl_name = "" # dictionary translation; default is no translation
+
+ for _opt, _arg in optlist:
+ if _opt == '-s':
+ report_stats = 0
+ if _opt == '-p':
+ prune = _arg
+ if _opt == '-o':
+ output_filename = _arg
+ if _opt == '-t':
+ transl = 1
+ transl_name = _arg
+ if _opt == '-r':
+ transl = 2
+ try:
+ del _opt, _arg
+ except NameError:
+ pass
+
+ if args:
+ sys.stderr.write("db2bkmk: too many arguments\n")
+ sys.stderr.write("Usage: db2bkmk [-s] [-p prune_folder]"
+ " [-o filename] [-t trans] [-r]\n")
+ sys.exit(1)
+
+ from storage import storage
+ storage = storage()
+
+ if report_stats:
+ sys.stdout.write("Loading %s: " % storage.filename)
+ sys.stdout.flush()
+
+ root_folder = storage.load()
+
+ if report_stats:
+ print("Ok")
+ sys.stdout.write("Writing %s: " % output_filename)
+ sys.stdout.flush()
+
+ if transl:
+ transl_d = {}
+
+ from m_lib.flad import fladm
+ transl_db = fladm.load_from_file(
+ transl_name, fladm.check_record, ["URL1", "URL2"], [""])
+ # This prevents any other key to appear in transl_db ^
+
+ # Generate translation dictionary (hash table)
+ if transl == 1:
+ for record in transl_db:
+ transl_d[record["URL1"]] = record["URL2"]
+ elif transl == 2:
+ for record in transl_db:
+ transl_d[record["URL2"]] = record["URL1"]
+ else:
+ raise ValueError("transl (%d) must be 1 or 2" % transl)
+
+ del transl_db # Save few bytes of memory
+
+ from bkmk_objects import Walker
+
+ class Transl(Walker):
+ def __init__(self, transl_d): # noqa: E306 expected 1 blank line
+ self.transl_d = transl_d
+
+ def bookmark(self, b, level):
+ href = b.href
+ transl_d = self.transl_d
+
+ if href in transl_d:
+ b.href = transl_d[href]
+
+ root_folder.walk_depth(Transl(transl_d))
+
+ outfile = open(output_filename, 'wt', encoding='utf-8')
+ root_folder.walk_depth(writer(outfile, prune))
+ outfile.close()
+
+ if report_stats:
+ print("Ok")
if __name__ == '__main__':
- run()
+ run()