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
sqlite3 ${TUTOR_DATABASE} <./tables.sql
exec tutor $@

View file

@ -5,7 +5,6 @@ import logging
import os.path
import re
import aiosqlite
import click
import httpx
import humanize
@ -116,25 +115,28 @@ def import_deck(ctx, filename, name):
name = name or os.path.splitext(os.path.basename(filename))[0]
async def import_deck():
async with aiosqlite.connect(ctx.obj["database"]) as db:
deck_id = await tutor.database.store_deck(db, name)
with tqdm.tqdm(lines) as bar:
bar.write(f"Importing deck '{name}' [{deck_id}]")
for line in bar:
if match := line_pattern.match(line.strip()):
groups = match.groupdict()
cards = await tutor.database.advanced_search(
db,
tutor.search.Search(
[tutor.search.Name(text=groups["name"])]
),
limit=1,
)
if cards:
await tutor.database.store_deck_card(
db, deck_id, cards[0].oracle_id, int(groups["quantity"])
async with await psycopg.AsyncConnection.connect(
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:
bar.write(f"Importing deck '{name}' [{deck_id}]")
for line in bar:
if match := line_pattern.match(line.strip()):
groups = match.groupdict()
cards = await tutor.database.advanced_search(
cursor,
tutor.search.Search(
[tutor.search.Name(text=groups["name"])]
),
limit=1,
)
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)