From 73863c8230df455dc433f8f10e2cee5edd8d8e58 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Fri, 14 Apr 2023 12:50:39 +0300 Subject: auth: Disconnect module from flask application To avoid application context errors in external scripts, disconnect the `gn3.auth.db` module from the `flask.current_app` dependency. --- gn3/auth/db.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'gn3') diff --git a/gn3/auth/db.py b/gn3/auth/db.py index 3b1844a..b8c91e9 100644 --- a/gn3/auth/db.py +++ b/gn3/auth/db.py @@ -1,12 +1,11 @@ """Handle connection to auth database.""" import sqlite3 +import logging import contextlib from typing import Any, Callable, Iterator, Protocol import traceback -from flask import current_app as app - class DbConnection(Protocol): """Type annotation for a generic database connection object.""" def cursor(self) -> Any: @@ -51,14 +50,13 @@ def connection(db_path: str, row_factory: Callable = sqlite3.Row) -> Iterator[Db """Create the connection to the auth database.""" conn = sqlite3.connect(db_path) conn.row_factory = row_factory - if app.config["DEBUG"]: - conn.set_trace_callback(app.logger.debug) + conn.set_trace_callback(logging.debug) conn.execute("PRAGMA foreign_keys = ON") try: yield conn except sqlite3.Error as exc: conn.rollback() - app.logger.debug(traceback.format_exc()) + logging.debug(traceback.format_exc()) raise exc finally: conn.commit() @@ -72,7 +70,7 @@ def cursor(conn: DbConnection) -> Iterator[DbCursor]: yield cur except sqlite3.Error as exc: conn.rollback() - app.logger.debug(traceback.format_exc()) + logging.debug(traceback.format_exc()) raise exc finally: conn.commit() -- cgit v1.2.3