]> git.phdru.name Git - bookmarks_db.git/blob - set-title-list.py
Feat(Python3): `raise Error, value` -> `raise Error(value)`
[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-2017 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-2017 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
70    from storage import storage
71    storage = storage()
72
73    if report_stats:
74       sys.stdout.write("Loading %s: " % storage.filename)
75       sys.stdout.flush()
76
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)
81
82    if report_stats:
83       print("Ok")
84
85
86    # Run through the list of objects and check URLs/titles
87    changed = 0
88    for object_no in range(objects):
89       object = root_folder.linear[object_no]
90
91       if object.isBookmark:
92          URL = object.href
93          if titles_dict.has_key(URL):
94             name = titles_dict[URL]
95             if object.name <> name:
96                object.name = name
97                changed += 1
98
99
100    if changed and report_stats:
101       sys.stdout.write("Saving %s: " % storage.filename)
102       sys.stdout.flush()
103
104    if not changed and report_stats:
105       sys.stdout.write("No need to save data\n")
106       sys.stdout.flush()
107
108    if changed:
109       break_tree(root_folder.linear)
110       storage.store(root_folder)
111
112    if changed and report_stats:
113       print("Ok")
114       print(objects, "objects passed")
115       print(changed, "objects changed")
116
117
118 if __name__ == '__main__':
119    run()