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,25 +115,28 @@ 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
with tqdm.tqdm(lines) as bar: ) as conn:
bar.write(f"Importing deck '{name}' [{deck_id}]") async with conn.cursor() as cursor:
for line in bar: deck_id = await tutor.database.store_deck(cursor, name)
if match := line_pattern.match(line.strip()): with tqdm.tqdm(lines) as bar:
groups = match.groupdict() bar.write(f"Importing deck '{name}' [{deck_id}]")
cards = await tutor.database.advanced_search( for line in bar:
db, if match := line_pattern.match(line.strip()):
tutor.search.Search( groups = match.groupdict()
[tutor.search.Name(text=groups["name"])] cards = await tutor.database.advanced_search(
), cursor,
limit=1, tutor.search.Search(
) [tutor.search.Name(text=groups["name"])]
if cards: ),
await tutor.database.store_deck_card( limit=1,
db, deck_id, cards[0].oracle_id, int(groups["quantity"])
) )
await db.commit() if cards:
await tutor.database.store_deck_card(
cursor, deck_id, cards[0].oracle_id, int(groups["quantity"])
)
await conn.commit()
tornado.ioloop.IOLoop.current().run_sync(import_deck) tornado.ioloop.IOLoop.current().run_sync(import_deck)