diff options
author | John Turner <jturner.usa@gmail.com> | 2025-09-07 00:40:08 -0400 |
---|---|---|
committer | John Turner <jturner.usa@gmail.com> | 2025-09-07 00:40:08 -0400 |
commit | 2629092ed70901325bf984315a0d579e931f4d01 (patch) | |
tree | 0076026905c6e519e9356b05bddc7ebb38e8fa21 | |
parent | 368e8d9faa92284249d7905ab9e2a53b27a4c8ca (diff) | |
download | pypaste-2629092ed70901325bf984315a0d579e931f4d01.tar.gz |
create database module
-rw-r--r-- | pypaste/__main__.py | 74 | ||||
-rw-r--r-- | pypaste/database.py | 78 |
2 files changed, 79 insertions, 73 deletions
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 @@ -87,78 +87,6 @@ def generate_key(words: List[str], length: int) -> str: @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 content_length_max_bytes: int 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 |