We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[Feature] Raise error when the engine doesn't exist
Raise an error when an engine doesn't exist and there is for example an old parameter that disabled a now removed engine.

See https://github.com/searxng/searxng/issues/1951#issuecomment-1324872496 for more info.

-----

<!-- PLEASE FILL THESE FIELDS, IT REALLY HELPS THE MAINTAINERS OF SearXNG -->

**Version of SearXNG, commit number if you are using on master branch and stipulate if you forked SearXNG**

Commit https://github.com/searxng/searxng/commit/3a765113eefc7ac0b1ee94e9a0ac0db8069ec336

**How did you install SearXNG?**
1. Git clone
2. pip install -r requirements.txt
3. `python -m searx.webapp`

**How To Reproduce**
Run searxng with this configuration:
```yaml
engines:
  - name: duckduckgo
    disabled: true
general:
  debug: true
server:
  secret_key: secret
use_default_settings: true
```

And you will get this error:
```
Traceback (most recent call last):
  File "/opt/python/latest/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/opt/python/latest/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/workspaces/searx-custom/searxng2/searx/webapp.py", line 1390, in <module>
    search_initialize(enable_checker=True, check_network=True, enable_metrics=settings['general']['enable_metrics'])
  File "/workspaces/searx-custom/searxng2/searx/search/__init__.py", line 30, in initialize
    load_engines(settings_engines)
  File "/workspaces/searx-custom/searxng2/searx/engines/__init__.py", line 298, in load_engines
    engine = load_engine(engine_data)
  File "/workspaces/searx-custom/searxng2/searx/engines/__init__.py", line 121, in load_engine
    engine_module = engine_data['engine']
KeyError: 'engine'
```

**Expected behavior**
Searxng should work fine.

**Screenshots & Logs**
<!-- If applicable, add screenshots, logs to help explain your problem. -->

**Additional context**
<!-- Add any other context about the problem here. -->


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `searx/engines/__init__.py`
Content:
```
1 # SPDX-License-Identifier: AGPL-3.0-or-later
2 # lint: pylint
3 """This module implements the engine loader.
4 
5 Load and initialize the ``engines``, see :py:func:`load_engines` and register
6 :py:obj:`engine_shortcuts`.
7 
8 usage::
9 
10     load_engines( settings['engines'] )
11 
12 """
13 
14 import sys
15 import copy
16 from typing import Dict, List, Optional
17 
18 from os.path import realpath, dirname
19 from babel.localedata import locale_identifiers
20 from searx import logger, settings
21 from searx.data import ENGINES_LANGUAGES
22 from searx.network import get
23 from searx.utils import load_module, match_language, gen_useragent
24 
25 
26 logger = logger.getChild('engines')
27 ENGINE_DIR = dirname(realpath(__file__))
28 BABEL_LANGS = [
29     lang_parts[0] + '-' + lang_parts[-1] if len(lang_parts) > 1 else lang_parts[0]
30     for lang_parts in (lang_code.split('_') for lang_code in locale_identifiers())
31 ]
32 ENGINE_DEFAULT_ARGS = {
33     "engine_type": "online",
34     "inactive": False,
35     "disabled": False,
36     "timeout": settings["outgoing"]["request_timeout"],
37     "shortcut": "-",
38     "categories": ["general"],
39     "supported_languages": [],
40     "language_aliases": {},
41     "paging": False,
42     "safesearch": False,
43     "time_range_support": False,
44     "enable_http": False,
45     "using_tor_proxy": False,
46     "display_error_messages": True,
47     "send_accept_language_header": False,
48     "tokens": [],
49     "about": {},
50 }
51 # set automatically when an engine does not have any tab category
52 OTHER_CATEGORY = 'other'
53 
54 
55 class Engine:  # pylint: disable=too-few-public-methods
56     """This class is currently never initialized and only used for type hinting."""
57 
58     name: str
59     engine: str
60     shortcut: str
61     categories: List[str]
62     supported_languages: List[str]
63     about: dict
64     inactive: bool
65     disabled: bool
66     language_support: bool
67     paging: bool
68     safesearch: bool
69     time_range_support: bool
70     timeout: float
71 
72 
73 # Defaults for the namespace of an engine module, see :py:func:`load_engine`
74 
75 categories = {'general': []}
76 engines: Dict[str, Engine] = {}
77 engine_shortcuts = {}
78 """Simple map of registered *shortcuts* to name of the engine (or ``None``).
79 
80 ::
81 
82     engine_shortcuts[engine.shortcut] = engine.name
83 
84 :meta hide-value:
85 """
86 
87 
88 def load_engine(engine_data: dict) -> Optional[Engine]:
89     """Load engine from ``engine_data``.
90 
91     :param dict engine_data:  Attributes from YAML ``settings:engines/<engine>``
92     :return: initialized namespace of the ``<engine>``.
93 
94     1. create a namespace and load module of the ``<engine>``
95     2. update namespace with the defaults from :py:obj:`ENGINE_DEFAULT_ARGS`
96     3. update namespace with values from ``engine_data``
97 
98     If engine *is active*, return namespace of the engine, otherwise return
99     ``None``.
100 
101     This function also returns ``None`` if initialization of the namespace fails
102     for one of the following reasons:
103 
104     - engine name contains underscore
105     - engine name is not lowercase
106     - required attribute is not set :py:func:`is_missing_required_attributes`
107 
108     """
109 
110     engine_name = engine_data['name']
111     if '_' in engine_name:
112         logger.error('Engine name contains underscore: "{}"'.format(engine_name))
113         return None
114 
115     if engine_name.lower() != engine_name:
116         logger.warn('Engine name is not lowercase: "{}", converting to lowercase'.format(engine_name))
117         engine_name = engine_name.lower()
118         engine_data['name'] = engine_name
119 
120     # load_module
121     engine_module = engine_data['engine']
122     try:
123         engine = load_module(engine_module + '.py', ENGINE_DIR)
124     except (SyntaxError, KeyboardInterrupt, SystemExit, SystemError, ImportError, RuntimeError):
125         logger.exception('Fatal exception in engine "{}"'.format(engine_module))
126         sys.exit(1)
127     except BaseException:
128         logger.exception('Cannot load engine "{}"'.format(engine_module))
129         return None
130 
131     update_engine_attributes(engine, engine_data)
132     set_language_attributes(engine)
133     update_attributes_for_tor(engine)
134 
135     if not is_engine_active(engine):
136         return None
137 
138     if is_missing_required_attributes(engine):
139         return None
140 
141     set_loggers(engine, engine_name)
142 
143     if not any(cat in settings['categories_as_tabs'] for cat in engine.categories):
144         engine.categories.append(OTHER_CATEGORY)
145 
146     return engine
147 
148 
149 def set_loggers(engine, engine_name):
150     # set the logger for engine
151     engine.logger = logger.getChild(engine_name)
152     # the engine may have load some other engines
153     # may sure the logger is initialized
154     # use sys.modules.copy() to avoid "RuntimeError: dictionary changed size during iteration"
155     # see https://github.com/python/cpython/issues/89516
156     # and https://docs.python.org/3.10/library/sys.html#sys.modules
157     modules = sys.modules.copy()
158     for module_name, module in modules.items():
159         if (
160             module_name.startswith("searx.engines")
161             and module_name != "searx.engines.__init__"
162             and not hasattr(module, "logger")
163         ):
164             module_engine_name = module_name.split(".")[-1]
165             module.logger = logger.getChild(module_engine_name)
166 
167 
168 def update_engine_attributes(engine: Engine, engine_data):
169     # set engine attributes from engine_data
170     for param_name, param_value in engine_data.items():
171         if param_name == 'categories':
172             if isinstance(param_value, str):
173                 param_value = list(map(str.strip, param_value.split(',')))
174             engine.categories = param_value
175         elif hasattr(engine, 'about') and param_name == 'about':
176             engine.about = {**engine.about, **engine_data['about']}
177         else:
178             setattr(engine, param_name, param_value)
179 
180     # set default attributes
181     for arg_name, arg_value in ENGINE_DEFAULT_ARGS.items():
182         if not hasattr(engine, arg_name):
183             setattr(engine, arg_name, copy.deepcopy(arg_value))
184 
185 
186 def set_language_attributes(engine: Engine):
187     # assign supported languages from json file
188     if engine.name in ENGINES_LANGUAGES:
189         engine.supported_languages = ENGINES_LANGUAGES[engine.name]
190 
191     elif engine.engine in ENGINES_LANGUAGES:
192         # The key of the dictionary ENGINES_LANGUAGES is the *engine name*
193         # configured in settings.xml.  When multiple engines are configured in
194         # settings.yml to use the same origin engine (python module) these
195         # additional engines can use the languages from the origin engine.
196         # For this use the configured ``engine: ...`` from settings.yml
197         engine.supported_languages = ENGINES_LANGUAGES[engine.engine]
198 
199     if hasattr(engine, 'language'):
200         # For an engine, when there is `language: ...` in the YAML settings, the
201         # engine supports only one language, in this case
202         # engine.supported_languages should contains this value defined in
203         # settings.yml
204         if engine.language not in engine.supported_languages:
205             raise ValueError(
206                 "settings.yml - engine: '%s' / language: '%s' not supported" % (engine.name, engine.language)
207             )
208 
209         if isinstance(engine.supported_languages, dict):
210             engine.supported_languages = {engine.language: engine.supported_languages[engine.language]}
211         else:
212             engine.supported_languages = [engine.language]
213 
214     # find custom aliases for non standard language codes
215     for engine_lang in engine.supported_languages:
216         iso_lang = match_language(engine_lang, BABEL_LANGS, fallback=None)
217         if (
218             iso_lang
219             and iso_lang != engine_lang
220             and not engine_lang.startswith(iso_lang)
221             and iso_lang not in engine.supported_languages
222         ):
223             engine.language_aliases[iso_lang] = engine_lang
224 
225     # language_support
226     engine.language_support = len(engine.supported_languages) > 0
227 
228     # assign language fetching method if auxiliary method exists
229     if hasattr(engine, '_fetch_supported_languages'):
230         headers = {
231             'User-Agent': gen_useragent(),
232             'Accept-Language': "en-US,en;q=0.5",  # bing needs to set the English language
233         }
234         engine.fetch_supported_languages = (
235             # pylint: disable=protected-access
236             lambda: engine._fetch_supported_languages(get(engine.supported_languages_url, headers=headers))
237         )
238 
239 
240 def update_attributes_for_tor(engine: Engine) -> bool:
241     if using_tor_proxy(engine) and hasattr(engine, 'onion_url'):
242         engine.search_url = engine.onion_url + getattr(engine, 'search_path', '')
243         engine.timeout += settings['outgoing'].get('extra_proxy_timeout', 0)
244 
245 
246 def is_missing_required_attributes(engine):
247     """An attribute is required when its name doesn't start with ``_`` (underline).
248     Required attributes must not be ``None``.
249 
250     """
251     missing = False
252     for engine_attr in dir(engine):
253         if not engine_attr.startswith('_') and getattr(engine, engine_attr) is None:
254             logger.error('Missing engine config attribute: "{0}.{1}"'.format(engine.name, engine_attr))
255             missing = True
256     return missing
257 
258 
259 def using_tor_proxy(engine: Engine):
260     """Return True if the engine configuration declares to use Tor."""
261     return settings['outgoing'].get('using_tor_proxy') or getattr(engine, 'using_tor_proxy', False)
262 
263 
264 def is_engine_active(engine: Engine):
265     # check if engine is inactive
266     if engine.inactive is True:
267         return False
268 
269     # exclude onion engines if not using tor
270     if 'onions' in engine.categories and not using_tor_proxy(engine):
271         return False
272 
273     return True
274 
275 
276 def register_engine(engine: Engine):
277     if engine.name in engines:
278         logger.error('Engine config error: ambiguous name: {0}'.format(engine.name))
279         sys.exit(1)
280     engines[engine.name] = engine
281 
282     if engine.shortcut in engine_shortcuts:
283         logger.error('Engine config error: ambiguous shortcut: {0}'.format(engine.shortcut))
284         sys.exit(1)
285     engine_shortcuts[engine.shortcut] = engine.name
286 
287     for category_name in engine.categories:
288         categories.setdefault(category_name, []).append(engine)
289 
290 
291 def load_engines(engine_list):
292     """usage: ``engine_list = settings['engines']``"""
293     engines.clear()
294     engine_shortcuts.clear()
295     categories.clear()
296     categories['general'] = []
297     for engine_data in engine_list:
298         engine = load_engine(engine_data)
299         if engine:
300             register_engine(engine)
301     return engines
302
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/searx/engines/__init__.py b/searx/engines/__init__.py
--- a/searx/engines/__init__.py
+++ b/searx/engines/__init__.py
@@ -106,8 +106,12 @@
     - required attribute is not set :py:func:`is_missing_required_attributes`
 
     """
+    # pylint: disable=too-many-return-statements
 
-    engine_name = engine_data['name']
+    engine_name = engine_data.get('name')
+    if engine_name is None:
+        logger.error('An engine does not have a "name" field')
+        return None
     if '_' in engine_name:
         logger.error('Engine name contains underscore: "{}"'.format(engine_name))
         return None
@@ -118,7 +122,10 @@
         engine_data['name'] = engine_name
 
     # load_module
-    engine_module = engine_data['engine']
+    engine_module = engine_data.get('engine')
+    if engine_module is None:
+        logger.error('The "engine" field is missing for the engine named "{}"'.format(engine_name))
+        return None
     try:
         engine = load_module(engine_module + '.py', ENGINE_DIR)
     except (SyntaxError, KeyboardInterrupt, SystemExit, SystemError, ImportError, RuntimeError):

{"golden_diff": "diff --git a/searx/engines/__init__.py b/searx/engines/__init__.py\n--- a/searx/engines/__init__.py\n+++ b/searx/engines/__init__.py\n@@ -106,8 +106,12 @@\n     - required attribute is not set :py:func:`is_missing_required_attributes`\n \n     \"\"\"\n+    # pylint: disable=too-many-return-statements\n \n-    engine_name = engine_data['name']\n+    engine_name = engine_data.get('name')\n+    if engine_name is None:\n+        logger.error('An engine does not have a \"name\" field')\n+        return None\n     if '_' in engine_name:\n         logger.error('Engine name contains underscore: \"{}\"'.format(engine_name))\n         return None\n@@ -118,7 +122,10 @@\n         engine_data['name'] = engine_name\n \n     # load_module\n-    engine_module = engine_data['engine']\n+    engine_module = engine_data.get('engine')\n+    if engine_module is None:\n+        logger.error('The \"engine\" field is missing for the engine named \"{}\"'.format(engine_name))\n+        return None\n     try:\n         engine = load_module(engine_module + '.py', ENGINE_DIR)\n     except (SyntaxError, KeyboardInterrupt, SystemExit, SystemError, ImportError, RuntimeError):\n", "issue": "[Feature] Raise error when the engine doesn't exist\nRaise an error when an engine doesn't exist and there is for example an old parameter that disabled a now removed engine.\r\n\r\nSee https://github.com/searxng/searxng/issues/1951#issuecomment-1324872496 for more info.\r\n\r\n-----\r\n\r\n<!-- PLEASE FILL THESE FIELDS, IT REALLY HELPS THE MAINTAINERS OF SearXNG -->\r\n\r\n**Version of SearXNG, commit number if you are using on master branch and stipulate if you forked SearXNG**\r\n\r\nCommit https://github.com/searxng/searxng/commit/3a765113eefc7ac0b1ee94e9a0ac0db8069ec336\r\n\r\n**How did you install SearXNG?**\r\n1. Git clone\r\n2. pip install -r requirements.txt\r\n3. `python -m searx.webapp`\r\n\r\n**How To Reproduce**\r\nRun searxng with this configuration:\r\n```yaml\r\nengines:\r\n  - name: duckduckgo\r\n    disabled: true\r\ngeneral:\r\n  debug: true\r\nserver:\r\n  secret_key: secret\r\nuse_default_settings: true\r\n```\r\n\r\nAnd you will get this error:\r\n```\r\nTraceback (most recent call last):\r\n  File \"/opt/python/latest/lib/python3.10/runpy.py\", line 196, in _run_module_as_main\r\n    return _run_code(code, main_globals, None,\r\n  File \"/opt/python/latest/lib/python3.10/runpy.py\", line 86, in _run_code\r\n    exec(code, run_globals)\r\n  File \"/workspaces/searx-custom/searxng2/searx/webapp.py\", line 1390, in <module>\r\n    search_initialize(enable_checker=True, check_network=True, enable_metrics=settings['general']['enable_metrics'])\r\n  File \"/workspaces/searx-custom/searxng2/searx/search/__init__.py\", line 30, in initialize\r\n    load_engines(settings_engines)\r\n  File \"/workspaces/searx-custom/searxng2/searx/engines/__init__.py\", line 298, in load_engines\r\n    engine = load_engine(engine_data)\r\n  File \"/workspaces/searx-custom/searxng2/searx/engines/__init__.py\", line 121, in load_engine\r\n    engine_module = engine_data['engine']\r\nKeyError: 'engine'\r\n```\r\n\r\n**Expected behavior**\r\nSearxng should work fine.\r\n\r\n**Screenshots & Logs**\r\n<!-- If applicable, add screenshots, logs to help explain your problem. -->\r\n\r\n**Additional context**\r\n<!-- Add any other context about the problem here. -->\r\n\n", "before_files": [{"content": "# SPDX-License-Identifier: AGPL-3.0-or-later\n# lint: pylint\n\"\"\"This module implements the engine loader.\n\nLoad and initialize the ``engines``, see :py:func:`load_engines` and register\n:py:obj:`engine_shortcuts`.\n\nusage::\n\n    load_engines( settings['engines'] )\n\n\"\"\"\n\nimport sys\nimport copy\nfrom typing import Dict, List, Optional\n\nfrom os.path import realpath, dirname\nfrom babel.localedata import locale_identifiers\nfrom searx import logger, settings\nfrom searx.data import ENGINES_LANGUAGES\nfrom searx.network import get\nfrom searx.utils import load_module, match_language, gen_useragent\n\n\nlogger = logger.getChild('engines')\nENGINE_DIR = dirname(realpath(__file__))\nBABEL_LANGS = [\n    lang_parts[0] + '-' + lang_parts[-1] if len(lang_parts) > 1 else lang_parts[0]\n    for lang_parts in (lang_code.split('_') for lang_code in locale_identifiers())\n]\nENGINE_DEFAULT_ARGS = {\n    \"engine_type\": \"online\",\n    \"inactive\": False,\n    \"disabled\": False,\n    \"timeout\": settings[\"outgoing\"][\"request_timeout\"],\n    \"shortcut\": \"-\",\n    \"categories\": [\"general\"],\n    \"supported_languages\": [],\n    \"language_aliases\": {},\n    \"paging\": False,\n    \"safesearch\": False,\n    \"time_range_support\": False,\n    \"enable_http\": False,\n    \"using_tor_proxy\": False,\n    \"display_error_messages\": True,\n    \"send_accept_language_header\": False,\n    \"tokens\": [],\n    \"about\": {},\n}\n# set automatically when an engine does not have any tab category\nOTHER_CATEGORY = 'other'\n\n\nclass Engine:  # pylint: disable=too-few-public-methods\n    \"\"\"This class is currently never initialized and only used for type hinting.\"\"\"\n\n    name: str\n    engine: str\n    shortcut: str\n    categories: List[str]\n    supported_languages: List[str]\n    about: dict\n    inactive: bool\n    disabled: bool\n    language_support: bool\n    paging: bool\n    safesearch: bool\n    time_range_support: bool\n    timeout: float\n\n\n# Defaults for the namespace of an engine module, see :py:func:`load_engine`\n\ncategories = {'general': []}\nengines: Dict[str, Engine] = {}\nengine_shortcuts = {}\n\"\"\"Simple map of registered *shortcuts* to name of the engine (or ``None``).\n\n::\n\n    engine_shortcuts[engine.shortcut] = engine.name\n\n:meta hide-value:\n\"\"\"\n\n\ndef load_engine(engine_data: dict) -> Optional[Engine]:\n    \"\"\"Load engine from ``engine_data``.\n\n    :param dict engine_data:  Attributes from YAML ``settings:engines/<engine>``\n    :return: initialized namespace of the ``<engine>``.\n\n    1. create a namespace and load module of the ``<engine>``\n    2. update namespace with the defaults from :py:obj:`ENGINE_DEFAULT_ARGS`\n    3. update namespace with values from ``engine_data``\n\n    If engine *is active*, return namespace of the engine, otherwise return\n    ``None``.\n\n    This function also returns ``None`` if initialization of the namespace fails\n    for one of the following reasons:\n\n    - engine name contains underscore\n    - engine name is not lowercase\n    - required attribute is not set :py:func:`is_missing_required_attributes`\n\n    \"\"\"\n\n    engine_name = engine_data['name']\n    if '_' in engine_name:\n        logger.error('Engine name contains underscore: \"{}\"'.format(engine_name))\n        return None\n\n    if engine_name.lower() != engine_name:\n        logger.warn('Engine name is not lowercase: \"{}\", converting to lowercase'.format(engine_name))\n        engine_name = engine_name.lower()\n        engine_data['name'] = engine_name\n\n    # load_module\n    engine_module = engine_data['engine']\n    try:\n        engine = load_module(engine_module + '.py', ENGINE_DIR)\n    except (SyntaxError, KeyboardInterrupt, SystemExit, SystemError, ImportError, RuntimeError):\n        logger.exception('Fatal exception in engine \"{}\"'.format(engine_module))\n        sys.exit(1)\n    except BaseException:\n        logger.exception('Cannot load engine \"{}\"'.format(engine_module))\n        return None\n\n    update_engine_attributes(engine, engine_data)\n    set_language_attributes(engine)\n    update_attributes_for_tor(engine)\n\n    if not is_engine_active(engine):\n        return None\n\n    if is_missing_required_attributes(engine):\n        return None\n\n    set_loggers(engine, engine_name)\n\n    if not any(cat in settings['categories_as_tabs'] for cat in engine.categories):\n        engine.categories.append(OTHER_CATEGORY)\n\n    return engine\n\n\ndef set_loggers(engine, engine_name):\n    # set the logger for engine\n    engine.logger = logger.getChild(engine_name)\n    # the engine may have load some other engines\n    # may sure the logger is initialized\n    # use sys.modules.copy() to avoid \"RuntimeError: dictionary changed size during iteration\"\n    # see https://github.com/python/cpython/issues/89516\n    # and https://docs.python.org/3.10/library/sys.html#sys.modules\n    modules = sys.modules.copy()\n    for module_name, module in modules.items():\n        if (\n            module_name.startswith(\"searx.engines\")\n            and module_name != \"searx.engines.__init__\"\n            and not hasattr(module, \"logger\")\n        ):\n            module_engine_name = module_name.split(\".\")[-1]\n            module.logger = logger.getChild(module_engine_name)\n\n\ndef update_engine_attributes(engine: Engine, engine_data):\n    # set engine attributes from engine_data\n    for param_name, param_value in engine_data.items():\n        if param_name == 'categories':\n            if isinstance(param_value, str):\n                param_value = list(map(str.strip, param_value.split(',')))\n            engine.categories = param_value\n        elif hasattr(engine, 'about') and param_name == 'about':\n            engine.about = {**engine.about, **engine_data['about']}\n        else:\n            setattr(engine, param_name, param_value)\n\n    # set default attributes\n    for arg_name, arg_value in ENGINE_DEFAULT_ARGS.items():\n        if not hasattr(engine, arg_name):\n            setattr(engine, arg_name, copy.deepcopy(arg_value))\n\n\ndef set_language_attributes(engine: Engine):\n    # assign supported languages from json file\n    if engine.name in ENGINES_LANGUAGES:\n        engine.supported_languages = ENGINES_LANGUAGES[engine.name]\n\n    elif engine.engine in ENGINES_LANGUAGES:\n        # The key of the dictionary ENGINES_LANGUAGES is the *engine name*\n        # configured in settings.xml.  When multiple engines are configured in\n        # settings.yml to use the same origin engine (python module) these\n        # additional engines can use the languages from the origin engine.\n        # For this use the configured ``engine: ...`` from settings.yml\n        engine.supported_languages = ENGINES_LANGUAGES[engine.engine]\n\n    if hasattr(engine, 'language'):\n        # For an engine, when there is `language: ...` in the YAML settings, the\n        # engine supports only one language, in this case\n        # engine.supported_languages should contains this value defined in\n        # settings.yml\n        if engine.language not in engine.supported_languages:\n            raise ValueError(\n                \"settings.yml - engine: '%s' / language: '%s' not supported\" % (engine.name, engine.language)\n            )\n\n        if isinstance(engine.supported_languages, dict):\n            engine.supported_languages = {engine.language: engine.supported_languages[engine.language]}\n        else:\n            engine.supported_languages = [engine.language]\n\n    # find custom aliases for non standard language codes\n    for engine_lang in engine.supported_languages:\n        iso_lang = match_language(engine_lang, BABEL_LANGS, fallback=None)\n        if (\n            iso_lang\n            and iso_lang != engine_lang\n            and not engine_lang.startswith(iso_lang)\n            and iso_lang not in engine.supported_languages\n        ):\n            engine.language_aliases[iso_lang] = engine_lang\n\n    # language_support\n    engine.language_support = len(engine.supported_languages) > 0\n\n    # assign language fetching method if auxiliary method exists\n    if hasattr(engine, '_fetch_supported_languages'):\n        headers = {\n            'User-Agent': gen_useragent(),\n            'Accept-Language': \"en-US,en;q=0.5\",  # bing needs to set the English language\n        }\n        engine.fetch_supported_languages = (\n            # pylint: disable=protected-access\n            lambda: engine._fetch_supported_languages(get(engine.supported_languages_url, headers=headers))\n        )\n\n\ndef update_attributes_for_tor(engine: Engine) -> bool:\n    if using_tor_proxy(engine) and hasattr(engine, 'onion_url'):\n        engine.search_url = engine.onion_url + getattr(engine, 'search_path', '')\n        engine.timeout += settings['outgoing'].get('extra_proxy_timeout', 0)\n\n\ndef is_missing_required_attributes(engine):\n    \"\"\"An attribute is required when its name doesn't start with ``_`` (underline).\n    Required attributes must not be ``None``.\n\n    \"\"\"\n    missing = False\n    for engine_attr in dir(engine):\n        if not engine_attr.startswith('_') and getattr(engine, engine_attr) is None:\n            logger.error('Missing engine config attribute: \"{0}.{1}\"'.format(engine.name, engine_attr))\n            missing = True\n    return missing\n\n\ndef using_tor_proxy(engine: Engine):\n    \"\"\"Return True if the engine configuration declares to use Tor.\"\"\"\n    return settings['outgoing'].get('using_tor_proxy') or getattr(engine, 'using_tor_proxy', False)\n\n\ndef is_engine_active(engine: Engine):\n    # check if engine is inactive\n    if engine.inactive is True:\n        return False\n\n    # exclude onion engines if not using tor\n    if 'onions' in engine.categories and not using_tor_proxy(engine):\n        return False\n\n    return True\n\n\ndef register_engine(engine: Engine):\n    if engine.name in engines:\n        logger.error('Engine config error: ambiguous name: {0}'.format(engine.name))\n        sys.exit(1)\n    engines[engine.name] = engine\n\n    if engine.shortcut in engine_shortcuts:\n        logger.error('Engine config error: ambiguous shortcut: {0}'.format(engine.shortcut))\n        sys.exit(1)\n    engine_shortcuts[engine.shortcut] = engine.name\n\n    for category_name in engine.categories:\n        categories.setdefault(category_name, []).append(engine)\n\n\ndef load_engines(engine_list):\n    \"\"\"usage: ``engine_list = settings['engines']``\"\"\"\n    engines.clear()\n    engine_shortcuts.clear()\n    categories.clear()\n    categories['general'] = []\n    for engine_data in engine_list:\n        engine = load_engine(engine_data)\n        if engine:\n            register_engine(engine)\n    return engines\n", "path": "searx/engines/__init__.py"}], "after_files": [{"content": "# SPDX-License-Identifier: AGPL-3.0-or-later\n# lint: pylint\n\"\"\"This module implements the engine loader.\n\nLoad and initialize the ``engines``, see :py:func:`load_engines` and register\n:py:obj:`engine_shortcuts`.\n\nusage::\n\n    load_engines( settings['engines'] )\n\n\"\"\"\n\nimport sys\nimport copy\nfrom typing import Dict, List, Optional\n\nfrom os.path import realpath, dirname\nfrom babel.localedata import locale_identifiers\nfrom searx import logger, settings\nfrom searx.data import ENGINES_LANGUAGES\nfrom searx.network import get\nfrom searx.utils import load_module, match_language, gen_useragent\n\n\nlogger = logger.getChild('engines')\nENGINE_DIR = dirname(realpath(__file__))\nBABEL_LANGS = [\n    lang_parts[0] + '-' + lang_parts[-1] if len(lang_parts) > 1 else lang_parts[0]\n    for lang_parts in (lang_code.split('_') for lang_code in locale_identifiers())\n]\nENGINE_DEFAULT_ARGS = {\n    \"engine_type\": \"online\",\n    \"inactive\": False,\n    \"disabled\": False,\n    \"timeout\": settings[\"outgoing\"][\"request_timeout\"],\n    \"shortcut\": \"-\",\n    \"categories\": [\"general\"],\n    \"supported_languages\": [],\n    \"language_aliases\": {},\n    \"paging\": False,\n    \"safesearch\": False,\n    \"time_range_support\": False,\n    \"enable_http\": False,\n    \"using_tor_proxy\": False,\n    \"display_error_messages\": True,\n    \"send_accept_language_header\": False,\n    \"tokens\": [],\n    \"about\": {},\n}\n# set automatically when an engine does not have any tab category\nOTHER_CATEGORY = 'other'\n\n\nclass Engine:  # pylint: disable=too-few-public-methods\n    \"\"\"This class is currently never initialized and only used for type hinting.\"\"\"\n\n    name: str\n    engine: str\n    shortcut: str\n    categories: List[str]\n    supported_languages: List[str]\n    about: dict\n    inactive: bool\n    disabled: bool\n    language_support: bool\n    paging: bool\n    safesearch: bool\n    time_range_support: bool\n    timeout: float\n\n\n# Defaults for the namespace of an engine module, see :py:func:`load_engine`\n\ncategories = {'general': []}\nengines: Dict[str, Engine] = {}\nengine_shortcuts = {}\n\"\"\"Simple map of registered *shortcuts* to name of the engine (or ``None``).\n\n::\n\n    engine_shortcuts[engine.shortcut] = engine.name\n\n:meta hide-value:\n\"\"\"\n\n\ndef load_engine(engine_data: dict) -> Optional[Engine]:\n    \"\"\"Load engine from ``engine_data``.\n\n    :param dict engine_data:  Attributes from YAML ``settings:engines/<engine>``\n    :return: initialized namespace of the ``<engine>``.\n\n    1. create a namespace and load module of the ``<engine>``\n    2. update namespace with the defaults from :py:obj:`ENGINE_DEFAULT_ARGS`\n    3. update namespace with values from ``engine_data``\n\n    If engine *is active*, return namespace of the engine, otherwise return\n    ``None``.\n\n    This function also returns ``None`` if initialization of the namespace fails\n    for one of the following reasons:\n\n    - engine name contains underscore\n    - engine name is not lowercase\n    - required attribute is not set :py:func:`is_missing_required_attributes`\n\n    \"\"\"\n    # pylint: disable=too-many-return-statements\n\n    engine_name = engine_data.get('name')\n    if engine_name is None:\n        logger.error('An engine does not have a \"name\" field')\n        return None\n    if '_' in engine_name:\n        logger.error('Engine name contains underscore: \"{}\"'.format(engine_name))\n        return None\n\n    if engine_name.lower() != engine_name:\n        logger.warn('Engine name is not lowercase: \"{}\", converting to lowercase'.format(engine_name))\n        engine_name = engine_name.lower()\n        engine_data['name'] = engine_name\n\n    # load_module\n    engine_module = engine_data.get('engine')\n    if engine_module is None:\n        logger.error('The \"engine\" field is missing for the engine named \"{}\"'.format(engine_name))\n        return None\n    try:\n        engine = load_module(engine_module + '.py', ENGINE_DIR)\n    except (SyntaxError, KeyboardInterrupt, SystemExit, SystemError, ImportError, RuntimeError):\n        logger.exception('Fatal exception in engine \"{}\"'.format(engine_module))\n        sys.exit(1)\n    except BaseException:\n        logger.exception('Cannot load engine \"{}\"'.format(engine_module))\n        return None\n\n    update_engine_attributes(engine, engine_data)\n    set_language_attributes(engine)\n    update_attributes_for_tor(engine)\n\n    if not is_engine_active(engine):\n        return None\n\n    if is_missing_required_attributes(engine):\n        return None\n\n    set_loggers(engine, engine_name)\n\n    if not any(cat in settings['categories_as_tabs'] for cat in engine.categories):\n        engine.categories.append(OTHER_CATEGORY)\n\n    return engine\n\n\ndef set_loggers(engine, engine_name):\n    # set the logger for engine\n    engine.logger = logger.getChild(engine_name)\n    # the engine may have load some other engines\n    # may sure the logger is initialized\n    # use sys.modules.copy() to avoid \"RuntimeError: dictionary changed size during iteration\"\n    # see https://github.com/python/cpython/issues/89516\n    # and https://docs.python.org/3.10/library/sys.html#sys.modules\n    modules = sys.modules.copy()\n    for module_name, module in modules.items():\n        if (\n            module_name.startswith(\"searx.engines\")\n            and module_name != \"searx.engines.__init__\"\n            and not hasattr(module, \"logger\")\n        ):\n            module_engine_name = module_name.split(\".\")[-1]\n            module.logger = logger.getChild(module_engine_name)\n\n\ndef update_engine_attributes(engine: Engine, engine_data):\n    # set engine attributes from engine_data\n    for param_name, param_value in engine_data.items():\n        if param_name == 'categories':\n            if isinstance(param_value, str):\n                param_value = list(map(str.strip, param_value.split(',')))\n            engine.categories = param_value\n        elif hasattr(engine, 'about') and param_name == 'about':\n            engine.about = {**engine.about, **engine_data['about']}\n        else:\n            setattr(engine, param_name, param_value)\n\n    # set default attributes\n    for arg_name, arg_value in ENGINE_DEFAULT_ARGS.items():\n        if not hasattr(engine, arg_name):\n            setattr(engine, arg_name, copy.deepcopy(arg_value))\n\n\ndef set_language_attributes(engine: Engine):\n    # assign supported languages from json file\n    if engine.name in ENGINES_LANGUAGES:\n        engine.supported_languages = ENGINES_LANGUAGES[engine.name]\n\n    elif engine.engine in ENGINES_LANGUAGES:\n        # The key of the dictionary ENGINES_LANGUAGES is the *engine name*\n        # configured in settings.xml.  When multiple engines are configured in\n        # settings.yml to use the same origin engine (python module) these\n        # additional engines can use the languages from the origin engine.\n        # For this use the configured ``engine: ...`` from settings.yml\n        engine.supported_languages = ENGINES_LANGUAGES[engine.engine]\n\n    if hasattr(engine, 'language'):\n        # For an engine, when there is `language: ...` in the YAML settings, the\n        # engine supports only one language, in this case\n        # engine.supported_languages should contains this value defined in\n        # settings.yml\n        if engine.language not in engine.supported_languages:\n            raise ValueError(\n                \"settings.yml - engine: '%s' / language: '%s' not supported\" % (engine.name, engine.language)\n            )\n\n        if isinstance(engine.supported_languages, dict):\n            engine.supported_languages = {engine.language: engine.supported_languages[engine.language]}\n        else:\n            engine.supported_languages = [engine.language]\n\n    # find custom aliases for non standard language codes\n    for engine_lang in engine.supported_languages:\n        iso_lang = match_language(engine_lang, BABEL_LANGS, fallback=None)\n        if (\n            iso_lang\n            and iso_lang != engine_lang\n            and not engine_lang.startswith(iso_lang)\n            and iso_lang not in engine.supported_languages\n        ):\n            engine.language_aliases[iso_lang] = engine_lang\n\n    # language_support\n    engine.language_support = len(engine.supported_languages) > 0\n\n    # assign language fetching method if auxiliary method exists\n    if hasattr(engine, '_fetch_supported_languages'):\n        headers = {\n            'User-Agent': gen_useragent(),\n            'Accept-Language': \"en-US,en;q=0.5\",  # bing needs to set the English language\n        }\n        engine.fetch_supported_languages = (\n            # pylint: disable=protected-access\n            lambda: engine._fetch_supported_languages(get(engine.supported_languages_url, headers=headers))\n        )\n\n\ndef update_attributes_for_tor(engine: Engine) -> bool:\n    if using_tor_proxy(engine) and hasattr(engine, 'onion_url'):\n        engine.search_url = engine.onion_url + getattr(engine, 'search_path', '')\n        engine.timeout += settings['outgoing'].get('extra_proxy_timeout', 0)\n\n\ndef is_missing_required_attributes(engine):\n    \"\"\"An attribute is required when its name doesn't start with ``_`` (underline).\n    Required attributes must not be ``None``.\n\n    \"\"\"\n    missing = False\n    for engine_attr in dir(engine):\n        if not engine_attr.startswith('_') and getattr(engine, engine_attr) is None:\n            logger.error('Missing engine config attribute: \"{0}.{1}\"'.format(engine.name, engine_attr))\n            missing = True\n    return missing\n\n\ndef using_tor_proxy(engine: Engine):\n    \"\"\"Return True if the engine configuration declares to use Tor.\"\"\"\n    return settings['outgoing'].get('using_tor_proxy') or getattr(engine, 'using_tor_proxy', False)\n\n\ndef is_engine_active(engine: Engine):\n    # check if engine is inactive\n    if engine.inactive is True:\n        return False\n\n    # exclude onion engines if not using tor\n    if 'onions' in engine.categories and not using_tor_proxy(engine):\n        return False\n\n    return True\n\n\ndef register_engine(engine: Engine):\n    if engine.name in engines:\n        logger.error('Engine config error: ambiguous name: {0}'.format(engine.name))\n        sys.exit(1)\n    engines[engine.name] = engine\n\n    if engine.shortcut in engine_shortcuts:\n        logger.error('Engine config error: ambiguous shortcut: {0}'.format(engine.shortcut))\n        sys.exit(1)\n    engine_shortcuts[engine.shortcut] = engine.name\n\n    for category_name in engine.categories:\n        categories.setdefault(category_name, []).append(engine)\n\n\ndef load_engines(engine_list):\n    \"\"\"usage: ``engine_list = settings['engines']``\"\"\"\n    engines.clear()\n    engine_shortcuts.clear()\n    categories.clear()\n    categories['general'] = []\n    for engine_data in engine_list:\n        engine = load_engine(engine_data)\n        if engine:\n            register_engine(engine)\n    return engines\n", "path": "searx/engines/__init__.py"}]}