__init__.py 2.0 KB

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