.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
__init__.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Implement request processors used by engine-types."""
3
4__all__ = [
5 "OfflineParamTypes",
6 "OnlineCurrenciesParams",
7 "OnlineDictParams",
8 "OnlineParamTypes",
9 "OnlineParams",
10 "OnlineUrlSearchParams",
11 "PROCESSORS",
12 "ParamTypes",
13 "RequestParams",
14]
15
16import typing as t
17
18from searx import logger
19from searx import engines
20
21from .abstract import EngineProcessor, RequestParams
22from .offline import OfflineProcessor
23from .online import OnlineProcessor, OnlineParams
24from .online_dictionary import OnlineDictionaryProcessor, OnlineDictParams
25from .online_currency import OnlineCurrencyProcessor, OnlineCurrenciesParams
26from .online_url_search import OnlineUrlSearchProcessor, OnlineUrlSearchParams
27
28logger = logger.getChild("search.processors")
29
30OnlineParamTypes: t.TypeAlias = OnlineParams | OnlineDictParams | OnlineCurrenciesParams | OnlineUrlSearchParams
31OfflineParamTypes: t.TypeAlias = RequestParams
32ParamTypes: t.TypeAlias = OfflineParamTypes | OnlineParamTypes
33
34
35class ProcessorMap(dict[str, EngineProcessor]):
36 """Class to manage :py:obj:`EngineProcessor` instances in a key/value map
37 (instances stored by *engine-name*)."""
38
39 processor_types: dict[str, type[EngineProcessor]] = {
40 OnlineProcessor.engine_type: OnlineProcessor,
41 OfflineProcessor.engine_type: OfflineProcessor,
42 OnlineDictionaryProcessor.engine_type: OnlineDictionaryProcessor,
43 OnlineCurrencyProcessor.engine_type: OnlineCurrencyProcessor,
44 OnlineUrlSearchProcessor.engine_type: OnlineUrlSearchProcessor,
45 }
46
47 def init(self, engine_list: list[dict[str, t.Any]]):
48 """Initialize all engines and registers a processor for each engine."""
49
50 for eng_settings in engine_list:
51 eng_name: str = eng_settings["name"]
52
53 if eng_settings.get("inactive", False) is True:
54 logger.info("Engine of name '%s' is inactive.", eng_name)
55 continue
56
57 eng_obj = engines.engines.get(eng_name)
58 if eng_obj is None:
59 logger.warning("Engine of name '%s' does not exists.", eng_name)
60 continue
61
62 eng_type = getattr(eng_obj, "engine_type", "online")
63 proc_cls = self.processor_types.get(eng_type)
64 if proc_cls is None:
65 logger.error("Engine '%s' is of unknown engine_type: %s", eng_type)
66 continue
67
68 # initialize (and register) the engine
69 eng_proc = proc_cls(eng_obj)
70 eng_proc.initialize(self.register_processor)
71
72 def register_processor(self, eng_proc: EngineProcessor, eng_proc_ok: bool) -> bool:
73 """Register the :py:obj:`EngineProcessor`.
74
75 This method is usually passed as a callback to the initialization of the
76 :py:obj:`EngineProcessor`.
77
78 The value (true/false) passed in ``eng_proc_ok`` indicates whether the
79 initialization of the :py:obj:`EngineProcessor` was successful; if this
80 is not the case, the processor is not registered.
81 """
82
83 if eng_proc_ok:
84 self[eng_proc.engine.name] = eng_proc
85 # logger.debug("registered engine processor: %s", eng_proc.engine.name)
86 else:
87 logger.error("init method of engine %s failed (%s).", eng_proc.engine.name)
88
89 return eng_proc_ok
90
91
92PROCESSORS = ProcessorMap()
93"""Global :py:obj:`ProcessorMap`.
94
95:meta hide-value:
96"""
init(self, list[dict[str, t.Any]] engine_list)
Definition __init__.py:47