summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pypaste/__main__.py74
-rw-r--r--pypaste/database.py78
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