.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 self.load()
28 self.cache.properties.set("currencies loaded", "OK")
29 # F I X M E:
30 # do we need a maintenance .. rember: database is stored
31 # in /tmp and will be rebuild during the reboot anyway
32
33 def load(self):
34 log.debug("init searx.data.CURRENCIES")
35 with open(self.json_file, encoding="utf-8") as f:
36 data_dict = json.load(f)
37 for key, value in data_dict["names"].items():
38 self.cache.set(key=key, value=value, ctx=self.ctx_names, expire=None)
39 for key, value in data_dict["iso4217"].items():
40 self.cache.set(key=key, value=value, ctx=self.ctx_iso4217, expire=None)
41
42 def name_to_iso4217(self, name):
43 self.init()
44
45 ret_val = self.cache.get(key=name, default=name, ctx=self.ctx_names)
46 if isinstance(ret_val, list):
47 # if more alternatives, use the last in the list
48 ret_val = ret_val[-1]
49 return ret_val
50
51 def iso4217_to_name(self, iso4217, language):
52 self.init()
53
54 iso4217_languages: dict = self.cache.get(key=iso4217, default={}, ctx=self.ctx_iso4217)
55 return iso4217_languages.get(language, iso4217)
iso4217_to_name(self, iso4217, language)
Definition currencies.py:51