__init__.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """This module holds the *data* created by::
  3. make data.all
  4. """
  5. __all__ = ["ahmia_blacklist_loader"]
  6. import json
  7. from pathlib import Path
  8. import typing
  9. from searx import logger
  10. log = logger.getChild("data")
  11. data_dir = Path(__file__).parent
  12. CURRENCIES: dict[str, typing.Any]
  13. USER_AGENTS: dict[str, typing.Any]
  14. EXTERNAL_URLS: dict[str, typing.Any]
  15. WIKIDATA_UNITS: dict[str, typing.Any]
  16. EXTERNAL_BANGS: dict[str, typing.Any]
  17. OSM_KEYS_TAGS: dict[str, typing.Any]
  18. ENGINE_DESCRIPTIONS: dict[str, typing.Any]
  19. ENGINE_TRAITS: dict[str, typing.Any]
  20. LOCALES: dict[str, typing.Any]
  21. lazy_globals = {
  22. "CURRENCIES": None,
  23. "USER_AGENTS": None,
  24. "EXTERNAL_URLS": None,
  25. "WIKIDATA_UNITS": None,
  26. "EXTERNAL_BANGS": None,
  27. "OSM_KEYS_TAGS": None,
  28. "ENGINE_DESCRIPTIONS": None,
  29. "ENGINE_TRAITS": None,
  30. "LOCALES": None,
  31. }
  32. data_json_files = {
  33. "CURRENCIES": "currencies.json",
  34. "USER_AGENTS": "useragents.json",
  35. "EXTERNAL_URLS": "external_urls.json",
  36. "WIKIDATA_UNITS": "wikidata_units.json",
  37. "EXTERNAL_BANGS": "external_bangs.json",
  38. "OSM_KEYS_TAGS": "osm_keys_tags.json",
  39. "ENGINE_DESCRIPTIONS": "engine_descriptions.json",
  40. "ENGINE_TRAITS": "engine_traits.json",
  41. "LOCALES": "locales.json",
  42. }
  43. def __getattr__(name):
  44. # lazy init of the global objects
  45. if name not in lazy_globals:
  46. raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
  47. data = lazy_globals[name]
  48. if data is not None:
  49. return data
  50. log.debug("init searx.data.%s", name)
  51. with open(data_dir / data_json_files[name], encoding='utf-8') as f:
  52. lazy_globals[name] = json.load(f)
  53. return lazy_globals[name]
  54. def ahmia_blacklist_loader():
  55. """Load data from `ahmia_blacklist.txt` and return a list of MD5 values of onion
  56. names. The MD5 values are fetched by::
  57. searxng_extra/update/update_ahmia_blacklist.py
  58. This function is used by :py:mod:`searx.plugins.ahmia_filter`.
  59. """
  60. with open(data_dir / 'ahmia_blacklist.txt', encoding='utf-8') as f:
  61. return f.read().split()