]> git.phdru.name Git - bookmarks_db.git/blob - set-title-list.py
Pass subproc_* parameters to the subprocess
[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 __author__ = "Oleg Broytman <phd@phdru.name>"
8 __copyright__ = "Copyright (C) 2003-2012 PhiloSoft Design"
9 __license__ = "GNU GPL"
10
11 import sys
12
13
14 def run():
15    from getopt import getopt
16    optlist, args = getopt(sys.argv[1:], "s")
17
18    report_stats = 1
19
20    for _opt, _arg in optlist:
21       if _opt == '-s':
22          report_stats = 0
23    try:
24       del _opt, _arg
25    except NameError:
26       pass
27
28    if report_stats:
29       print "Broytman set-title-list, Copyright (C) 2003-2007 PhiloSoft Design"
30
31    if len(args) <> 1:
32       sys.stderr.write("Usage: set-title-list [-s] title_list_file\n")
33       sys.exit(1)
34
35    # Read the external file with titles and build a mapping (URL => title)
36    titles_dict = {}
37
38    URL = None
39    title = None
40
41    title_list_file = open(args[0], 'r')
42    for line in title_list_file:
43       line = line[:-1] # strip trailing newline
44       if URL is None:
45          URL = line
46
47       elif title is None:
48          title = line
49
50       elif line: # the third line in every 3 lines must be empty
51          raise ValueError, "line is not empty for URL `%s', title `%s': line `%s'" % (URL, title, line)
52
53       else: # We've got 3 lines - add new entry to the mapping
54          if titles_dict.has_key(URL):
55             if title <> titles_dict[URL]:
56                raise ValueError, "titles are not identical for URL `%s': `%s' <> `%s'" % (URL, title, titles_dict[URL])
57
58          else:
59             titles_dict[URL] = title
60
61          # reset
62          URL = None
63          title = None
64
65    title_list_file.close()
66
67
68    from storage import storage
69    storage = storage()
70
71    if report_stats:
72       sys.stdout.write("Loading %s: " % storage.filename)
73       sys.stdout.flush()
74
75    root_folder = storage.load()
76    from bkmk_objects import make_linear, break_tree
77    make_linear(root_folder)
78    objects = len(root_folder.linear)
79
80    if report_stats:
81       print "Ok"
82
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
98    if changed and report_stats:
99       sys.stdout.write("Saving %s: " % storage.filename)
100       sys.stdout.flush()
101
102    if not changed and report_stats:
103       sys.stdout.write("No need to save data\n")
104       sys.stdout.flush()
105
106    if changed:
107       break_tree(root_folder.linear)
108       storage.store(root_folder)
109
110    if changed and report_stats:
111       print "Ok"
112       print objects, "objects passed"
113       print changed, "objects changed"
114
115
116 if __name__ == '__main__':
117    run()