demo_offline.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Within this module we implement a *demo offline engine*. Do not look to
  3. close to the implementation, its just a simple example. To get in use of this
  4. *demo* engine add the following entry to your engines list in ``settings.yml``:
  5. .. code:: yaml
  6. - name: my offline engine
  7. engine: demo_offline
  8. shortcut: demo
  9. disabled: false
  10. """
  11. import json
  12. engine_type = 'offline'
  13. categories = ['general']
  14. disabled = True
  15. timeout = 2.0
  16. about = {
  17. "wikidata_id": None,
  18. "official_api_documentation": None,
  19. "use_official_api": False,
  20. "require_api_key": False,
  21. "results": 'JSON',
  22. }
  23. # if there is a need for globals, use a leading underline
  24. _my_offline_engine = None
  25. def init(engine_settings=None):
  26. """Initialization of the (offline) engine. The origin of this demo engine is a
  27. simple json string which is loaded in this example while the engine is
  28. initialized.
  29. """
  30. global _my_offline_engine # pylint: disable=global-statement
  31. _my_offline_engine = (
  32. '[ {"value": "%s"}'
  33. ', {"value":"first item"}'
  34. ', {"value":"second item"}'
  35. ', {"value":"third item"}'
  36. ']' % engine_settings.get('name')
  37. )
  38. def search(query, request_params):
  39. """Query (offline) engine and return results. Assemble the list of results from
  40. your local engine. In this demo engine we ignore the 'query' term, usual
  41. you would pass the 'query' term to your local engine to filter out the
  42. results.
  43. """
  44. ret_val = []
  45. result_list = json.loads(_my_offline_engine)
  46. for row in result_list:
  47. entry = {
  48. 'query': query,
  49. 'language': request_params['searxng_locale'],
  50. 'value': row.get("value"),
  51. # choose a result template or comment out to use the *default*
  52. 'template': 'key-value.html',
  53. }
  54. ret_val.append(entry)
  55. return ret_val