| 
									
										
										
										
											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', | 
					
						
							| 
									
										
										
										
											2022-01-30 16:05:08 +01:00
										 |  |  |     'OnlineUrlSearchProcessor', | 
					
						
							| 
									
										
										
										
											2021-05-05 13:08:54 +02:00
										 |  |  |     'PROCESSORS', | 
					
						
							| 
									
										
										
										
											2021-04-27 15:13:39 +02:00
										 |  |  | ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 13:08:54 +02:00
										 |  |  | import threading | 
					
						
							| 
									
										
										
										
											2022-01-17 09:04:51 +01:00
										 |  |  | from typing import Dict | 
					
						
							| 
									
										
										
										
											2021-05-05 13:08:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-27 15:13:39 +02:00
										 |  |  | from searx import logger | 
					
						
							| 
									
										
										
										
											2021-07-03 17:51:39 +02:00
										 |  |  | from searx import 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 | 
					
						
							| 
									
										
										
										
											2022-01-30 16:05:08 +01:00
										 |  |  | from .online_url_search import OnlineUrlSearchProcessor | 
					
						
							| 
									
										
										
										
											2020-12-16 13:41:32 +01:00
										 |  |  | from .abstract import EngineProcessor | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | logger = logger.getChild('search.processors') | 
					
						
							| 
									
										
										
										
											2022-01-17 09:04:51 +01:00
										 |  |  | PROCESSORS: Dict[str, EngineProcessor] = {} | 
					
						
							| 
									
										
										
										
											2022-09-29 20:54:46 +02:00
										 |  |  | """Cache request processores, stored by *engine-name* (:py:func:`initialize`)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :meta hide-value: | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2020-12-16 13:41:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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``""" | 
					
						
							| 
									
										
										
										
											2022-01-30 16:05:08 +01:00
										 |  |  |     for c in [ | 
					
						
							|  |  |  |         OnlineProcessor, | 
					
						
							|  |  |  |         OfflineProcessor, | 
					
						
							|  |  |  |         OnlineDictionaryProcessor, | 
					
						
							|  |  |  |         OnlineCurrencyProcessor, | 
					
						
							|  |  |  |         OnlineUrlSearchProcessor, | 
					
						
							|  |  |  |     ]: | 
					
						
							| 
									
										
										
										
											2020-12-16 13:41:32 +01:00
										 |  |  |         if c.engine_type == engine_type: | 
					
						
							|  |  |  |             return c | 
					
						
							|  |  |  |     return None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 13:08:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 13:41:32 +01:00
										 |  |  | 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 13:08:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | def initialize_processor(processor): | 
					
						
							|  |  |  |     """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() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 13:41:32 +01:00
										 |  |  | def initialize(engine_list): | 
					
						
							| 
									
										
										
										
											2021-05-05 13:08:54 +02:00
										 |  |  |     """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 = engines.engines.get(engine_name) | 
					
						
							|  |  |  |         if engine: | 
					
						
							|  |  |  |             processor = get_processor(engine, engine_name) | 
					
						
							|  |  |  |             initialize_processor(processor) | 
					
						
							|  |  |  |             if processor is None: | 
					
						
							| 
									
										
										
										
											2021-09-06 19:46:08 +02:00
										 |  |  |                 engine.logger.error('Error get processor for engine %s', engine_name) | 
					
						
							| 
									
										
										
										
											2021-05-05 13:08:54 +02:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 PROCESSORS[engine_name] = processor |