From 1df1b9a49726194909ccf61b9bb4ea4999300463 Mon Sep 17 00:00:00 2001 From: Situphen Date: Mon, 7 Nov 2022 23:56:03 +0100 Subject: [PATCH] Remplacement de toml par tomli --- requirements.txt | 5 ++++- zds/settings/abstract_base/config.py | 12 ++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/requirements.txt b/requirements.txt index 86e58b0a2b..054281164b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -19,7 +19,6 @@ lxml==4.9.1 Pillow==9.2.0 pymemcache==3.5.2 requests==2.28.1 -toml==0.10.2 # Api dependencies django-cors-headers==3.13.0 @@ -33,3 +32,7 @@ drf_yasg==1.21.3 # Dependencies for slug generation, please be extra careful with those django-uuslug==2.0.0 python-slugify==6.1.2 + +# tomllib was added to the standard library in Python 3.11 +# tomli is only needed for older Python versions +tomli==2.0.1 ; python_version < "3.11" diff --git a/zds/settings/abstract_base/config.py b/zds/settings/abstract_base/config.py index 8c894d9d21..a1adcae4e8 100644 --- a/zds/settings/abstract_base/config.py +++ b/zds/settings/abstract_base/config.py @@ -1,13 +1,21 @@ import os from pathlib import Path -import toml + +# tomllib was added to the standard library in Python 3.11 +# tomli is only needed for older Python versions +# both libraries are strictly identical, only the name differs +try: + import tomllib +except ModuleNotFoundError: + import tomli as tomllib default_config_path = str(Path.cwd() / "config.toml") config_path = os.environ.get("ZDS_CONFIG", default_config_path) try: - config = toml.load(config_path) + with open(config_path, "rb") as f: + config = tomllib.load(f) print(f"Using the config file at {config_path!r}") except OSError: config = {}