2020-12-16 12:41:32 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2021-04-27 13:13:39 +00:00
|
|
|
# lint: pylint
|
|
|
|
|
|
|
|
"""Implement request processores used by engine-types.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'EngineProcessor',
|
|
|
|
'OfflineProcessor',
|
|
|
|
'OnlineProcessor',
|
|
|
|
'OnlineDictionaryProcessor',
|
|
|
|
'OnlineCurrencyProcessor',
|
2021-05-05 11:08:54 +00:00
|
|
|
'PROCESSORS',
|
2021-04-27 13:13:39 +00:00
|
|
|
]
|
|
|
|
|
2021-05-05 11:08:54 +00:00
|
|
|
import threading
|
2022-01-17 08:04:51 +00:00
|
|
|
from typing import Dict
|
2021-05-05 11:08:54 +00:00
|
|
|
|
2021-04-27 13:13:39 +00:00
|
|
|
from searx import logger
|
2021-07-03 15:51:39 +00:00
|
|
|
from searx import engines
|
2020-12-16 12:41:32 +00:00
|
|
|
|
|
|
|
from .online import OnlineProcessor
|
|
|
|
from .offline import OfflineProcessor
|
|
|
|
from .online_dictionary import OnlineDictionaryProcessor
|
|
|
|
from .online_currency import OnlineCurrencyProcessor
|
|
|
|
from .abstract import EngineProcessor
|
|
|
|
|
|
|
|
logger = logger.getChild('search.processors')
|
2022-01-17 08:04:51 +00:00
|
|
|
PROCESSORS: Dict[str, EngineProcessor] = {}
|
2021-04-27 13:13:39 +00:00
|
|
|
"""Cache request processores, stored by *engine-name* (:py:func:`initialize`)"""
|
2020-12-16 12:41:32 +00:00
|
|
|
|
2021-12-27 08:26:22 +00:00
|
|
|
|
2020-12-16 12:41:32 +00:00
|
|
|
def get_processor_class(engine_type):
|
2021-04-27 13:13:39 +00:00
|
|
|
"""Return processor class according to the ``engine_type``"""
|
2020-12-16 12:41:32 +00:00
|
|
|
for c in [OnlineProcessor, OfflineProcessor, OnlineDictionaryProcessor, OnlineCurrencyProcessor]:
|
|
|
|
if c.engine_type == engine_type:
|
|
|
|
return c
|
|
|
|
return None
|
|
|
|
|
2021-05-05 11:08:54 +00:00
|
|
|
|
2020-12-16 12:41:32 +00:00
|
|
|
def get_processor(engine, engine_name):
|
2021-04-27 13:13:39 +00:00
|
|
|
"""Return processor instance that fits to ``engine.engine.type``)"""
|
2020-12-16 12:41:32 +00:00
|
|
|
engine_type = getattr(engine, 'engine_type', 'online')
|
|
|
|
processor_class = get_processor_class(engine_type)
|
|
|
|
if processor_class:
|
|
|
|
return processor_class(engine, engine_name)
|
2021-04-27 13:13:39 +00:00
|
|
|
return None
|
2020-12-16 12:41:32 +00:00
|
|
|
|
2021-05-05 11:08:54 +00:00
|
|
|
|
|
|
|
def initialize_processor(processor):
|
|
|
|
"""Initialize one processor
|
|
|
|
|
|
|
|
Call the init function of the engine
|
|
|
|
"""
|
|
|
|
if processor.has_initialize_function:
|
|
|
|
t = threading.Thread(target=processor.initialize, daemon=True)
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
|
2020-12-16 12:41:32 +00:00
|
|
|
def initialize(engine_list):
|
2021-05-05 11:08:54 +00:00
|
|
|
"""Initialize all engines and store a processor for each engine in :py:obj:`PROCESSORS`."""
|
|
|
|
for engine_data in engine_list:
|
|
|
|
engine_name = engine_data['name']
|
|
|
|
engine = engines.engines.get(engine_name)
|
|
|
|
if engine:
|
|
|
|
processor = get_processor(engine, engine_name)
|
|
|
|
initialize_processor(processor)
|
|
|
|
if processor is None:
|
2021-09-06 17:46:08 +00:00
|
|
|
engine.logger.error('Error get processor for engine %s', engine_name)
|
2021-05-05 11:08:54 +00:00
|
|
|
else:
|
|
|
|
PROCESSORS[engine_name] = processor
|