Files
searxng/searx/search/processors/__init__.py
T

85 lines
2.5 KiB
Python
Raw Normal View History

# SPDX-License-Identifier: AGPL-3.0-or-later
2025-08-22 17:17:51 +02:00
"""Implement request processors used by engine-types."""
__all__ = [
'EngineProcessor',
'OfflineProcessor',
'OnlineProcessor',
'OnlineDictionaryProcessor',
'OnlineCurrencyProcessor',
2022-01-30 16:05:08 +01:00
'OnlineUrlSearchProcessor',
'PROCESSORS',
]
2025-08-22 17:17:51 +02:00
import typing as t
import threading
from searx import logger
from searx import engines
from .online import OnlineProcessor
from .offline import OfflineProcessor
from .online_dictionary import OnlineDictionaryProcessor
from .online_currency import OnlineCurrencyProcessor
2022-01-30 16:05:08 +01:00
from .online_url_search import OnlineUrlSearchProcessor
from .abstract import EngineProcessor
2025-08-22 17:17:51 +02:00
if t.TYPE_CHECKING:
from searx.enginelib import Engine
logger = logger.getChild('search.processors')
2025-08-22 17:17:51 +02:00
PROCESSORS: dict[str, EngineProcessor] = {}
2023-09-15 00:53:03 -07:00
"""Cache request processors, stored by *engine-name* (:py:func:`initialize`)
:meta hide-value:
"""
2025-08-22 17:17:51 +02:00
def get_processor_class(engine_type: str) -> type[EngineProcessor] | None:
"""Return processor class according to the ``engine_type``"""
2022-01-30 16:05:08 +01:00
for c in [
OnlineProcessor,
OfflineProcessor,
OnlineDictionaryProcessor,
OnlineCurrencyProcessor,
OnlineUrlSearchProcessor,
]:
if c.engine_type == engine_type:
return c
return None
2025-08-22 17:17:51 +02:00
def get_processor(engine: "Engine | ModuleType", engine_name: str) -> EngineProcessor | None:
"""Return processor instance that fits to ``engine.engine.type``"""
engine_type = getattr(engine, 'engine_type', 'online')
processor_class = get_processor_class(engine_type)
2025-08-22 17:17:51 +02:00
if processor_class is not None:
return processor_class(engine, engine_name)
return None
2025-08-22 17:17:51 +02:00
def initialize_processor(processor: EngineProcessor):
"""Initialize one processor
Call the init function of the engine
"""
if processor.has_initialize_function:
2025-08-22 17:17:51 +02:00
_t = threading.Thread(target=processor.initialize, daemon=True)
_t.start()
2025-08-22 17:17:51 +02:00
def initialize(engine_list: list[dict[str, t.Any]]):
"""Initialize all engines and store a processor for each engine in
:py:obj:`PROCESSORS`."""
for engine_data in engine_list:
2025-08-22 17:17:51 +02:00
engine_name: str = engine_data['name']
engine = engines.engines.get(engine_name)
if engine:
processor = get_processor(engine, engine_name)
if processor is None:
engine.logger.error('Error get processor for engine %s', engine_name)
else:
2025-08-22 17:17:51 +02:00
initialize_processor(processor)
PROCESSORS[engine_name] = processor