From 983afa5cc6790e2415b71c4fde5602380df44936 Mon Sep 17 00:00:00 2001 From: John Turner Date: Sun, 7 Sep 2025 18:46:05 -0400 Subject: fix imports --- pypaste/__main__.py | 4 +-- pypaste/database.py | 80 -------------------------------------------- pypaste/database/__init__.py | 80 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 82 deletions(-) delete mode 100644 pypaste/database.py create mode 100644 pypaste/database/__init__.py diff --git a/pypaste/__main__.py b/pypaste/__main__.py index 4b18c9d..d002b5b 100644 --- a/pypaste/__main__.py +++ b/pypaste/__main__.py @@ -19,8 +19,8 @@ import asyncio import secrets import aiohttp import zstandard -from . import s3 -from .database import Database, PasteRow +from pypaste import s3 +from pypaste.database import Database, PasteRow from socket import socket, AF_UNIX, SOCK_STREAM from hashlib import sha256 from argparse import ArgumentParser diff --git a/pypaste/database.py b/pypaste/database.py deleted file mode 100644 index 186259b..0000000 --- a/pypaste/database.py +++ /dev/null @@ -1,80 +0,0 @@ -import asyncio -import sqlite3 -from typing import Optional -from datetime import datetime -from pathlib import Path -from dataclasses import dataclass - - -@dataclass -class PasteRow: - key: str - date: datetime - size: int - syntax: Optional[str] - - -class Database: - - def __init__(self, path: Path): - self.path = path - - async def insert(self, paste: PasteRow): - 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 asyncio.to_thread(do) - - async def delete(self, key: str): - def do(): - with sqlite3.connect(self.path) as connection: - connection.execute("delete from pastes where key=?", (key,)) - - await asyncio.to_thread(do) - - async def exists(self, key: str) -> bool: - 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 asyncio.to_thread(do) - - async def oldest(self) -> Optional[str]: - 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]: - def do(): - with sqlite3.connect(self.path) as connection: - return connection.execute( - "select sum(pastes.size) from pastes" - ).fetchone() - - match asyncio.to_thread(do): - case int(use): - return use - case _: - return None diff --git a/pypaste/database/__init__.py b/pypaste/database/__init__.py new file mode 100644 index 0000000..186259b --- /dev/null +++ b/pypaste/database/__init__.py @@ -0,0 +1,80 @@ +import asyncio +import sqlite3 +from typing import Optional +from datetime import datetime +from pathlib import Path +from dataclasses import dataclass + + +@dataclass +class PasteRow: + key: str + date: datetime + size: int + syntax: Optional[str] + + +class Database: + + def __init__(self, path: Path): + self.path = path + + async def insert(self, paste: PasteRow): + 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 asyncio.to_thread(do) + + async def delete(self, key: str): + def do(): + with sqlite3.connect(self.path) as connection: + connection.execute("delete from pastes where key=?", (key,)) + + await asyncio.to_thread(do) + + async def exists(self, key: str) -> bool: + 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 asyncio.to_thread(do) + + async def oldest(self) -> Optional[str]: + 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]: + def do(): + with sqlite3.connect(self.path) as connection: + return connection.execute( + "select sum(pastes.size) from pastes" + ).fetchone() + + match asyncio.to_thread(do): + case int(use): + return use + case _: + return None -- cgit v1.2.3