2020-12-16 13:41:32 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2021-04-27 15:13:39 +02:00
|
|
|
# lint: pylint
|
|
|
|
|
|
|
|
"""Implement request processores used by engine-types.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'EngineProcessor',
|
|
|
|
'OfflineProcessor',
|
|
|
|
'OnlineProcessor',
|
|
|
|
'OnlineDictionaryProcessor',
|
|
|
|
'OnlineCurrencyProcessor',
|
|
|
|
'processors',
|
|
|
|
]
|
|
|
|
|
|
|
|
from searx import logger
|
|
|
|
import searx.engines as engines
|
2020-12-16 13:41:32 +01: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')
|
|
|
|
processors = {}
|
2021-04-27 15:13:39 +02:00
|
|
|
"""Cache request processores, stored by *engine-name* (:py:func:`initialize`)"""
|
2020-12-16 13:41:32 +01:00
|
|
|
|
|
|
|
def get_processor_class(engine_type):
|
2021-04-27 15:13:39 +02:00
|
|
|
"""Return processor class according to the ``engine_type``"""
|
2020-12-16 13:41:32 +01:00
|
|
|
for c in [OnlineProcessor, OfflineProcessor, OnlineDictionaryProcessor, OnlineCurrencyProcessor]:
|
|
|
|
if c.engine_type == engine_type:
|
|
|
|
return c
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_processor(engine, engine_name):
|
2021-04-27 15:13:39 +02:00
|
|
|
"""Return processor instance that fits to ``engine.engine.type``)"""
|
2020-12-16 13:41:32 +01: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 15:13:39 +02:00
|
|
|
return None
|
2020-12-16 13:41:32 +01:00
|
|
|
|
|
|
|
def initialize(engine_list):
|
2021-04-27 15:13:39 +02:00
|
|
|
"""Initialize all engines and store a processor for each engine in :py:obj:`processors`."""
|
2020-12-16 13:41:32 +01:00
|
|
|
engines.initialize_engines(engine_list)
|
|
|
|
for engine_name, engine in engines.engines.items():
|
|
|
|
processor = get_processor(engine, engine_name)
|
|
|
|
if processor is None:
|
|
|
|
logger.error('Error get processor for engine %s', engine_name)
|
|
|
|
else:
|
|
|
|
processors[engine_name] = processor
|