__init__.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. from .tracker_patterns import TrackerPatternsDB
  12. CURRENCIES: CurrenciesDB
  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. TRACKER_PATTERNS: TrackerPatternsDB
  22. lazy_globals = {
  23. "CURRENCIES": CurrenciesDB(),
  24. "USER_AGENTS": None,
  25. "EXTERNAL_URLS": None,
  26. "WIKIDATA_UNITS": None,
  27. "EXTERNAL_BANGS": None,
  28. "OSM_KEYS_TAGS": None,
  29. "ENGINE_DESCRIPTIONS": None,
  30. "ENGINE_TRAITS": None,
  31. "LOCALES": None,
  32. "TRACKER_PATTERNS": TrackerPatternsDB(),
  33. }
  34. data_json_files = {
  35. "USER_AGENTS": "useragents.json",
  36. "EXTERNAL_URLS": "external_urls.json",
  37. "WIKIDATA_UNITS": "wikidata_units.json",
  38. "EXTERNAL_BANGS": "external_bangs.json",
  39. "OSM_KEYS_TAGS": "osm_keys_tags.json",
  40. "ENGINE_DESCRIPTIONS": "engine_descriptions.json",
  41. "ENGINE_TRAITS": "engine_traits.json",
  42. "LOCALES": "locales.json",
  43. }
  44. def __getattr__(name):
  45. # lazy init of the global objects
  46. if name not in lazy_globals:
  47. raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
  48. data = lazy_globals[name]
  49. if data is not None:
  50. return data
  51. log.debug("init searx.data.%s", name)
  52. with open(data_dir / data_json_files[name], encoding='utf-8') as f:
  53. lazy_globals[name] = json.load(f)
  54. return lazy_globals[name]
  55. def ahmia_blacklist_loader():
  56. """Load data from `ahmia_blacklist.txt` and return a list of MD5 values of onion
  57. names. The MD5 values are fetched by::
  58. searxng_extra/update/update_ahmia_blacklist.py
  59. This function is used by :py:mod:`searx.plugins.ahmia_filter`.
  60. """
  61. with open(data_dir / 'ahmia_blacklist.txt', encoding='utf-8') as f:
  62. return f.read().split()