[pyright:basic] searx.webapp

This commit is contained in:
Martin Fischer 2022-01-24 11:44:18 +01:00
parent 640c404844
commit b767752d0c
2 changed files with 23 additions and 9 deletions

View File

@ -7,6 +7,7 @@ import threading
import concurrent.futures import concurrent.futures
from types import MethodType from types import MethodType
from timeit import default_timer from timeit import default_timer
from typing import Iterable, Tuple
import httpx import httpx
import anyio import anyio
@ -210,7 +211,7 @@ def _close_response_method(self):
continue continue
def stream(method, url, **kwargs): def stream(method, url, **kwargs) -> Tuple[httpx.Response, Iterable[bytes]]:
"""Replace httpx.stream. """Replace httpx.stream.
Usage: Usage:

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
# SPDX-License-Identifier: AGPL-3.0-or-later # SPDX-License-Identifier: AGPL-3.0-or-later
# lint: pylint # lint: pylint
# pyright: basic
"""WebbApp """WebbApp
""" """
@ -33,11 +34,11 @@ from flask import (
Flask, Flask,
render_template, render_template,
url_for, url_for,
Response,
make_response, make_response,
redirect, redirect,
send_from_directory, send_from_directory,
) )
from flask.wrappers import Response
from flask.ctx import has_request_context from flask.ctx import has_request_context
from flask.json import jsonify from flask.json import jsonify
@ -255,7 +256,13 @@ flask_babel.get_translations = _get_translations
@babel.localeselector @babel.localeselector
def get_locale(): def get_locale():
locale = request.preferences.get_value('locale') if has_request_context() else 'en' locale = 'en'
if has_request_context():
value = request.preferences.get_value('locale')
if value:
locale = value
if locale == 'oc': if locale == 'oc':
request.form['use-translation'] = 'oc' request.form['use-translation'] = 'oc'
locale = 'fr_FR' locale = 'fr_FR'
@ -310,6 +317,7 @@ def code_highlighter(codelines, language=None):
html_code = '' html_code = ''
tmp_code = '' tmp_code = ''
last_line = None last_line = None
line_code_start = None
# parse lines # parse lines
for line, code in codelines: for line, code in codelines:
@ -351,9 +359,11 @@ def get_current_theme_name(override: str = None) -> str:
if override and (override in themes or override == '__common__'): if override and (override in themes or override == '__common__'):
return override return override
theme_name = request.args.get('theme', request.preferences.get_value('theme')) theme_name = request.args.get('theme', request.preferences.get_value('theme'))
if theme_name not in themes:
theme_name = default_theme if theme_name and theme_name in themes:
return theme_name return theme_name
return default_theme
def get_result_template(theme_name: str, template_name: str): def get_result_template(theme_name: str, template_name: str):
@ -380,7 +390,7 @@ def proxify(url: str):
if not settings.get('result_proxy'): if not settings.get('result_proxy'):
return url return url
url_params = dict(mortyurl=url.encode()) url_params = dict(mortyurl=url)
if settings['result_proxy'].get('key'): if settings['result_proxy'].get('key'):
url_params['mortyhash'] = hmac.new(settings['result_proxy']['key'], url.encode(), hashlib.sha256).hexdigest() url_params['mortyhash'] = hmac.new(settings['result_proxy']['key'], url.encode(), hashlib.sha256).hexdigest()
@ -1137,10 +1147,13 @@ def image_proxy():
except httpx.HTTPError: except httpx.HTTPError:
logger.exception('HTTP error on closing') logger.exception('HTTP error on closing')
assert resp is not None
def close_stream(): def close_stream():
nonlocal resp, stream nonlocal resp, stream
try: try:
resp.close() if resp:
resp.close()
del resp del resp
del stream del stream
except httpx.HTTPError as e: except httpx.HTTPError as e:
@ -1207,7 +1220,7 @@ def stats():
reverse, key_name, default_value = STATS_SORT_PARAMETERS[sort_order] reverse, key_name, default_value = STATS_SORT_PARAMETERS[sort_order]
def get_key(engine_stat): def get_key(engine_stat):
reliability = engine_reliabilities.get(engine_stat['name']).get('reliablity', 0) reliability = engine_reliabilities.get(engine_stat['name'], {}).get('reliablity', 0)
reliability_order = 0 if reliability else 1 reliability_order = 0 if reliability else 1
if key_name == 'reliability': if key_name == 'reliability':
key = reliability key = reliability