2021-05-29 07:35:10 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
# lint: pylint
|
2023-06-30 16:07:02 +00:00
|
|
|
"""Load and initialize the ``engines``, see :py:func:`load_engines` and register
|
2021-06-01 11:34:41 +00:00
|
|
|
:py:obj:`engine_shortcuts`.
|
|
|
|
|
|
|
|
usage::
|
|
|
|
|
|
|
|
load_engines( settings['engines'] )
|
|
|
|
|
2021-05-29 07:35:10 +00:00
|
|
|
"""
|
2013-10-16 22:32:32 +00:00
|
|
|
|
2022-09-29 18:54:46 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2014-02-05 19:24:31 +00:00
|
|
|
import sys
|
2021-05-29 07:35:10 +00:00
|
|
|
import copy
|
2017-06-06 20:20:20 +00:00
|
|
|
from os.path import realpath, dirname
|
2022-09-29 18:54:46 +00:00
|
|
|
|
2023-06-25 10:37:31 +00:00
|
|
|
from typing import TYPE_CHECKING, Dict
|
|
|
|
import types
|
|
|
|
import inspect
|
2022-09-29 18:54:46 +00:00
|
|
|
|
2021-05-29 07:35:10 +00:00
|
|
|
from searx import logger, settings
|
2022-09-29 18:54:46 +00:00
|
|
|
from searx.utils import load_module
|
2015-01-09 03:13:05 +00:00
|
|
|
|
2022-09-29 18:54:46 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from searx.enginelib import Engine
|
2015-01-09 03:13:05 +00:00
|
|
|
|
|
|
|
logger = logger.getChild('engines')
|
2021-05-29 07:35:10 +00:00
|
|
|
ENGINE_DIR = dirname(realpath(__file__))
|
|
|
|
ENGINE_DEFAULT_ARGS = {
|
2023-06-25 10:37:31 +00:00
|
|
|
# Common options in the engine module
|
2021-05-29 07:35:10 +00:00
|
|
|
"engine_type": "online",
|
|
|
|
"paging": False,
|
|
|
|
"time_range_support": False,
|
2023-06-25 10:37:31 +00:00
|
|
|
"safesearch": False,
|
|
|
|
# settings.yml
|
|
|
|
"categories": ["general"],
|
2021-05-29 07:35:10 +00:00
|
|
|
"enable_http": False,
|
2023-06-25 10:37:31 +00:00
|
|
|
"shortcut": "-",
|
|
|
|
"timeout": settings["outgoing"]["request_timeout"],
|
2021-05-29 07:35:10 +00:00
|
|
|
"display_error_messages": True,
|
2023-06-25 10:37:31 +00:00
|
|
|
"disabled": False,
|
|
|
|
"inactive": False,
|
|
|
|
"about": {},
|
|
|
|
"using_tor_proxy": False,
|
2022-08-01 15:01:59 +00:00
|
|
|
"send_accept_language_header": False,
|
2021-05-29 07:35:10 +00:00
|
|
|
"tokens": [],
|
2023-11-13 18:12:50 +00:00
|
|
|
"max_page": 0,
|
2021-05-29 07:35:10 +00:00
|
|
|
}
|
2021-12-28 15:12:54 +00:00
|
|
|
# set automatically when an engine does not have any tab category
|
2022-07-24 07:32:05 +00:00
|
|
|
DEFAULT_CATEGORY = 'other'
|
2021-12-28 15:12:54 +00:00
|
|
|
|
2022-01-04 14:28:05 +00:00
|
|
|
|
2022-01-04 10:53:42 +00:00
|
|
|
# Defaults for the namespace of an engine module, see :py:func:`load_engine`
|
2021-06-01 11:34:41 +00:00
|
|
|
|
2021-05-29 07:35:10 +00:00
|
|
|
categories = {'general': []}
|
2023-06-25 10:37:31 +00:00
|
|
|
engines: Dict[str, Engine | types.ModuleType] = {}
|
2014-01-31 14:45:18 +00:00
|
|
|
engine_shortcuts = {}
|
2021-06-01 11:34:41 +00:00
|
|
|
"""Simple map of registered *shortcuts* to name of the engine (or ``None``).
|
|
|
|
|
|
|
|
::
|
2014-01-31 14:45:18 +00:00
|
|
|
|
2021-06-01 11:34:41 +00:00
|
|
|
engine_shortcuts[engine.shortcut] = engine.name
|
|
|
|
|
2022-09-18 10:44:12 +00:00
|
|
|
:meta hide-value:
|
2021-06-01 11:34:41 +00:00
|
|
|
"""
|
2014-01-19 21:59:01 +00:00
|
|
|
|
2021-12-27 08:26:22 +00:00
|
|
|
|
2023-06-25 10:37:31 +00:00
|
|
|
def check_engine_module(module: types.ModuleType):
|
|
|
|
# probe unintentional name collisions / for example name collisions caused
|
|
|
|
# by import statements in the engine module ..
|
|
|
|
|
|
|
|
# network: https://github.com/searxng/searxng/issues/762#issuecomment-1605323861
|
|
|
|
obj = getattr(module, 'network', None)
|
|
|
|
if obj and inspect.ismodule(obj):
|
|
|
|
msg = f'type of {module.__name__}.network is a module ({obj.__name__}), expected a string'
|
|
|
|
# logger.error(msg)
|
|
|
|
raise TypeError(msg)
|
|
|
|
|
|
|
|
|
|
|
|
def load_engine(engine_data: dict) -> Engine | types.ModuleType | None:
|
2021-06-01 11:34:41 +00:00
|
|
|
"""Load engine from ``engine_data``.
|
|
|
|
|
|
|
|
:param dict engine_data: Attributes from YAML ``settings:engines/<engine>``
|
|
|
|
:return: initialized namespace of the ``<engine>``.
|
|
|
|
|
|
|
|
1. create a namespace and load module of the ``<engine>``
|
|
|
|
2. update namespace with the defaults from :py:obj:`ENGINE_DEFAULT_ARGS`
|
|
|
|
3. update namespace with values from ``engine_data``
|
|
|
|
|
|
|
|
If engine *is active*, return namespace of the engine, otherwise return
|
|
|
|
``None``.
|
|
|
|
|
|
|
|
This function also returns ``None`` if initialization of the namespace fails
|
|
|
|
for one of the following reasons:
|
|
|
|
|
|
|
|
- engine name contains underscore
|
|
|
|
- engine name is not lowercase
|
|
|
|
- required attribute is not set :py:func:`is_missing_required_attributes`
|
|
|
|
|
|
|
|
"""
|
2022-12-04 22:43:59 +00:00
|
|
|
# pylint: disable=too-many-return-statements
|
2021-06-01 11:34:41 +00:00
|
|
|
|
2022-12-04 22:43:59 +00:00
|
|
|
engine_name = engine_data.get('name')
|
|
|
|
if engine_name is None:
|
|
|
|
logger.error('An engine does not have a "name" field')
|
|
|
|
return None
|
2019-07-26 07:04:00 +00:00
|
|
|
if '_' in engine_name:
|
|
|
|
logger.error('Engine name contains underscore: "{}"'.format(engine_name))
|
2021-06-01 11:34:41 +00:00
|
|
|
return None
|
2016-09-28 20:30:05 +00:00
|
|
|
|
2019-07-26 07:04:00 +00:00
|
|
|
if engine_name.lower() != engine_name:
|
2023-05-19 14:05:29 +00:00
|
|
|
logger.warning('Engine name is not lowercase: "{}", converting to lowercase'.format(engine_name))
|
2019-07-26 07:04:00 +00:00
|
|
|
engine_name = engine_name.lower()
|
|
|
|
engine_data['name'] = engine_name
|
|
|
|
|
2021-05-29 07:35:10 +00:00
|
|
|
# load_module
|
2023-06-25 10:37:31 +00:00
|
|
|
module_name = engine_data.get('engine')
|
|
|
|
if module_name is None:
|
2022-12-04 22:43:59 +00:00
|
|
|
logger.error('The "engine" field is missing for the engine named "{}"'.format(engine_name))
|
|
|
|
return None
|
2020-08-31 16:59:27 +00:00
|
|
|
try:
|
2023-06-25 10:37:31 +00:00
|
|
|
engine = load_module(module_name + '.py', ENGINE_DIR)
|
2020-11-16 08:37:13 +00:00
|
|
|
except (SyntaxError, KeyboardInterrupt, SystemExit, SystemError, ImportError, RuntimeError):
|
2023-06-25 10:37:31 +00:00
|
|
|
logger.exception('Fatal exception in engine "{}"'.format(module_name))
|
2020-09-07 13:39:26 +00:00
|
|
|
sys.exit(1)
|
2021-05-29 07:35:10 +00:00
|
|
|
except BaseException:
|
2023-06-25 10:37:31 +00:00
|
|
|
logger.exception('Cannot load engine "{}"'.format(module_name))
|
2020-08-31 16:59:27 +00:00
|
|
|
return None
|
2014-01-31 03:35:23 +00:00
|
|
|
|
2023-06-25 10:37:31 +00:00
|
|
|
check_engine_module(engine)
|
2021-05-29 07:35:10 +00:00
|
|
|
update_engine_attributes(engine, engine_data)
|
|
|
|
update_attributes_for_tor(engine)
|
|
|
|
|
2022-09-29 18:54:46 +00:00
|
|
|
# avoid cyclic imports
|
|
|
|
# pylint: disable=import-outside-toplevel
|
|
|
|
from searx.enginelib.traits import EngineTraitsMap
|
|
|
|
|
|
|
|
trait_map = EngineTraitsMap.from_data()
|
|
|
|
trait_map.set_traits(engine)
|
|
|
|
|
2021-05-29 07:35:10 +00:00
|
|
|
if not is_engine_active(engine):
|
|
|
|
return None
|
2021-06-01 11:34:41 +00:00
|
|
|
|
|
|
|
if is_missing_required_attributes(engine):
|
|
|
|
return None
|
|
|
|
|
2021-09-09 15:21:48 +00:00
|
|
|
set_loggers(engine, engine_name)
|
|
|
|
|
2021-12-28 15:12:54 +00:00
|
|
|
if not any(cat in settings['categories_as_tabs'] for cat in engine.categories):
|
2022-07-24 07:32:05 +00:00
|
|
|
engine.categories.append(DEFAULT_CATEGORY)
|
2021-12-28 15:12:54 +00:00
|
|
|
|
2021-05-29 07:35:10 +00:00
|
|
|
return engine
|
|
|
|
|
|
|
|
|
2021-09-09 15:21:48 +00:00
|
|
|
def set_loggers(engine, engine_name):
|
|
|
|
# set the logger for engine
|
|
|
|
engine.logger = logger.getChild(engine_name)
|
|
|
|
# the engine may have load some other engines
|
|
|
|
# may sure the logger is initialized
|
2022-06-18 05:38:36 +00:00
|
|
|
# use sys.modules.copy() to avoid "RuntimeError: dictionary changed size during iteration"
|
|
|
|
# see https://github.com/python/cpython/issues/89516
|
|
|
|
# and https://docs.python.org/3.10/library/sys.html#sys.modules
|
|
|
|
modules = sys.modules.copy()
|
|
|
|
for module_name, module in modules.items():
|
2021-09-09 15:21:48 +00:00
|
|
|
if (
|
|
|
|
module_name.startswith("searx.engines")
|
|
|
|
and module_name != "searx.engines.__init__"
|
|
|
|
and not hasattr(module, "logger")
|
|
|
|
):
|
|
|
|
module_engine_name = module_name.split(".")[-1]
|
2023-06-25 10:37:31 +00:00
|
|
|
module.logger = logger.getChild(module_engine_name) # type: ignore
|
2021-09-09 15:21:48 +00:00
|
|
|
|
|
|
|
|
2023-06-25 10:37:31 +00:00
|
|
|
def update_engine_attributes(engine: Engine | types.ModuleType, engine_data):
|
2021-05-29 07:35:10 +00:00
|
|
|
# set engine attributes from engine_data
|
2020-11-16 11:44:07 +00:00
|
|
|
for param_name, param_value in engine_data.items():
|
2021-05-29 07:35:10 +00:00
|
|
|
if param_name == 'categories':
|
|
|
|
if isinstance(param_value, str):
|
|
|
|
param_value = list(map(str.strip, param_value.split(',')))
|
2023-06-25 10:37:31 +00:00
|
|
|
engine.categories = param_value # type: ignore
|
2021-12-22 08:13:23 +00:00
|
|
|
elif hasattr(engine, 'about') and param_name == 'about':
|
2023-06-25 10:37:31 +00:00
|
|
|
engine.about = {**engine.about, **engine_data['about']} # type: ignore
|
2021-10-06 16:02:29 +00:00
|
|
|
else:
|
2020-11-16 11:44:07 +00:00
|
|
|
setattr(engine, param_name, param_value)
|
2014-01-31 14:45:18 +00:00
|
|
|
|
2021-05-29 07:35:10 +00:00
|
|
|
# set default attributes
|
|
|
|
for arg_name, arg_value in ENGINE_DEFAULT_ARGS.items():
|
2016-02-19 14:13:01 +00:00
|
|
|
if not hasattr(engine, arg_name):
|
2021-05-29 07:35:10 +00:00
|
|
|
setattr(engine, arg_name, copy.deepcopy(arg_value))
|
2015-01-31 22:11:45 +00:00
|
|
|
|
2014-01-31 14:45:18 +00:00
|
|
|
|
2023-06-25 10:37:31 +00:00
|
|
|
def update_attributes_for_tor(engine: Engine | types.ModuleType):
|
2022-01-22 18:51:40 +00:00
|
|
|
if using_tor_proxy(engine) and hasattr(engine, 'onion_url'):
|
2023-06-25 10:37:31 +00:00
|
|
|
engine.search_url = engine.onion_url + getattr(engine, 'search_path', '') # type: ignore
|
|
|
|
engine.timeout += settings['outgoing'].get('extra_proxy_timeout', 0) # type: ignore
|
2021-05-29 07:35:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_missing_required_attributes(engine):
|
2021-06-01 11:34:41 +00:00
|
|
|
"""An attribute is required when its name doesn't start with ``_`` (underline).
|
|
|
|
Required attributes must not be ``None``.
|
|
|
|
|
2021-05-29 07:35:10 +00:00
|
|
|
"""
|
|
|
|
missing = False
|
|
|
|
for engine_attr in dir(engine):
|
|
|
|
if not engine_attr.startswith('_') and getattr(engine, engine_attr) is None:
|
2021-12-27 08:26:22 +00:00
|
|
|
logger.error('Missing engine config attribute: "{0}.{1}"'.format(engine.name, engine_attr))
|
2021-05-29 07:35:10 +00:00
|
|
|
missing = True
|
|
|
|
return missing
|
|
|
|
|
|
|
|
|
2023-06-25 10:37:31 +00:00
|
|
|
def using_tor_proxy(engine: Engine | types.ModuleType):
|
2022-01-22 18:51:40 +00:00
|
|
|
"""Return True if the engine configuration declares to use Tor."""
|
|
|
|
return settings['outgoing'].get('using_tor_proxy') or getattr(engine, 'using_tor_proxy', False)
|
|
|
|
|
|
|
|
|
2023-06-25 10:37:31 +00:00
|
|
|
def is_engine_active(engine: Engine | types.ModuleType):
|
2021-05-29 07:35:10 +00:00
|
|
|
# check if engine is inactive
|
|
|
|
if engine.inactive is True:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# exclude onion engines if not using tor
|
2022-01-22 18:51:40 +00:00
|
|
|
if 'onions' in engine.categories and not using_tor_proxy(engine):
|
2021-05-29 07:35:10 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2016-02-19 14:13:01 +00:00
|
|
|
|
2023-06-25 10:37:31 +00:00
|
|
|
def register_engine(engine: Engine | types.ModuleType):
|
2021-06-01 14:24:31 +00:00
|
|
|
if engine.name in engines:
|
2022-09-27 15:01:00 +00:00
|
|
|
logger.error('Engine config error: ambiguous name: {0}'.format(engine.name))
|
2021-06-01 14:24:31 +00:00
|
|
|
sys.exit(1)
|
2021-05-29 07:35:10 +00:00
|
|
|
engines[engine.name] = engine
|
2021-06-01 14:24:31 +00:00
|
|
|
|
2016-02-19 14:13:01 +00:00
|
|
|
if engine.shortcut in engine_shortcuts:
|
2022-09-27 15:01:00 +00:00
|
|
|
logger.error('Engine config error: ambiguous shortcut: {0}'.format(engine.shortcut))
|
2016-02-19 14:13:01 +00:00
|
|
|
sys.exit(1)
|
|
|
|
engine_shortcuts[engine.shortcut] = engine.name
|
2021-06-01 14:24:31 +00:00
|
|
|
|
2021-05-29 07:35:10 +00:00
|
|
|
for category_name in engine.categories:
|
|
|
|
categories.setdefault(category_name, []).append(engine)
|
2014-01-31 14:45:18 +00:00
|
|
|
|
2014-01-19 21:59:01 +00:00
|
|
|
|
2017-07-21 12:27:25 +00:00
|
|
|
def load_engines(engine_list):
|
2021-12-27 08:26:22 +00:00
|
|
|
"""usage: ``engine_list = settings['engines']``"""
|
2017-07-21 12:27:25 +00:00
|
|
|
engines.clear()
|
2016-05-19 05:38:43 +00:00
|
|
|
engine_shortcuts.clear()
|
2021-05-29 07:35:10 +00:00
|
|
|
categories.clear()
|
|
|
|
categories['general'] = []
|
2016-12-27 16:25:19 +00:00
|
|
|
for engine_data in engine_list:
|
2020-08-31 16:59:27 +00:00
|
|
|
engine = load_engine(engine_data)
|
2021-05-29 07:35:10 +00:00
|
|
|
if engine:
|
|
|
|
register_engine(engine)
|
2017-07-21 12:27:25 +00:00
|
|
|
return engines
|