.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
__init__.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""This module holds the *data* created by::
3
4 make data.all
5
6"""
7# pylint: disable=invalid-name
8
9__all__ = ["ahmia_blacklist_loader", "data_dir", "get_cache"]
10
11import json
12import typing as t
13
14from .core import log, data_dir, get_cache
15from .currencies import CurrenciesDB
16from .tracker_patterns import TrackerPatternsDB
17
18
19class UserAgentType(t.TypedDict):
20 """Data structure of ``useragents.json``"""
21
22 os: list[str]
23 ua: str
24 versions: list[str]
25
26
27class WikiDataUnitType(t.TypedDict):
28 """Data structure of an item in ``wikidata_units.json``"""
29
30 si_name: str
31 symbol: str
32 to_si_factor: float
33
34
35class LocalesType(t.TypedDict):
36 """Data structure of an item in ``locales.json``"""
37
38 LOCALE_NAMES: dict[str, str]
39 RTL_LOCALES: list[str]
40
41
42USER_AGENTS: UserAgentType
43WIKIDATA_UNITS: dict[str, WikiDataUnitType]
44TRACKER_PATTERNS: TrackerPatternsDB
45LOCALES: LocalesType
46CURRENCIES: CurrenciesDB
47
48EXTERNAL_URLS: dict[str, dict[str, dict[str, str | dict[str, str]]]]
49EXTERNAL_BANGS: dict[str, dict[str, t.Any]]
50OSM_KEYS_TAGS: dict[str, dict[str, t.Any]]
51ENGINE_DESCRIPTIONS: dict[str, dict[str, t.Any]]
52ENGINE_TRAITS: dict[str, dict[str, t.Any]]
53
54
55lazy_globals = {
56 "CURRENCIES": CurrenciesDB(),
57 "USER_AGENTS": None,
58 "EXTERNAL_URLS": None,
59 "WIKIDATA_UNITS": None,
60 "EXTERNAL_BANGS": None,
61 "OSM_KEYS_TAGS": None,
62 "ENGINE_DESCRIPTIONS": None,
63 "ENGINE_TRAITS": None,
64 "LOCALES": None,
65 "TRACKER_PATTERNS": TrackerPatternsDB(),
66}
67
68data_json_files = {
69 "USER_AGENTS": "useragents.json",
70 "EXTERNAL_URLS": "external_urls.json",
71 "WIKIDATA_UNITS": "wikidata_units.json",
72 "EXTERNAL_BANGS": "external_bangs.json",
73 "OSM_KEYS_TAGS": "osm_keys_tags.json",
74 "ENGINE_DESCRIPTIONS": "engine_descriptions.json",
75 "ENGINE_TRAITS": "engine_traits.json",
76 "LOCALES": "locales.json",
77}
78
79
80def __getattr__(name: str) -> t.Any:
81 # lazy init of the global objects
82 if name not in lazy_globals:
83 raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
84
85 data = lazy_globals[name]
86 if data is not None:
87 return data
88
89 log.debug("init searx.data.%s", name)
90
91 with open(data_dir / data_json_files[name], encoding='utf-8') as f:
92 lazy_globals[name] = json.load(f)
93
94 return lazy_globals[name]
95
96
97def ahmia_blacklist_loader() -> list[str]:
98 """Load data from `ahmia_blacklist.txt` and return a list of MD5 values of onion
99 names. The MD5 values are fetched by::
100
101 searxng_extra/update/update_ahmia_blacklist.py
102
103 This function is used by :py:mod:`searx.plugins.ahmia_filter`.
104
105 """
106 with open(data_dir / 'ahmia_blacklist.txt', encoding='utf-8') as f:
107 return f.read().split()
list[str] ahmia_blacklist_loader()
Definition __init__.py:97
t.Any __getattr__(str name)
Definition __init__.py:80