summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexandre Flament <alexandre.flament@hesge.ch>2024-03-02 08:35:48 +0100
committerMarkus Heiser <markus.heiser@darmarIT.de>2024-05-05 17:35:29 +0200
commitac430a9eafdfdb96cc7a58e5fc5681cd5db8bc73 (patch)
tree33327ec8699ff367a16715cad29cec23a8d7528b
parentdbed8da284f0e91bef0fb42606ae10241505b364 (diff)
downloadsearx-ac430a9eafdfdb96cc7a58e5fc5681cd5db8bc73.tar.gz
searx-ac430a9eafdfdb96cc7a58e5fc5681cd5db8bc73.zip
Drop pytomlpp dependency for Python >= 3.11
Rely on tomllib for Python >= 3.11
-rw-r--r--requirements.txt2
-rw-r--r--searx/botdetection/config.py41
2 files changed, 34 insertions, 9 deletions
diff --git a/requirements.txt b/requirements.txt
index 98213e4d8..d47cc4c4b 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -15,4 +15,4 @@ setproctitle==1.3.3
redis==5.0.4
markdown-it-py==3.0.0
fasttext-predict==0.9.2.2
-pytomlpp==1.0.13
+pytomlpp==1.0.13; python_version < '3.11'
diff --git a/searx/botdetection/config.py b/searx/botdetection/config.py
index d7e3426cf..147104205 100644
--- a/searx/botdetection/config.py
+++ b/searx/botdetection/config.py
@@ -13,7 +13,18 @@ import copy
import typing
import logging
import pathlib
-import pytomlpp as toml
+
+try:
+ import tomllib
+
+ pytomlpp = None
+ USE_TOMLLIB = True
+except ImportError:
+ import pytomlpp
+
+ tomllib = None
+ USE_TOMLLIB = False
+
__all__ = ['Config', 'UNSET', 'SchemaIssue']
@@ -61,7 +72,7 @@ class Config:
# init schema
log.debug("load schema file: %s", schema_file)
- cfg = cls(cfg_schema=toml.load(schema_file), deprecated=deprecated)
+ cfg = cls(cfg_schema=toml_load(schema_file), deprecated=deprecated)
if not cfg_file.exists():
log.warning("missing config file: %s", cfg_file)
return cfg
@@ -69,12 +80,7 @@ class Config:
# load configuration
log.debug("load config file: %s", cfg_file)
- try:
- upd_cfg = toml.load(cfg_file)
- except toml.DecodeError as exc:
- msg = str(exc).replace('\t', '').replace('\n', ' ')
- log.error("%s: %s", cfg_file, msg)
- raise
+ upd_cfg = toml_load(cfg_file)
is_valid, issue_list = cfg.validate(upd_cfg)
for msg in issue_list:
@@ -176,6 +182,25 @@ class Config:
return getattr(m, name)
+def toml_load(file_name):
+ if USE_TOMLLIB:
+ # Python >= 3.11
+ try:
+ with open(file_name, "rb") as f:
+ return tomllib.load(f)
+ except tomllib.TOMLDecodeError as exc:
+ msg = str(exc).replace('\t', '').replace('\n', ' ')
+ log.error("%s: %s", file_name, msg)
+ raise
+ # fallback to pytomlpp for Python < 3.11
+ try:
+ return pytomlpp.load(file_name)
+ except pytomlpp.DecodeError as exc:
+ msg = str(exc).replace('\t', '').replace('\n', ' ')
+ log.error("%s: %s", file_name, msg)
+ raise
+
+
# working with dictionaries