.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
currencies.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Simple implementation to store currencies data in a SQL database."""
3
4__all__ = ["CurrenciesDB"]
5
6import typing as t
7import json
8import pathlib
9
10from .core import get_cache, log
11
12
13@t.final
15 # pylint: disable=missing-class-docstring
16
17 ctx_names: str = "data_currencies_names"
18 ctx_iso4217: str = "data_currencies_iso4217"
19
20 json_file: pathlib.Path = pathlib.Path(__file__).parent / "currencies.json"
21
22 def __init__(self):
23 self.cache = get_cache()
24
25 def init(self):
26 if self.cache.properties("currencies loaded") != "OK":
27 # To avoid parallel initializations, the property is set first
28 self.cache.properties.set("currencies loaded", "OK")
29 self.load()
30 # F I X M E:
31 # do we need a maintenance .. rember: database is stored
32 # in /tmp and will be rebuild during the reboot anyway
33
34 def load(self):
35 log.debug("init searx.data.CURRENCIES")
36 with open(self.json_file, encoding="utf-8") as f:
37 data_dict: dict[str, dict[str, str]] = json.load(f)
38 for key, value in data_dict["names"].items():
39 self.cache.set(key=key, value=value, ctx=self.ctx_names, expire=None)
40 for key, value in data_dict["iso4217"].items():
41 self.cache.set(key=key, value=value, ctx=self.ctx_iso4217, expire=None)
42
43 def name_to_iso4217(self, name: str) -> str | None:
44 self.init()
45
46 ret_val: str | list[str] | None = self.cache.get(key=name, default=None, ctx=self.ctx_names)
47 if isinstance(ret_val, list):
48 # if more alternatives, use the last in the list
49 ret_val = ret_val[-1]
50 return ret_val
51
52 def iso4217_to_name(self, iso4217: str, language: str) -> str | None:
53 self.init()
54
55 iso4217_languages: dict[str, str] = self.cache.get(key=iso4217, default={}, ctx=self.ctx_iso4217)
56 return iso4217_languages.get(language)
57
58 def is_iso4217(self, iso4217: str) -> bool:
59 item = self.cache.get(key=iso4217, default={}, ctx=self.ctx_iso4217)
60 return bool(item)
bool is_iso4217(self, str iso4217)
Definition currencies.py:58
str|None name_to_iso4217(self, str name)
Definition currencies.py:43
str|None iso4217_to_name(self, str iso4217, str language)
Definition currencies.py:52