From f05229c7b90ff11ce551c89bc37c9e139cecede8 Mon Sep 17 00:00:00 2001 From: John Turner Date: Sun, 7 Sep 2025 19:18:06 -0400 Subject: put database.py back in the top level package dir --- meson.build | 6 +--- pypaste/database.py | 80 ++++++++++++++++++++++++++++++++++++++++++++ pypaste/database/__init__.py | 80 -------------------------------------------- 3 files changed, 81 insertions(+), 85 deletions(-) create mode 100644 pypaste/database.py delete mode 100644 pypaste/database/__init__.py diff --git a/meson.build b/meson.build index 467f2c3..646a141 100644 --- a/meson.build +++ b/meson.build @@ -4,10 +4,6 @@ python = import('python').find_installation( modules: ['pygments', 'zstandard', 'aiohttp', 'bozo4'], ) -sources = files( - 'pypaste/__main__.py', - 'pypaste/database/__init__.py', - 'pypaste/s3.py', -) +sources = files('pypaste/__main__.py', 'pypaste/database.py', 'pypaste/s3.py') python.install_sources(sources, preserve_path: true) diff --git a/pypaste/database.py b/pypaste/database.py new file mode 100644 index 0000000..186259b --- /dev/null +++ b/pypaste/database.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 diff --git a/pypaste/database/__init__.py b/pypaste/database/__init__.py deleted file mode 100644 index 186259b..0000000 --- a/pypaste/database/__init__.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 -- cgit v1.2.3