]> git.phdru.name Git - bookmarks_db.git/blob - set-title-list.py
Added docstrings, __{version,revision,etc}__ boilerplates.
[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 __version__ = "$Revision$"[11:-2]
8 __revision__ = "$Id$"[5:-2]
9 __date__ = "$Date$"[7:-2]
10 __author__ = "Oleg Broytman <phd@phdru.name>"
11 __copyright__ = "Copyright (C) 2003-2011 PhiloSoft Design"
12 __license__ = "GNU GPL"
13
14 import sys
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) 2003-2007 PhiloSoft Design"
33
34    if len(args) <> 1:
35       sys.stderr.write("Usage: set-title-list [-s] title_list_file\n")
36       sys.exit(1)
37
38    # Read the external file with titles and build a mapping (URL => title)
39    titles_dict = {}
40
41    URL = None
42    title = None
43
44    title_list_file = open(args[0], 'r')
45    for line in title_list_file:
46       line = line[:-1] # strip trailing newline
47       if URL is None:
48          URL = line
49
50       elif title is None:
51          title = line
52
53       elif line: # the third line in every 3 lines must be empty
54          raise ValueError, "line is not empty for URL `%s', title `%s': line `%s'" % (URL, title, line)
55
56       else: # We've got 3 lines - add new entry to the mapping
57          if titles_dict.has_key(URL):
58             if title <> titles_dict[URL]:
59                raise ValueError, "titles are not identical for URL `%s': `%s' <> `%s'" % (URL, title, titles_dict[URL])
60
61          else:
62             titles_dict[URL] = title
63
64          # reset
65          URL = None
66          title = None
67
68    title_list_file.close()
69
70
71    from storage import storage
72    storage = storage()
73
74    if report_stats:
75       sys.stdout.write("Loading %s: " % storage.filename)
76       sys.stdout.flush()
77
78    root_folder = storage.load()
79    from bkmk_objects import make_linear, break_tree
80    make_linear(root_folder)
81    objects = len(root_folder.linear)
82
83    if report_stats:
84       print "Ok"
85
86
87    # Run through the list of objects and check URLs/titles
88    changed = 0
89    for object_no in range(objects):
90       object = root_folder.linear[object_no]
91
92       if object.isBookmark:
93          URL = object.href
94          if titles_dict.has_key(URL):
95             name = titles_dict[URL]
96             if object.name <> name:
97                object.name = name
98                changed += 1
99
100
101    if changed and report_stats:
102       sys.stdout.write("Saving %s: " % storage.filename)
103       sys.stdout.flush()
104
105    if not changed and report_stats:
106       sys.stdout.write("No need to save data\n")
107       sys.stdout.flush()
108
109    if changed:
110       break_tree(root_folder.linear)
111       storage.store(root_folder)
112
113    if changed and report_stats:
114       print "Ok"
115       print objects, "objects passed"
116       print changed, "objects changed"
117
118
119 if __name__ == '__main__':
120    run()