| 
									
										
										
										
											2022-03-12 10:18:08 +01:00
										 |  |  | #!/usr/bin/env python | 
					
						
							|  |  |  | # lint: pylint | 
					
						
							|  |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | """Script that implements some prebuild tasks needed by target docs.prebuild | 
					
						
							|  |  |  | """ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | import os.path | 
					
						
							|  |  |  | import time | 
					
						
							| 
									
										
										
										
											2022-03-13 22:15:27 +01:00
										 |  |  | from contextlib import contextmanager | 
					
						
							| 
									
										
										
										
											2022-06-14 16:31:41 +02:00
										 |  |  | from searx import settings, get_setting, locales | 
					
						
							| 
									
										
										
										
											2022-03-12 10:18:08 +01:00
										 |  |  | from searx.infopage import InfoPageSet, InfoPage | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-13 22:15:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-12 10:18:08 +01:00
										 |  |  | _doc_user = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'docs', 'user')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-13 22:15:27 +01:00
										 |  |  | def main(): | 
					
						
							| 
									
										
										
										
											2022-06-14 16:31:41 +02:00
										 |  |  |     locales.locales_initialize() | 
					
						
							| 
									
										
										
										
											2022-03-12 10:18:08 +01:00
										 |  |  |     base_url = get_setting('server.base_url', None) | 
					
						
							|  |  |  |     if base_url: | 
					
						
							| 
									
										
										
										
											2022-03-13 22:15:27 +01:00
										 |  |  |         infopageset_ctx = _instance_infosetset_ctx(base_url) | 
					
						
							| 
									
										
										
										
											2022-03-12 10:18:08 +01:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2022-03-13 22:15:27 +01:00
										 |  |  |         infopageset_ctx = _offline_infosetset_ctx() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with infopageset_ctx as infopageset: | 
					
						
							| 
									
										
										
										
											2022-03-16 22:24:35 +01:00
										 |  |  |         for _, _, page in infopageset.iter_pages('en'): | 
					
						
							| 
									
										
										
										
											2022-03-13 22:15:27 +01:00
										 |  |  |             fname = os.path.join(_doc_user, os.path.basename(page.fname)) | 
					
						
							|  |  |  |             with open(fname, 'w') as f: | 
					
						
							|  |  |  |                 f.write(page.content) | 
					
						
							| 
									
										
										
										
											2022-03-12 10:18:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class OfflinePage(InfoPage): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def get_ctx(self):  # pylint: disable=no-self-use | 
					
						
							|  |  |  |         """Jinja context to render :py:obj:`DocPage.content` for offline purpose (no | 
					
						
							|  |  |  |         links to SearXNG instance)""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ctx = super().get_ctx() | 
					
						
							|  |  |  |         ctx['link'] = lambda name, url: '`%s`' % name | 
					
						
							|  |  |  |         ctx['search'] = lambda query: '`%s`' % query | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return ctx | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-13 22:15:27 +01:00
										 |  |  | @contextmanager | 
					
						
							|  |  |  | def _offline_infosetset_ctx(): | 
					
						
							|  |  |  |     yield InfoPageSet(OfflinePage) | 
					
						
							| 
									
										
										
										
											2022-03-12 10:18:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-13 22:15:27 +01:00
										 |  |  | @contextmanager | 
					
						
							|  |  |  | def _instance_infosetset_ctx(base_url): | 
					
						
							| 
									
										
										
										
											2022-03-12 10:18:08 +01:00
										 |  |  |     # The url_for functions in the jinja templates need all routes to be | 
					
						
							|  |  |  |     # registered in the Flask app. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-13 22:15:27 +01:00
										 |  |  |     settings['server']['secret_key'] = '' | 
					
						
							| 
									
										
										
										
											2022-03-12 10:18:08 +01:00
										 |  |  |     from searx.webapp import app | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Specify base_url so that url_for() works for base_urls.  If base_url is | 
					
						
							|  |  |  |     # specified, then these values from are given preference over any Flask's | 
					
						
							|  |  |  |     # generics (see flaskfix.py). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with app.test_request_context(base_url=base_url): | 
					
						
							| 
									
										
										
										
											2022-03-13 22:15:27 +01:00
										 |  |  |         yield InfoPageSet() | 
					
						
							| 
									
										
										
										
											2022-03-12 10:18:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 09:53:03 +02:00
										 |  |  |     # The searx.webapp import from above fires some HTTP requests, that's | 
					
						
							| 
									
										
										
										
											2022-03-12 10:18:08 +01:00
										 |  |  |     # why we get a RuntimeError:: | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     #     RuntimeError: The connection pool was closed while 1 HTTP \ | 
					
						
							|  |  |  |     #       requests/responses were still in-flight. | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # Closing network won't help .. | 
					
						
							|  |  |  |     #   from searx.network import network | 
					
						
							|  |  |  |     #   network.done() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # waiting some seconds before ending the comand line was the only solution I | 
					
						
							|  |  |  |     # found .. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     time.sleep(3) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     sys.exit(main()) |