2 """Run through the bookmarks database and set names to titles from an external file
4 This file is a part of Bookmarks database and Internet robot.
7 from __future__ import print_function
11 __author__ = "Oleg Broytman <phd@phdru.name>"
12 __copyright__ = "Copyright (C) 2003-2023 PhiloSoft Design"
13 __license__ = "GNU GPL"
17 from getopt import getopt
18 optlist, args = getopt(sys.argv[1:], "s")
22 for _opt, _arg in optlist:
31 print("Broytman set-title-list, Copyright (C) 2003-2023 PhiloSoft Design")
34 sys.stderr.write("Usage: set-title-list [-s] title_list_file\n")
37 # Read the external file with titles and build a mapping (URL => title)
43 title_list_file = open(args[0], 'r')
44 for line in title_list_file:
45 line = line[:-1] # strip trailing newline
52 elif line: # the third line in every 3 lines must be empty
53 raise ValueError("line is not empty for URL `%s', title `%s': line `%s'" % (URL, title, line))
55 else: # We've got 3 lines - add new entry to the mapping
56 if titles_dict.has_key(URL):
57 if title != titles_dict[URL]:
58 raise ValueError("titles are not identical for URL `%s': `%s' != `%s'" % (URL, title, titles_dict[URL]))
61 titles_dict[URL] = title
67 title_list_file.close()
70 from storage import storage
74 sys.stdout.write("Loading %s: " % storage.filename)
77 root_folder = storage.load()
78 from bkmk_objects import make_linear, break_tree
79 make_linear(root_folder)
80 objects = len(root_folder.linear)
86 # Run through the list of objects and check URLs/titles
88 for object_no in range(objects):
89 object = root_folder.linear[object_no]
93 if titles_dict.has_key(URL):
94 name = titles_dict[URL]
95 if object.name != name:
100 if changed and report_stats:
101 sys.stdout.write("Saving %s: " % storage.filename)
104 if not changed and report_stats:
105 sys.stdout.write("No need to save data\n")
109 break_tree(root_folder.linear)
110 storage.store(root_folder)
112 if changed and report_stats:
114 print(objects, "objects passed")
115 print(changed, "objects changed")
118 if __name__ == '__main__':