]> git.phdru.name Git - bookmarks_db.git/blob - set-real_title.py
Feat(Python3): Use print() function
[bookmarks_db.git] / set-real_title.py
1 #! /usr/bin/env python
2 """Run through the bookmarks database and set name to real title
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) 2002-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-real_title, Copyright (C) 2003-2017 PhiloSoft Design")
32
33    if args:
34       sys.stderr.write("set-real_title: too many arguments\n")
35       sys.stderr.write("Usage: set-real_title [-s]\n")
36       sys.exit(1)
37
38    from storage import storage
39    storage = storage()
40
41    if report_stats:
42       sys.stdout.write("Loading %s: " % storage.filename)
43       sys.stdout.flush()
44
45    root_folder = storage.load()
46    from bkmk_objects import make_linear
47    make_linear(root_folder)
48    objects = len(root_folder.linear)
49
50    if report_stats:
51       print("Ok")
52
53
54    changed = 0
55    for object_no in range(objects):
56       object = root_folder.linear[object_no]
57
58       if object.isBookmark:
59          if not hasattr(object, "real_title"):
60             continue
61
62          real_title = object.real_title
63          if not real_title:
64             real_title = object.href
65          if object.name <> real_title:
66             object.name = real_title
67             changed += 1
68
69
70    if changed and report_stats:
71       sys.stdout.write("Saving %s: " % storage.filename)
72       sys.stdout.flush()
73
74    if not changed and report_stats:
75       sys.stdout.write("No need to save data\n")
76       sys.stdout.flush()
77
78    if changed:
79       storage.store(root_folder)
80
81    if changed and report_stats:
82       print("Ok")
83       print(objects, "objects passed")
84       print(changed, "objects changed")
85
86
87 if __name__ == '__main__':
88    run()