.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
from
__future__
import
annotations
5
6
__all__ = [
"CurrenciesDB"
]
7
8
import
json
9
import
pathlib
10
11
from
.core
import
get_cache, log
12
13
14
class
CurrenciesDB
:
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)
searx.data.currencies.CurrenciesDB
Definition
currencies.py:14
searx.data.currencies.CurrenciesDB.cache
cache
Definition
currencies.py:23
searx.data.currencies.CurrenciesDB.name_to_iso4217
name_to_iso4217(self, name)
Definition
currencies.py:42
searx.data.currencies.CurrenciesDB.json_file
str json_file
Definition
currencies.py:20
searx.data.currencies.CurrenciesDB.load
load(self)
Definition
currencies.py:33
searx.data.currencies.CurrenciesDB.iso4217_to_name
iso4217_to_name(self, iso4217, language)
Definition
currencies.py:51
searx.data.currencies.CurrenciesDB.ctx_iso4217
str ctx_iso4217
Definition
currencies.py:18
searx.data.currencies.CurrenciesDB.ctx_names
str ctx_names
Definition
currencies.py:17
searx.data.currencies.CurrenciesDB.__init__
__init__(self)
Definition
currencies.py:22
searx.data.currencies.CurrenciesDB.init
init(self)
Definition
currencies.py:25
searxng
searx
data
currencies.py
Generated on Sun Jun 1 2025 21:11:46 for .oO SearXNG Developer Documentation Oo. by
1.13.2