mirror of
https://github.com/searxng/searxng.git
synced 2025-12-31 16:10:01 +00:00
[mod] addition of various type hints / tbc
- pyright configuration [1]_ - stub files: types-lxml [2]_ - addition of various type hints - enable use of new type system features on older Python versions [3]_ - ``.tool-versions`` - set python to lowest version we support (3.10.18) [4]_: Older versions typically lack some typing features found in newer Python versions. Therefore, for local type checking (before commit), it is necessary to use the older Python interpreter. .. [1] https://docs.basedpyright.com/v1.20.0/configuration/config-files/ .. [2] https://pypi.org/project/types-lxml/ .. [3] https://typing-extensions.readthedocs.io/en/latest/# .. [4] https://mise.jdx.dev/configuration.html#tool-versions Signed-off-by: Markus Heiser <markus.heiser@darmarit.de> Format: reST
This commit is contained in:
committed by
Markus Heiser
parent
09500459fe
commit
57b9673efb
@@ -1,8 +1,5 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
"""Implement request processors used by engine-types.
|
||||
|
||||
"""
|
||||
"""Implement request processors used by engine-types."""
|
||||
|
||||
__all__ = [
|
||||
'EngineProcessor',
|
||||
@@ -14,8 +11,9 @@ __all__ = [
|
||||
'PROCESSORS',
|
||||
]
|
||||
|
||||
import typing as t
|
||||
|
||||
import threading
|
||||
from typing import Dict
|
||||
|
||||
from searx import logger
|
||||
from searx import engines
|
||||
@@ -27,15 +25,18 @@ from .online_currency import OnlineCurrencyProcessor
|
||||
from .online_url_search import OnlineUrlSearchProcessor
|
||||
from .abstract import EngineProcessor
|
||||
|
||||
if t.TYPE_CHECKING:
|
||||
from searx.enginelib import Engine
|
||||
|
||||
logger = logger.getChild('search.processors')
|
||||
PROCESSORS: Dict[str, EngineProcessor] = {}
|
||||
PROCESSORS: dict[str, EngineProcessor] = {}
|
||||
"""Cache request processors, stored by *engine-name* (:py:func:`initialize`)
|
||||
|
||||
:meta hide-value:
|
||||
"""
|
||||
|
||||
|
||||
def get_processor_class(engine_type):
|
||||
def get_processor_class(engine_type: str) -> type[EngineProcessor] | None:
|
||||
"""Return processor class according to the ``engine_type``"""
|
||||
for c in [
|
||||
OnlineProcessor,
|
||||
@@ -49,34 +50,35 @@ def get_processor_class(engine_type):
|
||||
return None
|
||||
|
||||
|
||||
def get_processor(engine, engine_name):
|
||||
"""Return processor instance that fits to ``engine.engine.type``)"""
|
||||
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)
|
||||
if processor_class:
|
||||
if processor_class is not None:
|
||||
return processor_class(engine, engine_name)
|
||||
return None
|
||||
|
||||
|
||||
def initialize_processor(processor):
|
||||
def initialize_processor(processor: EngineProcessor):
|
||||
"""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()
|
||||
_t = threading.Thread(target=processor.initialize, daemon=True)
|
||||
_t.start()
|
||||
|
||||
|
||||
def initialize(engine_list):
|
||||
"""Initialize all engines and store a processor for each engine in :py:obj:`PROCESSORS`."""
|
||||
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:
|
||||
engine_name = engine_data['name']
|
||||
engine_name: str = engine_data['name']
|
||||
engine = engines.engines.get(engine_name)
|
||||
if engine:
|
||||
processor = get_processor(engine, engine_name)
|
||||
initialize_processor(processor)
|
||||
if processor is None:
|
||||
engine.logger.error('Error get processor for engine %s', engine_name)
|
||||
else:
|
||||
initialize_processor(processor)
|
||||
PROCESSORS[engine_name] = processor
|
||||
|
||||
Reference in New Issue
Block a user