-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdbop.py
107 lines (76 loc) · 2.26 KB
/
dbop.py
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
103
104
105
106
107
#!/usr/bin/env python
#-*- coding: utf-8 -*-
"""
database operations.
dbop.py manipulate database add, update, delete
"""
import shelve
from os import path
homedir = path.expanduser('~')
def rebuild_library():
import termfeed.dbinit
print('created ".termfeed.db" in {}'.format(homedir))
# instantiate db if it's not created yet
if not path.exists(homedir + '/.termfeed.db'):
rebuild_library()
# connect to db
d = shelve.open(path.join(homedir, '.termfeed'), 'w')
def topics():
return d.keys()
def read(topic):
if topic in d.keys():
return d[topic]
else:
return None
def browse_links(topic):
if topic in d.keys():
links = d[topic]
print('{} resources:'.format(topic))
for link in links:
print('\t{}'.format(link))
else:
print('no category named {}'.format(topic))
print_topics()
def print_topics():
print('available topics: ')
for t in topics():
print('\t{}'.format(t))
def add_link(link, topic='General'):
if topic in d.keys():
if link not in d[topic]:
# to add a new url: copy, mutates, store back
temp = d[topic]
temp.append(link)
d[topic] = temp
print('Updated .. {}'.format(topic))
else:
print('{} already exists in {}!!'.format(link, topic))
else:
print('Created new category .. {}'.format(topic))
d[topic] = [link]
def remove_link(link):
done = False
for topic in topics():
if link in d[topic]:
d[topic] = [l for l in d[topic] if l != link]
print('removed: {}\nfrom: {}'.format(link, topic))
done = True
if not done:
print('URL not found: {}'.format(link))
def delete_topic(topic):
if topic == 'General':
print('Default topic "General" cannot be removed.')
exit()
try:
del d[topic]
print('Removed "{}" from your library.'.format(topic))
except KeyError:
print('"{}" is not in your library!'.format(topic))
exit()
# if __name__ == '__main__':
# for l in read('News'):
# print(l)
# remove_link('http://rt.com/rss/')
# add_link('http://rt.com/rss/', 'News')
# for l in read('News'):
# print(l)