.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
4from __future__ import annotations
5
6__all__ = ["CurrenciesDB"]
7
8import json
9import pathlib
10
11from .core import get_cache, log
12
13
15 # pylint: disable=missing-class-docstring
16
17 ctx_names = "data_currencies_names"
18 ctx_iso4217 = "data_currencies_iso4217"
19
20 json_file = 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 = 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):
44 self.init()
45
46 ret_val = self.cache.get(key=name, default=name, 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, language):
53 self.init()
54
55 iso4217_languages: dict = self.cache.get(key=iso4217, default={}, ctx=self.ctx_iso4217)
56 return iso4217_languages.get(language, iso4217)
iso4217_to_name(self, iso4217, language)
Definition currencies.py:52