From 2629092ed70901325bf984315a0d579e931f4d01 Mon Sep 17 00:00:00 2001 From: John Turner Date: Sun, 7 Sep 2025 00:40:08 -0400 Subject: create database module --- pypaste/__main__.py | 74 +------------------------------------------------- pypaste/database.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 73 deletions(-) create mode 100644 pypaste/database.py diff --git a/pypaste/__main__.py b/pypaste/__main__.py index 0ac25fd..8968166 100644 --- a/pypaste/__main__.py +++ b/pypaste/__main__.py @@ -17,10 +17,10 @@ import sys import os import asyncio import secrets -import sqlite3 import aiohttp import zstandard from . import s3 +from .database import Database, PasteRow from socket import socket, AF_UNIX, SOCK_STREAM from hashlib import sha256 from argparse import ArgumentParser @@ -86,78 +86,6 @@ def generate_key(words: List[str], length: int) -> str: return "-".join(word for word in choices).lower() -@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") - - match asyncio.to_thread(do): - case int(use): - return use - case _: - return None - - @dataclass class AppConfig: site: str diff --git a/pypaste/database.py b/pypaste/database.py new file mode 100644 index 0000000..dfdd699 --- /dev/null +++ b/pypaste/database.py @@ -0,0 +1,78 @@ +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") + + match asyncio.to_thread(do): + case int(use): + return use + case _: + return None -- cgit v1.2.3