on master help sign in
Merge pull request #2237 from dalf/mod-engines-init
584760cAlexandre Flament
docs/conf.py−3
@@ -28,11 +28,8 @@ numfig = True
28exclude_patterns = ['build-templates/*.rst']
28exclude_patterns = ['build-templates/*.rst']
29
29
30from searx import webapp
30from searx import webapp
31−import searx.engines
32−searx.engines.initialize_engines(searx.settings['engines'])
33jinja_contexts = {
31jinja_contexts = {
34 'webapp': dict(**webapp.__dict__),
32 'webapp': dict(**webapp.__dict__),
35− 'engines': searx.engines.engines
36}
33}
37
34
38# usage:: lorem :patch:`f373169` ipsum
35# usage:: lorem :patch:`f373169` ipsum
searx/webapp.py+16−4
@@ -65,7 +65,7 @@ from searx.engines import (
65from searx.webutils import (
65from searx.webutils import (
66 UnicodeWriter, highlight_content, get_resources_directory,
66 UnicodeWriter, highlight_content, get_resources_directory,
67 get_static_files, get_result_templates, get_themes,
67 get_static_files, get_result_templates, get_themes,
68− prettify_url, new_hmac
68+ prettify_url, new_hmac, is_flask_run_cmdline
69)
69)
70from searx.webadapter import get_search_query_from_webapp, get_selected_categories
70from searx.webadapter import get_search_query_from_webapp, get_selected_categories
71from searx.utils import html_to_text, gen_useragent, dict_subset, match_language
71from searx.utils import html_to_text, gen_useragent, dict_subset, match_language
@@ -114,9 +114,21 @@ app.jinja_env.lstrip_blocks = True
114app.jinja_env.add_extension('jinja2.ext.loopcontrols')
114app.jinja_env.add_extension('jinja2.ext.loopcontrols')
115app.secret_key = settings['server']['secret_key']
115app.secret_key = settings['server']['secret_key']
116
116
117−if not searx_debug \
117+# see https://flask.palletsprojects.com/en/1.1.x/cli/
118+# True if "FLASK_APP=searx/webapp.py FLASK_ENV=development flask run"
119+flask_run_development = \
120+ os.environ.get("FLASK_APP") is not None\
121+ and os.environ.get("FLASK_ENV") == 'development'\
122+ and is_flask_run_cmdline()
123+
124+# True if reload feature is activated of werkzeug, False otherwise (including uwsgi, etc..)
125+# __name__ != "__main__" if searx.webapp is imported (make test, make docs, uwsgi...)
126+# see run() at the end of this file : searx_debug activates the reload feature.
127+werkzeug_reloader = flask_run_development or (searx_debug and __name__ == "__main__")
128+
129+# initialize the engines except on the first run of the werkzeug server.
130+if not werkzeug_reloader\
118− or os.environ.get("WERKZEUG_RUN_MAIN") == "true" \
131+ or (werkzeug_reloader and os.environ.get("WERKZEUG_RUN_MAIN") == "true"):
119− or os.environ.get('UWSGI_ORIGINAL_PROC_NAME') is not None:
120 initialize_engines(settings['engines'])
132 initialize_engines(settings['engines'])
121
133
122babel = Babel(app)
134babel = Babel(app)
searx/webutils.py+16
@@ -4,6 +4,7 @@ import csv
4import hashlib
4import hashlib
5import hmac
5import hmac
6import re
6import re
7+import inspect
7
8
8from io import StringIO
9from io import StringIO
9from codecs import getincrementalencoder
10from codecs import getincrementalencoder
@@ -127,3 +128,18 @@ def highlight_content(content, query):
127 content, flags=re.I | re.U)
128 content, flags=re.I | re.U)
128
129
129 return content
130 return content
131+
132+
133+def is_flask_run_cmdline():
134+ """Check if the application was started using "flask run" command line
135+
136+ Inspect the callstack.
137+ See https://github.com/pallets/flask/blob/master/src/flask/__main__.py
138+
139+ Returns:
140+ bool: True if the application was started using "flask run".
141+ """
142+ frames = inspect.stack()
143+ if len(frames) < 2:
144+ return False
145+ return frames[-2].filename.endswith('flask/cli.py')