From 10ea2eef3affd6e801db070c62430a5afabe1053 Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Thu, 20 May 2021 17:34:46 +0200 Subject: [PATCH] [enh] utils: implement a context manager for a SQLite cursor usage in a with statement [2]:: with SQLiteCursor('test.db') as cur: print(cur.execute('select sqlite_version();').fetchall()[0][0]) [1] https://docs.python.org/3/library/stdtypes.html#context-manager-types [2] https://docs.python.org/3/reference/compound_stmts.html#with Signed-off-by: Markus Heiser --- searx/engines/sqlite.py | 31 ++----------------------------- searx/utils.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/searx/engines/sqlite.py b/searx/engines/sqlite.py index af7eca3cd..e9d5ecfde 100644 --- a/searx/engines/sqlite.py +++ b/searx/engines/sqlite.py @@ -6,9 +6,8 @@ """ -import sqlite3 - from searx import logger +from searx.utils import SQLiteCursor logger = logger.getChild('SQLite engine') @@ -19,32 +18,6 @@ limit = 10 paging = True result_template = 'key-value.html' -class SQLiteDB: - """ - Implements a `Context Manager`_ for a SQLite. - - usage:: - - with SQLiteDB('test.db') as cur: - print(cur.execute('select sqlite_version();').fetchall()[0][0]) - - .. _Context Manager: https://docs.python.org/3/library/stdtypes.html#context-manager-types - """ - - def __init__(self, db): - self.database = db - self.connect = None - - def __enter__(self): - self.connect = sqlite3.connect(self.database) - self.connect.row_factory = sqlite3.Row - return self.connect.cursor() - - def __exit__(self, exc_type, exc_val, exc_tb): - if exc_type is None: - self.connect.commit() - self.connect.close() - def init(engine_settings): if 'query_str' not in engine_settings: raise ValueError('query_str cannot be empty') @@ -64,7 +37,7 @@ def search(query, params): } query_to_run = query_str + ' LIMIT :limit OFFSET :offset' - with SQLiteDB(database) as cur: + with SQLiteCursor(database) as cur: cur.execute(query_to_run, query_params) col_names = [cn[0] for cn in cur.description] diff --git a/searx/utils.py b/searx/utils.py index c28243e37..0f2225823 100644 --- a/searx/utils.py +++ b/searx/utils.py @@ -9,6 +9,7 @@ import sys import re import importlib +import sqlite3 from numbers import Number from os.path import splitext, join @@ -612,3 +613,36 @@ def eval_xpath_getindex(elements, xpath_spec, index, default=NOTSET): # to record xpath_spec raise SearxEngineXPathException(xpath_spec, 'index ' + str(index) + ' not found') return default + +class SQLiteCursor: + """Implements a `Context Manager`_ for a SQLite *cursor* + + Returns :py:obj:`sqlite3.Cursor` on entering the runtime context of a `with + statement`_. + + usage:: + + with SQLiteCursor('test.db') as cur: + print(cur.execute('select sqlite_version();').fetchall()[0][0]) + + .. _Context Manager: https://docs.python.org/3/library/stdtypes.html#context-manager-types + .. _with statement: https://docs.python.org/3/reference/compound_stmts.html#with + + """ + + def __init__(self, db): + self.database = db + self.connect = None + self.cursor = None + + def __enter__(self): + self.connect = sqlite3.connect(self.database) + self.connect.row_factory = sqlite3.Row + self.cursor = self.connect.cursor() + return self.cursor + + def __exit__(self, exc_type, exc_val, exc_tb): + if exc_type is None: + self.connect.commit() + self.cursor.close() + self.connect.close()