]> git.phdru.name Git - bookmarks_db.git/blob - set-title-list.py
Feat: Set shebang to `python3`
[bookmarks_db.git] / set-title-list.py
1 #! /usr/bin/env python3
2 """Run through the bookmarks database and set names to titles
3 from an external file
4
5 This file is a part of Bookmarks database and Internet robot.
6 """
7
8 from __future__ import print_function
9 import sys
10
11
12 __author__ = "Oleg Broytman <phd@phdru.name>"
13 __copyright__ = "Copyright (C) 2003-2023 PhiloSoft Design"
14 __license__ = "GNU GPL"
15
16
17 def run():
18     from getopt import getopt
19     optlist, args = getopt(sys.argv[1:], "s")
20
21     report_stats = 1
22
23     for _opt, _arg in optlist:
24         if _opt == '-s':
25             report_stats = 0
26     try:
27         del _opt, _arg
28     except NameError:
29         pass
30
31     if report_stats:
32         print("Broytman set-title-list, Copyright (C)"
33               " 2003-2023 PhiloSoft Design")
34
35     if len(args) != 1:
36         sys.stderr.write("Usage: set-title-list [-s] title_list_file\n")
37         sys.exit(1)
38
39     # Read the external file with titles and build a mapping (URL => title)
40     titles_dict = {}
41
42     URL = None
43     title = None
44
45     title_list_file = open(args[0], 'r')
46     for line in title_list_file:
47         line = line[:-1]  # strip trailing newline
48         if URL is None:
49             URL = line
50
51         elif title is None:
52             title = line
53
54         elif line:  # the third line in every 3 lines must be empty
55             raise ValueError(
56                 "line is not empty for URL `%s', title `%s': line `%s'"
57                 % (URL, title, line)
58             )
59
60         else:  # We've got 3 lines - add new entry to the mapping
61             if titles_dict.has_key(URL):
62                 if title != titles_dict[URL]:
63                     raise ValueError(
64                         "titles are not identical for URL `%s': `%s' != `%s'"
65                         % (URL, title, titles_dict[URL])
66                     )
67
68             else:
69                 titles_dict[URL] = title
70
71             # reset
72             URL = None
73             title = None
74
75     title_list_file.close()
76
77     from storage import storage
78     storage = storage()
79
80     if report_stats:
81         sys.stdout.write("Loading %s: " % storage.filename)
82         sys.stdout.flush()
83
84     root_folder = storage.load()
85     from bkmk_objects import make_linear, break_tree
86     make_linear(root_folder)
87     objects = len(root_folder.linear)
88
89     if report_stats:
90         print("Ok")
91
92     # Run through the list of objects and check URLs/titles
93     changed = 0
94     for object_no in range(objects):
95         object = root_folder.linear[object_no]
96
97         if object.isBookmark:
98             URL = object.href
99             if titles_dict.has_key(URL):
100                 name = titles_dict[URL]
101                 if object.name != name:
102                     object.name = name
103                     changed += 1
104
105     if changed and report_stats:
106         sys.stdout.write("Saving %s: " % storage.filename)
107         sys.stdout.flush()
108
109     if not changed and report_stats:
110         sys.stdout.write("No need to save data\n")
111         sys.stdout.flush()
112
113     if changed:
114         break_tree(root_folder.linear)
115         storage.store(root_folder)
116
117     if changed and report_stats:
118         print("Ok")
119         print(objects, "objects passed")
120         print(changed, "objects changed")
121
122
123 if __name__ == '__main__':
124     run()