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