forked from applenob/paper_manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManager.py
369 lines (305 loc) · 11.5 KB
/
Manager.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# coding=utf-8
import os
from Color import Colored
import cPickle as Pkl
import sqlite3
from datetime import date
import cmd
import sys
color = Colored()
pkl_name = "user_set.pkl"
paper_item_list = ['paper_name', 'importance',
'urgency', 'tags', 'path',
'read', 'date', 'id']
def init():
global paper_path
global user_set
global conn
global cursor
# get path of papers
if not os.path.exists(pkl_name):
paper_path = raw_input('input path of your papers: ')
if paper_path == '':
paper_path = '/home/cer/文档/nlp'
user_set = {'paper_path': paper_path}
else:
pkl_file = file(pkl_name, 'rb')
user_set = Pkl.load(pkl_file)
paper_path = user_set['paper_path']
# use sqlite
conn = sqlite3.connect('papers.db')
conn.text_factory = str
cursor = conn.cursor()
sql_create = 'create table if not exists papers' \
' ( paper_name varchar(100) , ' \
'importance integer, urgency integer, ' \
'tags varchar(100), path varchar(100), ' \
'read varchar(10), date TEXT, ' \
'id integer primary key autoincrement)'
cursor.execute(sql_create)
def refresh():
global papers_name_now
papers_name_now = {}
traverse_papers(paper_path)
# print papers_name_now
old_papers = cursor.execute("SELECT * FROM papers").fetchall()
for old_paper in old_papers:
old_name = old_paper[paper_item_list.index('paper_name')]
old_path = old_paper[paper_item_list.index('path')]
# delete the paper info that was already been deleted in os
if old_name not in papers_name_now.keys():
del_by_names([old_name])
# if the paper was moved, update the paper path info
elif old_path != papers_name_now[old_name]:
cursor.execute("UPDATE papers SET path = ? where paper_name = ?"
, (papers_name_now[old_name], old_name))
old_paper_names = cursor.execute("SELECT paper_name FROM papers").fetchall()
old_paper_names = [rec[0] for rec in old_paper_names]
for now_paper in papers_name_now.keys():
# find a new paper, put in infos
if now_paper not in old_paper_names:
print color.cyan("Find a new paper: {}".format(now_paper))
paper_im, paper_ug, paper_tags, read = get_on_paper_info_from_user()
insert_one(now_paper, paper_im, paper_ug, paper_tags, read)
def del_by_names(names):
for name in names:
cursor.execute("DELETE FROM papers WHERE paper_name = ?", (name,))
conn.commit()
def insert_one(paper_name, paper_im, paper_ug, paper_tags, read):
cursor.execute("INSERT INTO papers (paper_name, importance, urgency, tags, path, read, date) "
"VALUES (?, ?, ?, ?, ?, ?, ?)", (paper_name, paper_im,
paper_ug, paper_tags, papers_name_now[paper_name],
read, str(date.today())))
conn.commit()
def update_one(paper_name, paper_im, paper_ug, paper_tags, read):
cursor.execute("UPDATE papers set importance=?, urgency=?, "
"tags=?, path=?, read=?, date=? where paper_name=?",
(paper_im, paper_ug, paper_tags, papers_name_now[paper_name],
read, str(date.today()), paper_name))
conn.commit()
def print_papers(recs):
from terminaltables import DoubleTable
recs_head = ['id', 'paper_name',
'importance', 'urgency', 'tags',
'read', 'date']
recs_t = [prettify_one(rec) for rec in recs]
recs_t.insert(0, recs_head)
table = DoubleTable(recs_t)
print table.table
def prettify_one(rec):
one_row = [color.blue_yellow(str(rec[7])),
color.cyan(rec[0]),
color.magenta(str(rec[1])),
color.red(str(rec[2])),
color.blue(rec[3]),
color.yellow(str(rec[5])),
color.green(rec[6])]
one_row = [item.decode('utf-8') for item in one_row]
return one_row
def traverse_papers(fa_path):
"""pre-ordered depth-first search for every paper ends with '.pdf' """
paths = os.listdir(fa_path)
for path in paths:
if os.path.isdir(os.path.join(fa_path, path)):
traverse_papers(os.path.join(fa_path, path))
else:
papers_name_now[path] = os.path.join(fa_path, path)
def recommend_papers():
"""select papers i can read for the sake of importance and urgency"""
rec_papers = cursor.execute("SELECT * from papers where importance!='' and urgency!='' and read='n' ORDER BY urgency DESC , importance DESC LIMIT 5 ").fetchall()
if len(rec_papers) > 0:
print_papers(rec_papers)
def show_tags():
"""show all tags of my papers"""
tags = cursor.execute("SELECT tags from papers").fetchall()
tags = [tag[0] for tag in tags]
tag_set = set()
for line in tags:
for tag in line.strip().split(' '):
if tag.strip() != '':
tag_set.add(tag)
from Color import colors
tag_s = 'All Tags: \n'
for i, tag in enumerate(tag_set):
tag_s += color.paint(colors[i % len(colors)], tag) + ' '
print tag_s
def query_by_tags(tags_s):
"""search papers by tags"""
sets = []
tags = tags_s.strip().split(' ')
if len(tags) > 0:
for tag in tags:
recs = cursor.execute("select * from papers where tags like '%{}%'".format(tag)).fetchall()
if len(recs) > 0:
res_set = set()
for rec in recs:
res_set.add(rec)
sets.append(res_set)
results = reduce(lambda x, y: x & y, sets)
if len(results) > 0:
print_papers(results)
else:
print color.red("find nothing !")
def print_path_by_nums(num_s):
results = query_path_by_nums(num_s)
if len(results) > 0:
for res in results:
print res
else:
print color.red("find nothing !")
def query_path_by_nums(num_s):
results = []
nums = num_s.strip().split(' ')
if len(nums) > 0:
for num in nums:
recs = cursor.execute("select * from papers where id=? ", (num,)).fetchall()
if len(recs) > 0:
for rec in recs:
results.append(rec[4])
return results
def open_paper_by_num(num_s):
results = query_path_by_nums(num_s)
if len(results) == 0:
print color.red("find nothing !")
elif len(results) > 1:
print color.red("too much nums, please input one id num !")
else:
# open paper by system default software, only support linux platform now
if sys.platform.startswith('linux'):
file_path = '\\'.join(results[0])
# print file_path
os.system("xdg-open {} > log.txt 2>&1 &".format(file_path))
else:
print color.red("open file only support linux platform now !")
def query_by_nums(num_s):
"""search papers by id nums"""
results = []
nums = num_s.strip().split(' ')
if len(nums) > 0:
for num in nums:
recs = cursor.execute("select * from papers where id=? ", (num,)).fetchall()
if len(recs) > 0:
for rec in recs:
results.append(rec)
if len(results) > 0:
print_papers(results)
else:
print color.red("find nothing !")
def query_by_id(id_num):
papers = cursor.execute("select * from papers where id=?", (id_num,)).fetchall()
if len(papers) == 1:
print color.cyan("find the paper:")
print_papers(papers)
return papers
def edit_one_paper(id_num):
papers = query_by_id(id_num)
if len(papers) > 0:
paper_im, paper_ug, paper_tags, read = get_on_paper_info_from_user()
# if user only press enter, save the old value
if paper_im == '':
paper_im = papers[0][1]
if paper_ug == '':
paper_ug = papers[0][2]
if paper_tags == '':
paper_tags = papers[0][3]
if read == '':
read = papers[0][5]
update_one(papers[0][0], paper_im, paper_ug, paper_tags, read)
else:
print color.red("paper id num equals {} dose not exist!".format(id_num))
def get_on_paper_info_from_user():
paper_im = raw_input(color.red("Please input the importance of this "
"paper (from 1 to 5):"))
paper_ug = raw_input(color.yellow("Please input the urgency of this "
"paper (from 1 to 5):"))
paper_tags = raw_input(color.blue("Please input the tags of this paper"
" (split by space):"))
read = raw_input(color.magenta("Is this paper has been read?"
" (y/n): "))
print
return paper_im, paper_ug, paper_tags, read
def quit_manager():
# save user data
pkl_file = file(pkl_name, 'wb')
Pkl.dump(user_set, pkl_file)
# close the sqlite db
cursor.close()
conn.commit()
conn.close()
class MyCmd(cmd.Cmd):
"""my command processor"""
def __init__(self):
cmd.Cmd.__init__(self)
self.prompt = '(manager)>'
self.intro = color.yellow('''%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%''')+\
color.cyan('''
%%% %%%% %%%%%%% %%
%%%% %%% %%% %%%%%% %%%
%%%% %%% .%% -%%%% . %%%
%%%% %%% %%% % %%%.=. %%%
%%%% -%%%% % %% %. %%%
%%%% %%%%%%%% %% = %%. %%%
%%%% %%%%%%%% %%% %%%. %%%
%%%% %%%%%%%% %%% %%% %%%
''')+\
color.yellow('''%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n''')+\
color.red('''Paper Manager Usage:''')+\
'''
^---^ ^---^ ^---^ ^---^ ^---^
rec recommend the papers according to urgency and importance
all show all the papers info
tags show all tags
sbt search by tags, like (sbt tag1 tg2)
sbn search by id nums, like (sbn 1 2)
edit edit one paper info by paper id, like (edit 1)
path find path by paper id, like (path 1 2)
open open paper to read by id, like (open 1)
help help info
quit exit the manager
'''
def do_rec(self, arg):
recommend_papers()
def help_rec(self):
print "recommend the papers according to urgency and importance"
def do_all(self, arg):
recs = cursor.execute("SELECT * from papers ").fetchall()
print_papers(recs)
def help_all(self):
print "show all the papers info"
def do_tags(self, arg):
show_tags()
def help_tags(self):
print "show all tags"
def do_sbt(self, arg):
query_by_tags(arg)
def help_sbt(self):
print "search by tags, like (sbt tag1 tg2)"
def do_sbn(self, arg):
query_by_nums(arg)
def help_sbn(self):
print "search by id nums, like (sbn 1 2)"
def do_edit(self, arg):
edit_one_paper(arg)
def help_edit(self):
print "edit one paper info by paper id, like (edit 1),\n" \
"use 'all' or 'tags' to see the id of your paper."
def do_path(self, arg):
print_path_by_nums(arg)
def help_path(self):
print "find path by paper id, like (path 1 2)"
def do_open(self, arg):
open_paper_by_num(arg)
def help_open(self):
print "open paper to read by id, like (open 1)"
def do_quit(self, arg):
print color.yellow("Bye ...")
quit_manager()
import sys
sys.exit()
def help_quit(self):
print 'exit the manager'
if __name__ == '__main__':
init()
refresh()
mycmd = MyCmd()
mycmd.cmdloop()