1
0
mirror of https://github.com/searxng/searxng.git synced 2024-11-14 00:30:15 +01:00
searxng/searx/engines/sqlite.py
Markus Heiser 443bf35e09 [pylint] fix global-variable-not-assigned issues
If there is no write access, there is no need for global.  Remove global
statement if there is no assignment.

global-variable-not-assigned:
  Using global for names but no assignment is done Used when a variable is
  defined through the "global" statement but no assignment to this variable is
  done.

In Pylint 2.11 the global-variable-not-assigned checker now catches global
variables that are never reassigned in a local scope and catches (reassigned)
functions [1][2]

[1] https://pylint.pycqa.org/en/latest/whatsnew/2.11.html
[2] https://github.com/PyCQA/pylint/issues/1375

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
2021-09-17 10:14:27 +02:00

68 lines
1.8 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
# lint: pylint
"""SQLite database (Offline)
"""
import sqlite3
import contextlib
engine_type = 'offline'
database = ""
query_str = ""
limit = 10
paging = True
result_template = 'key-value.html'
def init(engine_settings):
if 'query_str' not in engine_settings:
raise ValueError('query_str cannot be empty')
if not engine_settings['query_str'].lower().startswith('select '):
raise ValueError('only SELECT query is supported')
@contextlib.contextmanager
def sqlite_cursor():
"""Implements a `Context Manager`_ for a :py:obj:`sqlite3.Cursor`.
Open database in read only mode: if the database doesn't exist.
The default mode creates an empty file on the file system.
see:
* https://docs.python.org/3/library/sqlite3.html#sqlite3.connect
* https://www.sqlite.org/uri.html
"""
uri = 'file:' + database + '?mode=ro'
with contextlib.closing(sqlite3.connect(uri, uri=True)) as connect:
connect.row_factory = sqlite3.Row
with contextlib.closing(connect.cursor()) as cursor:
yield cursor
def search(query, params):
results = []
query_params = {
'query': query,
'wildcard': r'%' + query.replace(' ', r'%') + r'%',
'limit': limit,
'offset': (params['pageno'] - 1) * limit
}
query_to_run = query_str + ' LIMIT :limit OFFSET :offset'
with sqlite_cursor() as cur:
cur.execute(query_to_run, query_params)
col_names = [cn[0] for cn in cur.description]
for row in cur.fetchall():
item = dict( zip(col_names, map(str, row)) )
item['template'] = result_template
logger.debug("append result --> %s", item)
results.append(item)
return results