Clean up remaining sqlite code

This commit is contained in:
Correl Roush 2022-07-27 23:26:33 -04:00
parent 88575765b8
commit 5e641a23e4
2 changed files with 21 additions and 20 deletions

View file

@ -3,5 +3,4 @@ set -e
. /venv/bin/activate . /venv/bin/activate
sqlite3 ${TUTOR_DATABASE} <./tables.sql
exec tutor $@ exec tutor $@

View file

@ -5,7 +5,6 @@ import logging
import os.path import os.path
import re import re
import aiosqlite
import click import click
import httpx import httpx
import humanize import humanize
@ -116,15 +115,18 @@ def import_deck(ctx, filename, name):
name = name or os.path.splitext(os.path.basename(filename))[0] name = name or os.path.splitext(os.path.basename(filename))[0]
async def import_deck(): async def import_deck():
async with aiosqlite.connect(ctx.obj["database"]) as db: async with await psycopg.AsyncConnection.connect(
deck_id = await tutor.database.store_deck(db, name) ctx.obj["database"], autocommit=False
) as conn:
async with conn.cursor() as cursor:
deck_id = await tutor.database.store_deck(cursor, name)
with tqdm.tqdm(lines) as bar: with tqdm.tqdm(lines) as bar:
bar.write(f"Importing deck '{name}' [{deck_id}]") bar.write(f"Importing deck '{name}' [{deck_id}]")
for line in bar: for line in bar:
if match := line_pattern.match(line.strip()): if match := line_pattern.match(line.strip()):
groups = match.groupdict() groups = match.groupdict()
cards = await tutor.database.advanced_search( cards = await tutor.database.advanced_search(
db, cursor,
tutor.search.Search( tutor.search.Search(
[tutor.search.Name(text=groups["name"])] [tutor.search.Name(text=groups["name"])]
), ),
@ -132,9 +134,9 @@ def import_deck(ctx, filename, name):
) )
if cards: if cards:
await tutor.database.store_deck_card( await tutor.database.store_deck_card(
db, deck_id, cards[0].oracle_id, int(groups["quantity"]) cursor, deck_id, cards[0].oracle_id, int(groups["quantity"])
) )
await db.commit() await conn.commit()
tornado.ioloop.IOLoop.current().run_sync(import_deck) tornado.ioloop.IOLoop.current().run_sync(import_deck)