-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
82 lines (60 loc) · 2.08 KB
/
db.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
import sqlite3
import os
def get_papers(cat="_all"):
""" Returns a list of dicts for all papers in database
"""
conn = sqlite3.connect(get_db_path())
cur = conn.cursor()
if cat != "_all":
cur.execute("select * from papers where category = ?", [cat])
else:
cur.execute("select * from papers")
paper_list = []
for row in cur:
paper_dict = {}
paper_dict["paperid"] = row[0]
paper_dict["title"] = row[1]
paper_dict["authors"] = get_list(row[2])
paper_dict["year"] = row[3]
paper_dict["category"] = row[4]
paper_dict["keywords"] = get_list(row[5])
paper_dict["path"] = row[6]
paper_dict["notes"] = row[7]
paper_list.append(paper_dict)
return paper_list
def add_paper(paper):
""" Accepts a paper as a list and adds it to the database
"""
conn = sqlite3.connect(get_db_path())
cur = conn.cursor()
cur.execute("""insert into papers (title, authors, year, category, keywords, path, notes, important)
values (?, ?, ?, ?, ?, ?, ?, ?)""", paper)
conn.commit()
conn.close()
def del_paper(paperid):
""" Removes paper with specified paperid from database
Also returns path to file containing the paper
"""
conn = sqlite3.connect(get_db_path())
cur = conn.cursor()
cur.execute("select path from papers where paper_id = ?", [paperid])
path = cur.fetchone()[0]
cur.execute("delete from papers where paper_id = ?", [paperid])
conn.commit()
conn.close()
return path
def get_cats():
""" Returns a list of all categories in database
"""
conn = sqlite3.connect(get_db_path())
cur = conn.cursor()
cur.execute("select distinct category from papers")
cat_list = [row[0] for row in cur]
return cat_list
def get_list(string):
""" Accepts a string having comma-separated values and returns a list of those values
"""
return [val.strip() for val in string.split(",")]
def get_db_path():
currpath = os.path.dirname(os.path.realpath(__file__))
return currpath + "/data/paper-browser.db"