From 38b8178cce160b3d2e6c8b3256c7655f4d0a27cd Mon Sep 17 00:00:00 2001 From: "user@node5.net" Date: Mon, 15 Jun 2026 21:24:56 +0200 Subject: internalize needed files for starting the program --- src/db_handler.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/db_handler.py') diff --git a/src/db_handler.py b/src/db_handler.py index 471154a..b063692 100644 --- a/src/db_handler.py +++ b/src/db_handler.py @@ -1,13 +1,23 @@ import logging -from dataclasses import dataclass - import sqlite3 +import os logger = logging.getLogger(__name__) # Set the logger name, to the name of the module class DBHandler: + def __init__(self, db_init_script_path: os.PathLike, db_path: str = "blog.node5.net.db"): + self.db_init_script_path = db_init_script_path + self.db_path = db_path + if not os.path.exists(self.db_path): + script = open(self.db_init_script_path, "r", encoding="utf-8").read() + with self.con_factory() as con: + con.executescript(script) + con.commit() + logger.info("Database initialized from %s", self.db_init_script_path) + + def con_factory(self) -> sqlite3.Connection: con = sqlite3.connect("blog.node5.net.db") return con -- cgit v1.3.1