.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
3"""Implement request processors used by engine-types.
4
5"""
6
7__all__ = [
8 'EngineProcessor',
9 'OfflineProcessor',
10 'OnlineProcessor',
11 'OnlineDictionaryProcessor',
12 'OnlineCurrencyProcessor',
13 'OnlineUrlSearchProcessor',
14 'PROCESSORS',
15]
16
17import threading
18from typing import Dict
19
20from searx import logger
21from searx import engines
22
23from .online import OnlineProcessor
24from .offline import OfflineProcessor
25from .online_dictionary import OnlineDictionaryProcessor
26from .online_currency import OnlineCurrencyProcessor
27from .online_url_search import OnlineUrlSearchProcessor
28from .abstract import EngineProcessor
29
30logger = logger.getChild('search.processors')
31PROCESSORS: Dict[str, EngineProcessor] = {}
32"""Cache request processors, stored by *engine-name* (:py:func:`initialize`)
33
34:meta hide-value:
35"""
36
37
38def get_processor_class(engine_type):
39 """Return processor class according to the ``engine_type``"""
40 for c in [
41 OnlineProcessor,
42 OfflineProcessor,
43 OnlineDictionaryProcessor,
44 OnlineCurrencyProcessor,
45 OnlineUrlSearchProcessor,
46 ]:
47 if c.engine_type == engine_type:
48 return c
49 return None
50
51
52def get_processor(engine, engine_name):
53 """Return processor instance that fits to ``engine.engine.type``)"""
54 engine_type = getattr(engine, 'engine_type', 'online')
55 processor_class = get_processor_class(engine_type)
56 if processor_class:
57 return processor_class(engine, engine_name)
58 return None
59
60
61def initialize_processor(processor):
62 """Initialize one processor
63
64 Call the init function of the engine
65 """
66 if processor.has_initialize_function:
67 t = threading.Thread(target=processor.initialize, daemon=True)
68 t.start()
69
70
71def initialize(engine_list):
72 """Initialize all engines and store a processor for each engine in :py:obj:`PROCESSORS`."""
73 for engine_data in engine_list:
74 engine_name = engine_data['name']
75 engine = engines.engines.get(engine_name)
76 if engine:
77 processor = get_processor(engine, engine_name)
78 initialize_processor(processor)
79 if processor is None:
80 engine.logger.error('Error get processor for engine %s', engine_name)
81 else:
82 PROCESSORS[engine_name] = processor
initialize(engine_list)
Definition __init__.py:71
get_processor(engine, engine_name)
Definition __init__.py:52
get_processor_class(engine_type)
Definition __init__.py:38
initialize_processor(processor)
Definition __init__.py:61