forked from jimmyyhwu/ddsm-visual-primitives
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.py
41 lines (34 loc) · 1.24 KB
/
database.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
import sqlite3 as lite
import os
import sys
sys.path.insert(0,'../db')
from populate_images import populate_db_with_images
from flask import g
class DB(object):
DB_ROOT = os.path.join("..", "db")
DATABASE = os.path.join(DB_ROOT, "test.db")
def __init__(self):
if not os.path.isfile(self.DATABASE):
conn = self.get_connection()
self.__generate_tables(conn)
self.__populate_tables(conn)
def get_connection(self):
try:
db = getattr(g, '_database', None)
if db is None:
db = g._database = lite.connect(self.DATABASE)
except RuntimeError:
# not in flask context:
db = lite.connect(self.DATABASE)
return db
def __generate_tables(self, conn):
with open(os.path.join(self.DB_ROOT, "init.sql"), "r") as generation_script:
conn.execute("PRAGMA foreign_keys=on;")
conn.commit()
conn.executescript(generation_script.read())
conn.commit()
def __populate_tables(self, conn):
# populate images
image_list_path = os.path.join(self.DB_ROOT, "..", "data", "ddsm_raw_image_lists")
populate_db_with_images(conn, image_list_path)
conn.commit()