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