-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathklip_cmd.py
executable file
·227 lines (179 loc) · 5.2 KB
/
klip_cmd.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import re
import traceback
from klip_common import getClipPath, getKindleDir, PDEBUG
model = None
stop = False
def loadFile(args):
""" Load clippings from file (if specified), or from default file. """
if args:
path = args[0]
else:
path = getClipPath()
model.loadFile(path)
pass
def showHelp(args):
""" Show help message."""
print('Show help message...\n')
for key in handlers.keys():
func = handlers[key]
print(' %s: %s' % (key, func.__doc__))
pass
def exitFunc(args):
""" exit klip."""
global stop
stop = True
pass
def showBooks():
""" Show books. """
print('Showing books:')
counter = 0
iter = model.getBooks()
while iter.next():
counter += 1
print(' [%d] -- %s' % (iter.id, iter.name))
print('\nTotal books: %d' % (counter))
pass
def showClipIter(it):
idx = 0
while it.next():
idx += 1
print(' [%d] -- %s -- %s' % (it.id, it.pos, it.content))
print('\nTotal clippings: %d' % (idx))
return idx
def showClipsByName(book):
print('Showing clips from book: %s' % book)
num = showClipIter(model.getClipsByBookName(book))
return num
def showClips():
""" Show Clips from all books. """
counter_book = 0
counter_clips = 0
iter_book = model.getBooks()
while iter_book.next():
counter_book += 1
counter_clips += showClipsByName(iter_book.name)
print('\nTotal books: %d, total clips: %d' % (counter_book, counter_clips))
def showFunc(args):
""" Show books or clips, eg:
show books
show clips [book_number]
"""
if args:
target = args.pop(0).lower()
if target == "books":
showBooks()
elif target == "clips":
if args:
PDEBUG('ARGS: %s', args)
book = None
if len(args) == 1:
m = re.match("\\[(\\d+)\\]", args[0])
PDEBUG('Match: %s', m)
if m:
bi = model.getBookById(int(m.group(1)))
if bi.next():
book = bi.name
if book is None:
book = " ".join(args[1:])
showClipsByName(book)
else:
showClips()
else:
raise Exception("not implemented: %s" % target)
else:
showBooks()
pass
def showGUI(args):
"""Show GUI """
from klip_gui import startGUI
startGUI(model)
pass
def cleanupCallback(book, lst):
if lst:
print('Going to remove following items for book: %s\n' % book)
idx = 1
for (keep, drop) in lst:
print('[%d]' % idx)
print(' KEEP: %s' % (keep))
print(' DROP: %s' % (drop))
idx += 1
print('')
print('\nContintue? Y/[N]')
line = sys.stdin.readline().lower().strip()
if len(line) == 0:
return True
if len(line) == 1 and line[0] == 'y':
return True
return False
return True
def cleanUp(books=None):
"""Clean clippings, by removing duplicated records. """
if books:
for book in books:
model.cleanUpBook(book, cleanupCallback)
else:
model.cleanUpBooks(cleanupCallback)
def searchClips(args):
"""Search clippings.
Arguments:
- `args`: List of keywords.
"""
showClipIter(model.searchClips(args))
def cleanupDeviceCallback(lst):
if lst:
print('Going to remove following directories: %s\n')
idx = 1
for dir in lst:
print('[%d] -- %s' % (idx, dir))
idx += 1
print('\nContintue? Y/[N]')
line = sys.stdin.readline().lower().strip()
if len(line) == 0:
return True
if len(line) == 1 and line[0] == 'y':
return True
return False
return True
def cleanDevice(args):
"""Clean up kindle device.
"""
model.cleanUpDevice(getKindleDir(), cleanupDeviceCallback)
handlers = {
"load": loadFile,
"help": showHelp,
"exit": exitFunc,
"quit": exitFunc,
"q": exitFunc,
"show": showFunc,
"clean": cleanUp,
"gui": showGUI,
'search': searchClips,
'clean_dev' : cleanDevice,
}
def startCMD(model_):
global model
model = model_
print('Input your commands here, type "help" for help.. ')
while not stop:
try:
print('>')
line = sys.stdin.readline().strip()
args = line.split()
if not args:
continue
cmd = args.pop(0).lower()
handler = handlers.get(cmd, None)
if handler is None:
print('CMD: %s not implemented' % (cmd))
continue
handler(args)
except Exception as e:
print('str(Exception):\t %s' % str(Exception))
print('str(e):\t\t%s' % str(e))
print('repr(e):\t%s' % repr(e))
print('traceback.print_exc():%s' % traceback.print_exc())
print('traceback.format_exc():\n%s' % traceback.format_exc())
pass