continue refactor gendb.py
parent
612e061229
commit
34f572555d
138
gendb/gendb.py
138
gendb/gendb.py
|
@ -4,6 +4,7 @@ import os
|
||||||
import pprint
|
import pprint
|
||||||
import sys
|
import sys
|
||||||
from lib.basics import *
|
from lib.basics import *
|
||||||
|
import lib.utils as utils
|
||||||
|
|
||||||
DBFILE = 'tmp.db'
|
DBFILE = 'tmp.db'
|
||||||
DATA_PATH = "../data/yaml" # This is path relative to gendb.py
|
DATA_PATH = "../data/yaml" # This is path relative to gendb.py
|
||||||
|
@ -18,7 +19,7 @@ def main():
|
||||||
print("{}".format(e))
|
print("{}".format(e))
|
||||||
|
|
||||||
# Get a DB conn
|
# Get a DB conn
|
||||||
conn = get_db_conn()
|
conn = utils.get_db_conn(DBFILE)
|
||||||
pragma = "PRAGMA foreign_keys = ON;"
|
pragma = "PRAGMA foreign_keys = ON;"
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
c.execute(pragma)
|
c.execute(pragma)
|
||||||
|
@ -129,6 +130,7 @@ def main():
|
||||||
data = yaml.full_load(yl)
|
data = yaml.full_load(yl)
|
||||||
do_ancestries(data, conn)
|
do_ancestries(data, conn)
|
||||||
|
|
||||||
|
|
||||||
def do_ancestries(data, conn):
|
def do_ancestries(data, conn):
|
||||||
# create tables
|
# create tables
|
||||||
table = """
|
table = """
|
||||||
|
@ -188,7 +190,7 @@ def do_ancestries(data, conn):
|
||||||
sstmt = """
|
sstmt = """
|
||||||
SELECT size_id FROM size WHERE short_name=?;
|
SELECT size_id FROM size WHERE short_name=?;
|
||||||
"""
|
"""
|
||||||
sinp_data = (i['size'],)
|
sinp_data = (i['size'], )
|
||||||
sres = c.execute(sstmt, sinp_data).fetchall()
|
sres = c.execute(sstmt, sinp_data).fetchall()
|
||||||
sid = sres[0][0]
|
sid = sres[0][0]
|
||||||
print(sid)
|
print(sid)
|
||||||
|
@ -197,7 +199,7 @@ def do_ancestries(data, conn):
|
||||||
vstmt = """
|
vstmt = """
|
||||||
SELECT senses_id FROM senses WHERE name=?;
|
SELECT senses_id FROM senses WHERE name=?;
|
||||||
"""
|
"""
|
||||||
vinp_data = (i['senses'],)
|
vinp_data = (i['senses'], )
|
||||||
vres = c.execute(vstmt, vinp_data).fetchall()
|
vres = c.execute(vstmt, vinp_data).fetchall()
|
||||||
print(vres)
|
print(vres)
|
||||||
if len(vres) > 0:
|
if len(vres) > 0:
|
||||||
|
@ -206,9 +208,9 @@ def do_ancestries(data, conn):
|
||||||
vid = None
|
vid = None
|
||||||
print(vid)
|
print(vid)
|
||||||
|
|
||||||
|
|
||||||
#print(i)
|
#print(i)
|
||||||
inp_data.append((i['name'], i['flavor_text'], i['hp'], sid, i['speed'], vid))
|
inp_data.append(
|
||||||
|
(i['name'], i['flavor_text'], i['hp'], sid, i['speed'], vid))
|
||||||
|
|
||||||
stmt = "INSERT INTO ancestries(name, flavor_text, hp, size_id, speed, vision_id) VALUES (?,?,?,?,?,?)"
|
stmt = "INSERT INTO ancestries(name, flavor_text, hp, size_id, speed, vision_id) VALUES (?,?,?,?,?,?)"
|
||||||
try:
|
try:
|
||||||
|
@ -220,7 +222,6 @@ def do_ancestries(data, conn):
|
||||||
else:
|
else:
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
# do boosts
|
# do boosts
|
||||||
for i in data['ancestries']:
|
for i in data['ancestries']:
|
||||||
boostlist = []
|
boostlist = []
|
||||||
|
@ -240,7 +241,9 @@ def do_ancestries(data, conn):
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
print("Error creating ancestries_boosts: {}".format(e))
|
print("Error creating ancestries_boosts: {}".format(e))
|
||||||
except:
|
except:
|
||||||
print("Error creating ancestries_boosts something other than sqlite3 error")
|
print(
|
||||||
|
"Error creating ancestries_boosts something other than sqlite3 error"
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
@ -263,7 +266,9 @@ def do_ancestries(data, conn):
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
print("Error creating ancestries_flaws: {}".format(e))
|
print("Error creating ancestries_flaws: {}".format(e))
|
||||||
except:
|
except:
|
||||||
print("Error creating ancestries_flaws something other than sqlite3 error")
|
print(
|
||||||
|
"Error creating ancestries_flaws something other than sqlite3 error"
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
@ -286,10 +291,13 @@ def do_ancestries(data, conn):
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
print("Error creating ancestries_traits: {}".format(e))
|
print("Error creating ancestries_traits: {}".format(e))
|
||||||
except:
|
except:
|
||||||
print("Error creating ancestries_traits something other than sqlite3 error")
|
print(
|
||||||
|
"Error creating ancestries_traits something other than sqlite3 error"
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
def do_gear(data, conn):
|
def do_gear(data, conn):
|
||||||
table = """
|
table = """
|
||||||
CREATE TABLE gear(
|
CREATE TABLE gear(
|
||||||
|
@ -330,7 +338,8 @@ def do_gear(data, conn):
|
||||||
inp_data = []
|
inp_data = []
|
||||||
for i in data['gear']:
|
for i in data['gear']:
|
||||||
# print(i)
|
# print(i)
|
||||||
inp_data.append((i['bulk'], i['descr'], i['hands'], i['level'], i['name'], i['price_gp']))
|
inp_data.append((i['bulk'], i['descr'], i['hands'], i['level'],
|
||||||
|
i['name'], i['price_gp']))
|
||||||
|
|
||||||
stmt = "INSERT INTO gear(bulk, descr, hands, level, name, price_gp) VALUES (?,?,?,?,?,?)"
|
stmt = "INSERT INTO gear(bulk, descr, hands, level, name, price_gp) VALUES (?,?,?,?,?,?)"
|
||||||
try:
|
try:
|
||||||
|
@ -363,18 +372,19 @@ def do_gear(data, conn):
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
print("Error creating gear_traits data: {}".format(e))
|
print("Error creating gear_traits data: {}".format(e))
|
||||||
except:
|
except:
|
||||||
print("Error creating gear_traits data something other than sqlite3 error")
|
print(
|
||||||
|
"Error creating gear_traits data something other than sqlite3 error"
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# do the sourceentry linking
|
# do the sourceentry linking
|
||||||
for i in data['gear']:
|
for i in data['gear']:
|
||||||
srcentrydata = util_srcentrydata(i)
|
srcentrydata = util_srcentrydata(i)
|
||||||
util_insert_into_sourceentry(srcentrydata, conn)
|
util_insert_into_sourceentry(srcentrydata, conn)
|
||||||
link_sourceentry_gear(i['name'], srcentrydata, conn)
|
link_sourceentry_gear(i['name'], srcentrydata, conn)
|
||||||
|
|
||||||
|
|
||||||
def link_sourceentry_gear(name, srcentrydata, conn):
|
def link_sourceentry_gear(name, srcentrydata, conn):
|
||||||
stmt = """
|
stmt = """
|
||||||
INSERT INTO sourceentry_gear (sourceentry_id, gear_id)
|
INSERT INTO sourceentry_gear (sourceentry_id, gear_id)
|
||||||
|
@ -397,6 +407,7 @@ def link_sourceentry_gear(name, srcentrydata, conn):
|
||||||
else:
|
else:
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
def do_ammo(data, conn):
|
def do_ammo(data, conn):
|
||||||
table = """
|
table = """
|
||||||
CREATE TABLE ammunition (
|
CREATE TABLE ammunition (
|
||||||
|
@ -427,7 +438,8 @@ def do_ammo(data, conn):
|
||||||
inp_data = []
|
inp_data = []
|
||||||
for i in data['ammunition']:
|
for i in data['ammunition']:
|
||||||
# print(i)
|
# print(i)
|
||||||
inp_data.append((i['amount'], i['bulk'], i['descr'], i['name'], i['price_gp']))
|
inp_data.append(
|
||||||
|
(i['amount'], i['bulk'], i['descr'], i['name'], i['price_gp']))
|
||||||
|
|
||||||
stmt = "INSERT INTO ammunition(amount, bulk, descr, name, price_gp) VALUES (?,?,?,?,?)"
|
stmt = "INSERT INTO ammunition(amount, bulk, descr, name, price_gp) VALUES (?,?,?,?,?)"
|
||||||
try:
|
try:
|
||||||
|
@ -444,6 +456,7 @@ def do_ammo(data, conn):
|
||||||
util_insert_into_sourceentry(srcentrydata, conn)
|
util_insert_into_sourceentry(srcentrydata, conn)
|
||||||
link_sourceentry_ammunition(i['name'], srcentrydata, conn)
|
link_sourceentry_ammunition(i['name'], srcentrydata, conn)
|
||||||
|
|
||||||
|
|
||||||
def util_srcentrydata(i):
|
def util_srcentrydata(i):
|
||||||
srcentrydata = []
|
srcentrydata = []
|
||||||
for j in i['source']:
|
for j in i['source']:
|
||||||
|
@ -480,6 +493,7 @@ def link_sourceentry_ammunition(name, srcentrydata, conn):
|
||||||
else:
|
else:
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
def do_armor(data, conn):
|
def do_armor(data, conn):
|
||||||
# Create the 3 tables
|
# Create the 3 tables
|
||||||
table = """
|
table = """
|
||||||
|
@ -1722,101 +1736,5 @@ CREATE TABLE trait (
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
def do_size(data, conn):
|
|
||||||
table = """
|
|
||||||
CREATE TABLE size (
|
|
||||||
size_id INTEGER PRIMARY KEY,
|
|
||||||
short_name TEXT NOT NULL UNIQUE,
|
|
||||||
space_in_ft INTEGER NOT NULL,
|
|
||||||
reach_tall_ft INTEGER NOT NULL,
|
|
||||||
reach_long_ft INTEGER NOT NULL
|
|
||||||
);
|
|
||||||
"""
|
|
||||||
|
|
||||||
c = conn.cursor()
|
|
||||||
c.execute(table)
|
|
||||||
|
|
||||||
inp_data = []
|
|
||||||
for i in data:
|
|
||||||
inp_data.append((i['name'], i['space_in_ft'], i['reach_tall_ft'],
|
|
||||||
i['reach_long_ft']))
|
|
||||||
|
|
||||||
stmt = "INSERT INTO size (short_name, space_in_ft, reach_tall_ft, reach_long_ft) VALUES (?,?,?,?)"
|
|
||||||
try:
|
|
||||||
conn.executemany(stmt, inp_data)
|
|
||||||
except:
|
|
||||||
print("Error creating size")
|
|
||||||
else:
|
|
||||||
conn.commit()
|
|
||||||
|
|
||||||
|
|
||||||
def do_weaponcategory(data, conn):
|
|
||||||
table = """
|
|
||||||
CREATE TABLE weaponcategory (
|
|
||||||
weaponcategory_id INTEGER PRIMARY KEY,
|
|
||||||
"name" TEXT NOT NULL UNIQUE
|
|
||||||
);
|
|
||||||
"""
|
|
||||||
|
|
||||||
c = conn.cursor()
|
|
||||||
c.execute(table)
|
|
||||||
|
|
||||||
inp_data = []
|
|
||||||
for i in data:
|
|
||||||
inp_data.append((i, )) # trailing comma necessary for one-item tuple
|
|
||||||
|
|
||||||
stmt = "INSERT INTO weaponcategory(name) VALUES (?)"
|
|
||||||
try:
|
|
||||||
conn.executemany(stmt, inp_data)
|
|
||||||
except:
|
|
||||||
e = sys.exc_info()[0]
|
|
||||||
print("Error creating weaponcategory: {}".format(e))
|
|
||||||
print(vars(e))
|
|
||||||
else:
|
|
||||||
conn.commit()
|
|
||||||
|
|
||||||
|
|
||||||
def do_movement(data, conn):
|
|
||||||
table = """
|
|
||||||
CREATE TABLE movement (
|
|
||||||
movement_id INTEGER PRIMARY KEY,
|
|
||||||
"name" TEXT UNIQUE NOT NULL
|
|
||||||
);
|
|
||||||
"""
|
|
||||||
|
|
||||||
c = conn.cursor()
|
|
||||||
c.execute(table)
|
|
||||||
|
|
||||||
inp_data = []
|
|
||||||
for i in data:
|
|
||||||
inp_data.append((i, )) # trailing comma necessary for one-item tuple
|
|
||||||
|
|
||||||
stmt = "INSERT INTO movement(name) VALUES (?)"
|
|
||||||
try:
|
|
||||||
conn.executemany(stmt, inp_data)
|
|
||||||
except:
|
|
||||||
e = sys.exc_info()[0]
|
|
||||||
print("Error creating movement: {}".format(e))
|
|
||||||
print(vars(e))
|
|
||||||
else:
|
|
||||||
conn.commit()
|
|
||||||
|
|
||||||
def get_db_conn():
|
|
||||||
## Get database connection
|
|
||||||
conn = sqlite3.connect(DBFILE) # eventually hook this up to be the main db
|
|
||||||
## Set pragmas
|
|
||||||
pragma1 = "PRAGMA synchronous=OFF;"
|
|
||||||
pragma2 = "PRAGMA count_changes=OFF;"
|
|
||||||
pragma3 = "PRAGMA journal_mode=MEMORY;"
|
|
||||||
pragma4 = "PRAGMA temp_store=MEMORY;"
|
|
||||||
pragma5 = "PRAGMA foreign_keys=ON;"
|
|
||||||
conn.execute(pragma1)
|
|
||||||
conn.execute(pragma2)
|
|
||||||
conn.execute(pragma3)
|
|
||||||
conn.execute(pragma4)
|
|
||||||
conn.execute(pragma5)
|
|
||||||
return conn
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
def do_abilityscore(data, conn):
|
def do_abilityscore(data, conn):
|
||||||
table = """
|
table = """
|
||||||
CREATE TABLE abilityscore (
|
CREATE TABLE abilityscore (
|
||||||
|
@ -24,6 +23,7 @@ CREATE TABLE abilityscore (
|
||||||
else:
|
else:
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
def do_actioncost(data, conn):
|
def do_actioncost(data, conn):
|
||||||
table = """
|
table = """
|
||||||
CREATE TABLE actioncost (
|
CREATE TABLE actioncost (
|
||||||
|
@ -48,6 +48,7 @@ CREATE TABLE actioncost (
|
||||||
else:
|
else:
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
def do_alignment(data, conn):
|
def do_alignment(data, conn):
|
||||||
# print(data)
|
# print(data)
|
||||||
table = """
|
table = """
|
||||||
|
@ -73,6 +74,7 @@ CREATE TABLE alignment (
|
||||||
else:
|
else:
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
def do_frequency(data, conn):
|
def do_frequency(data, conn):
|
||||||
table = """
|
table = """
|
||||||
CREATE TABLE frequency (
|
CREATE TABLE frequency (
|
||||||
|
@ -99,8 +101,6 @@ CREATE TABLE frequency (
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def do_langrarity(data, conn):
|
def do_langrarity(data, conn):
|
||||||
table = """
|
table = """
|
||||||
CREATE TABLE langrarity (
|
CREATE TABLE langrarity (
|
||||||
|
@ -125,3 +125,83 @@ CREATE TABLE langrarity (
|
||||||
print(vars(e))
|
print(vars(e))
|
||||||
else:
|
else:
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def do_movement(data, conn):
|
||||||
|
table = """
|
||||||
|
CREATE TABLE movement (
|
||||||
|
movement_id INTEGER PRIMARY KEY,
|
||||||
|
"name" TEXT UNIQUE NOT NULL
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
|
||||||
|
c = conn.cursor()
|
||||||
|
c.execute(table)
|
||||||
|
|
||||||
|
inp_data = []
|
||||||
|
for i in data:
|
||||||
|
inp_data.append((i, )) # trailing comma necessary for one-item tuple
|
||||||
|
|
||||||
|
stmt = "INSERT INTO movement(name) VALUES (?)"
|
||||||
|
try:
|
||||||
|
conn.executemany(stmt, inp_data)
|
||||||
|
except:
|
||||||
|
e = sys.exc_info()[0]
|
||||||
|
print("Error creating movement: {}".format(e))
|
||||||
|
print(vars(e))
|
||||||
|
else:
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def do_size(data, conn):
|
||||||
|
table = """
|
||||||
|
CREATE TABLE size (
|
||||||
|
size_id INTEGER PRIMARY KEY,
|
||||||
|
short_name TEXT NOT NULL UNIQUE,
|
||||||
|
space_in_ft INTEGER NOT NULL,
|
||||||
|
reach_tall_ft INTEGER NOT NULL,
|
||||||
|
reach_long_ft INTEGER NOT NULL
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
|
||||||
|
c = conn.cursor()
|
||||||
|
c.execute(table)
|
||||||
|
|
||||||
|
inp_data = []
|
||||||
|
for i in data:
|
||||||
|
inp_data.append((i['name'], i['space_in_ft'], i['reach_tall_ft'],
|
||||||
|
i['reach_long_ft']))
|
||||||
|
|
||||||
|
stmt = "INSERT INTO size (short_name, space_in_ft, reach_tall_ft, reach_long_ft) VALUES (?,?,?,?)"
|
||||||
|
try:
|
||||||
|
conn.executemany(stmt, inp_data)
|
||||||
|
except:
|
||||||
|
print("Error creating size")
|
||||||
|
else:
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def do_weaponcategory(data, conn):
|
||||||
|
table = """
|
||||||
|
CREATE TABLE weaponcategory (
|
||||||
|
weaponcategory_id INTEGER PRIMARY KEY,
|
||||||
|
"name" TEXT NOT NULL UNIQUE
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
|
||||||
|
c = conn.cursor()
|
||||||
|
c.execute(table)
|
||||||
|
|
||||||
|
inp_data = []
|
||||||
|
for i in data:
|
||||||
|
inp_data.append((i, )) # trailing comma necessary for one-item tuple
|
||||||
|
|
||||||
|
stmt = "INSERT INTO weaponcategory(name) VALUES (?)"
|
||||||
|
try:
|
||||||
|
conn.executemany(stmt, inp_data)
|
||||||
|
except:
|
||||||
|
e = sys.exc_info()[0]
|
||||||
|
print("Error creating weaponcategory: {}".format(e))
|
||||||
|
print(vars(e))
|
||||||
|
else:
|
||||||
|
conn.commit()
|
|
@ -0,0 +1,17 @@
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
def get_db_conn(DBFILE):
|
||||||
|
## Get database connection
|
||||||
|
conn = sqlite3.connect(DBFILE) # eventually hook this up to be the main db
|
||||||
|
## Set pragmas
|
||||||
|
pragma1 = "PRAGMA synchronous=OFF;"
|
||||||
|
pragma2 = "PRAGMA count_changes=OFF;"
|
||||||
|
pragma3 = "PRAGMA journal_mode=MEMORY;"
|
||||||
|
pragma4 = "PRAGMA temp_store=MEMORY;"
|
||||||
|
pragma5 = "PRAGMA foreign_keys=ON;"
|
||||||
|
conn.execute(pragma1)
|
||||||
|
conn.execute(pragma2)
|
||||||
|
conn.execute(pragma3)
|
||||||
|
conn.execute(pragma4)
|
||||||
|
conn.execute(pragma5)
|
||||||
|
return conn
|
Loading…
Reference in New Issue