summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Turner <jturner.usa@gmail.com>2025-09-23 21:46:56 -0400
committerJohn Turner <jturner.usa@gmail.com>2025-09-23 21:48:12 -0400
commitab00a851708df208d5151d4804afe9f64e296993 (patch)
treee6411fece2615b19a27afb9450325c12750b5e24
parent3b14ad173a9d8ca5c49921c8f6676f215c3dbade (diff)
downloadpypaste-ab00a851708df208d5151d4804afe9f64e296993.tar.gz
create PasteInfo struct and replace read_row with read_paste_infofeature/new-keys
-rw-r--r--pypaste/server/__init__.py13
-rw-r--r--pypaste/server/s3/__init__.py8
-rw-r--r--pypaste/server/sqlite/__init__.py8
3 files changed, 16 insertions, 13 deletions
diff --git a/pypaste/server/__init__.py b/pypaste/server/__init__.py
index 2ecd975..8b8a7f0 100644
--- a/pypaste/server/__init__.py
+++ b/pypaste/server/__init__.py
@@ -18,7 +18,7 @@ import aiosqlite
from aiohttp import web
from datetime import datetime
from dataclasses import dataclass
-from typing import Optional, Tuple
+from typing import Optional
from pypaste import log_error, log_warning, log_info
from pygments import highlight
from pygments.lexers import guess_lexer, get_lexer_by_name
@@ -89,6 +89,13 @@ class Paste:
@dataclass
+class PasteInfo:
+ dt: datetime
+ size: int
+ syntax: Optional[str]
+
+
+@dataclass
class Storage:
connection: aiosqlite.Connection
@@ -116,14 +123,14 @@ class Storage:
async def vacuum(self, size: int) -> None:
pass
- async def read_row(self, key: Key) -> Optional[Tuple[datetime, int, Optional[str]]]:
+ async def read_paste_info(self, key: Key) -> Optional[PasteInfo]:
async with self.connection.execute(
"select pastes.datetime,pastes.size,pastes.syntax from pastes where pastes.key=? limit 1",
(key.data,),
) as cursor:
match await cursor.fetchone():
case [str(dt), int(size), syntax]:
- return (datetime.fromisoformat(dt), size, syntax)
+ return PasteInfo(datetime.fromisoformat(dt), size, syntax)
case None:
return None
case _:
diff --git a/pypaste/server/s3/__init__.py b/pypaste/server/s3/__init__.py
index 23be1ee..c9c8297 100644
--- a/pypaste/server/s3/__init__.py
+++ b/pypaste/server/s3/__init__.py
@@ -64,11 +64,9 @@ class S3(Storage):
if not await self.exists(key):
return None
- row = await self.read_row(key)
+ info = await self.read_paste_info(key)
- assert row is not None
-
- (dt, size, syntax) = row
+ assert info is not None
data = await self.bucket.get(key.data.hex())
@@ -79,7 +77,7 @@ class S3(Storage):
text = await asyncio.to_thread(decompress)
- return Paste(dt, syntax, text)
+ return Paste(info.dt, info.syntax, text)
async def delete(self, key: Key) -> None:
await self.connection.execute("delete from pastes where key=?", (key.data,))
diff --git a/pypaste/server/sqlite/__init__.py b/pypaste/server/sqlite/__init__.py
index ee75d91..7e490aa 100644
--- a/pypaste/server/sqlite/__init__.py
+++ b/pypaste/server/sqlite/__init__.py
@@ -50,18 +50,16 @@ class Sqlite(Storage):
case _:
raise Exception("unreachable")
- row = await self.read_row(key)
+ info = await self.read_paste_info(key)
- assert row is not None
-
- (dt, size, syntax) = row
+ assert info is not None
def decompress():
return zstandard.decompress(data).decode()
text = await asyncio.to_thread(decompress)
- return Paste(dt, syntax, text)
+ return Paste(info.dt, info.syntax, text)
async def delete(self, key: Key) -> None:
await self.connection.execute("delete from pastes where key=?", (key.data,))