forked from daleathan/ProgrammingInPython3-MarkSummerfield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookmarks.py
More file actions
executable file
·102 lines (86 loc) · 2.87 KB
/
bookmarks.py
File metadata and controls
executable file
·102 lines (86 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
# Copyright (c) 2008-11 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. It is provided for educational
# purposes and is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
import os
import pickle
import shelve
import sys
import Console
import Util
def main():
functions = dict(a=add_bookmark, e=edit_bookmark, l=list_bookmarks,
r=remove_bookmark, q=quit)
filename = os.path.join(os.path.dirname(__file__), "bookmarks.dbm")
db = None
try:
db = shelve.open(filename, protocol=pickle.HIGHEST_PROTOCOL)
action = ""
while True:
print("\nBookmarks ({0})".format(os.path.basename(filename)))
list_bookmarks(db)
print()
menu = "(A)dd (E)dit (L)ist (R)emove (Q)uit"
action = Console.get_menu_choice(menu, "aelrq",
"l" if len(db) else "a", True)
functions[action](db)
finally:
if db is not None:
db.close()
def add_bookmark(db):
url = Console.get_string("URL", "URL")
if not url:
return
if "://" not in url:
url = "http://" + url
name = Console.get_string("Name", "name")
if not name:
return
db[name] = url
db.sync()
def edit_bookmark(db):
name = find_bookmark(db, "edit")
if name is None:
return
url = Console.get_string("URL", "URL", db[name])
if not url:
return
if "://" not in url:
url = "http://" + url
new_name = Console.get_string("Name", "name", name)
db[new_name] = url
if new_name != name:
del db[name]
db.sync()
def list_bookmarks(db):
for i, name in enumerate(sorted(db, key=str.lower)):
print("({0}) {1:.<40} {2}".format(i + 1, name, db[name]))
def remove_bookmark(db):
name = find_bookmark(db, "remove")
if name is None:
return
ans = Console.get_bool("Remove {0}?".format(db[name]), "no")
if ans:
del db[name]
db.sync()
def quit(db):
print("Saved {0} bookmark{1}".format(len(db), Util.s(len(db))))
db.close()
sys.exit()
def find_bookmark(db, message):
message = "Number of bookmark to " + message
number = Console.get_integer(message, "number", minimum=0,
maximum=len(db))
if number == 0:
return None
number -= 1
for i, name in enumerate(sorted(db, key=str.lower)):
if i == number:
return name
main()