--- /dev/null
+#! /usr/bin/env python3
+"""Run through the bookmarks database and delete bookmarks
+listed in a file
+
+This file is a part of Bookmarks database and Internet robot.
+"""
+
+from __future__ import print_function
+import sys
+
+
+__author__ = "Oleg Broytman <phd@phdru.name>"
+__copyright__ = "Copyright (C) 2023 PhiloSoft Design"
+__license__ = "GNU GPL"
+
+
+def run():
+ from getopt import getopt
+ optlist, args = getopt(sys.argv[1:], "s")
+
+ report_stats = 1
+
+ for _opt, _arg in optlist:
+ if _opt == '-s':
+ report_stats = 0
+ try:
+ del _opt, _arg
+ except NameError:
+ pass
+
+ if report_stats:
+ print("Broytman delete bookmarks, Copyright (C)"
+ " 2023 PhiloSoft Design")
+
+ if len(args) != 1:
+ sys.stderr.write("Usage: delete [-s] del_file\n")
+ sys.exit(1)
+
+ # Read the file with bookmarks to delete
+ delete_urls = set()
+
+ del_file = open(args[0], 'rt')
+ for line in del_file:
+ delete_urls.add(line[:-1]) # strip trailing newline
+ del_file.close()
+
+ from storage import storage
+ storage = storage()
+
+ if report_stats:
+ sys.stdout.write("Loading %s: " % storage.filename)
+ sys.stdout.flush()
+
+ root_folder = storage.load()
+ from bkmk_objects import make_linear, break_tree
+ make_linear(root_folder)
+ objects = len(root_folder.linear)
+
+ if report_stats:
+ print("Ok")
+
+ # Run through the list of objects and store bookmarks indices
+ deleted = 0
+ del_idx = []
+ for object_no in range(objects):
+ object = root_folder.linear[object_no]
+
+ if object.isBookmark:
+ if object.href in delete_urls:
+ del_idx.append(object_no)
+ deleted += 1
+
+ if deleted and report_stats:
+ sys.stdout.write("Saving %s: " % storage.filename)
+ sys.stdout.flush()
+
+ if not deleted and report_stats:
+ sys.stdout.write("No need to save data\n")
+ sys.stdout.flush()
+
+ if deleted:
+ for idx in reversed(del_idx):
+ bookmark = root_folder.linear[idx]
+ folder = bookmark.parent
+ folder_indices = []
+ for object_no in range(len(folder)):
+ object = folder[object_no]
+ if object.isBookmark and object.href in delete_urls:
+ folder_indices.append(object_no)
+ for _idx in reversed(folder_indices):
+ del folder[_idx]
+ del root_folder.linear[idx]
+ break_tree(root_folder.linear)
+ storage.store(root_folder)
+
+ if deleted and report_stats:
+ print("Ok")
+ print(objects, "objects passed")
+ print(deleted, "objects deleted")
+
+
+if __name__ == '__main__':
+ run()