diff options
author | John Turner <jturner.usa@gmail.com> | 2025-09-06 23:00:50 -0400 |
---|---|---|
committer | John Turner <jturner.usa@gmail.com> | 2025-09-06 23:32:22 -0400 |
commit | 6f80113b2f3da9047822eab18d46e01c1ed70ecc (patch) | |
tree | b4614464a004b687cdbef350875c4695d86055bc | |
parent | 9beca5fcb35a0bb4b654f1fac92dc53b8f117a1f (diff) | |
download | pypaste-6f80113b2f3da9047822eab18d46e01c1ed70ecc.tar.gz |
remove aiosqlite
-rw-r--r-- | pypaste/__main__.py | 82 |
1 files changed, 46 insertions, 36 deletions
diff --git a/pypaste/__main__.py b/pypaste/__main__.py index a2a8de2..98de65f 100644 --- a/pypaste/__main__.py +++ b/pypaste/__main__.py @@ -17,8 +17,8 @@ import sys import os import asyncio import secrets +import sqlite3 import aiohttp -import aiosqlite import zstandard from . import s3 from socket import socket, AF_UNIX, SOCK_STREAM @@ -80,48 +80,62 @@ class Database: self.path = path async def insert(self, paste: PasteRow): - async with aiosqlite.connect(self.path) as db: - await db.execute( - "insert into pastes values(?, ?, ?, ?)", - ( - paste.key, - paste.date.isoformat(), - paste.size, - paste.syntax, - ), - ) + def do(): + with sqlite3.connect(self.path) as connection: + connection.execute( + "insert into pastes values(?, ?, ?, ?)", + ( + paste.key, + paste.date.isoformat(), + paste.size, + paste.syntax, + ), + ) - await db.commit() + await asyncio.to_thread(do) async def delete(self, key: str): - async with aiosqlite.connect(self.path) as db: - await db.execute("delete from pastes where key=?", (key,)) + def do(): + with sqlite3.connect(self.path) as connection: + connection.execute("delete from pastes where key=?", (key,)) - await db.commit() + await asyncio.to_thread(do) async def exists(self, key: str) -> bool: - async with aiosqlite.connect(self.path) as db: - result = await db.execute("select 1 from pastes where pastes.key=?", (key,)) + def do(): + with sqlite3.connect(self.path) as connection: + return ( + connection.execute( + "select 1 from pastes where pastes.key=?", (key,) + ).fetchone() + is not None + ) - return await result.fetchone() is not None + return await asyncio.to_thread(do) async def oldest(self) -> Optional[str]: - async with aiosqlite.connect(self.path) as db: - result = await db.execute( - "select pastes.key from pastes order by pastes.datetime limit 1", - ) - - row = await result.fetchone() - - return row[0] + def do(): + with sqlite3.connect(self.path) as connection: + return connection.execute( + "select pastes.key from pastes order by pastes.datetime limit 1", + ).fetchone() + + match await asyncio.to_thread(do): + case str(key): + return key + case _: + return None async def storage_use(self) -> Optional[int]: - async with aiosqlite.connect(self.path) as db: - result = await db.execute("select sum(pastes.size) from pastes") - - row = await result.fetchone() + def do(): + with sqlite3.connect(self.path) as connection: + return connection.execute("select sum(pastes.size) from pastes") - return row[0] + match asyncio.to_thread(do): + case int(use): + return use + case _: + return None @dataclass @@ -355,11 +369,7 @@ async def main() -> int: print(f"{args.database} does not exist or is not a file", file=sys.stderr) return 1 - try: - database = Database(args.database) - except Exception as e: - print(f"failed to open database: {e}", file=sys.stderr) - return 1 + database = Database(args.database) bucket = s3.Bucket( args.endpoint, |