Use callbacks for database connections

This commit is contained in:
XANTRONIX Development 2024-11-26 16:24:32 -05:00
parent 613134d63f
commit 1e1ffabd72
2 changed files with 10 additions and 7 deletions

View file

@ -1,6 +1,7 @@
import enum
from nntp.tiny.db import Database
from typing import Callable
from nntp.tiny.newsgroup import Newsgroup
class ServerCapability(enum.Flag):
@ -9,13 +10,15 @@ class ServerCapability(enum.Flag):
POST = enum.auto()
class Server():
def __init__(self, db: Database):
self.db = db
self.capabilities = ServerCapability.NONE
self.newsgroups = dict()
def __init__(self, connect_to_db: Callable):
self.connect_to_db = connect_to_db
self.capabilities = ServerCapability.NONE
self.newsgroups = dict()
self._init_newsgroups()
def _init_newsgroups(self):
for newsgroup in self.db.query(Newsgroup).each():
db = self.connect_to_db()
for newsgroup in db.query(Newsgroup).each():
self.newsgroups[newsgroup.name.casefold()] = newsgroup

View file

@ -106,7 +106,7 @@ class Session():
def __init__(self, server: Server, sock: socket.socket):
self.server: Server = server
self.db: Database = server.db
self.db: Database = server.connect_to_db()
self.sock: socket.socket = sock
self.buf: LineBuffer = LineBuffer()
self.state: SessionState = SessionState.ACTIVE