forked from ViennaRNA/forna
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathforna_db.py
60 lines (48 loc) · 1.31 KB
/
forna_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
#!/usr/bin/python
"""forna_db.py: A script for storing forna sessions in a database and retrieve
a unique hash for sharing via link."""
__author__ = "Stefan Hammer"
__copyright__ = "Copyright 2015"
__version__ = "0.1"
__maintainer__ = "Stefan Hammer"
__email__ = "[email protected]"
import json
import math
import uuid
import sqlite3
import sys
from datetime import datetime, date
database = 'forna.db'
def init():
"""
If the database does not exist, create it
"""
conn = sqlite3.connect(database)
c = conn.cursor()
c.execute('''CREATE TABLE if not exists share
(date timestamp, uuid text, json text)''')
conn.commit()
conn.close()
def put(json):
"""
Store a json file in the database
@param json: The JSON content
"""
identifier = uuid.uuid4().hex
conn = sqlite3.connect(database)
c = conn.cursor()
data = (datetime.now(), identifier, json,)
c.execute('INSERT INTO share VALUES (?,?,?)', data)
conn.commit()
conn.close()
return identifier
def get(identifier):
"""
Get json object by its uuid:
@param uuid: The unique identifier
"""
conn = sqlite3.connect(database)
c = conn.cursor()
c.execute('SELECT json FROM share WHERE uuid=(?)', (identifier,))
json = c.fetchone()[0]
return json