Skip to content

Commit

Permalink
refactor: use tomlkit instead of toml and tomllib
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Jan 23, 2025
1 parent 8bf4f2e commit 574660b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 43 deletions.
34 changes: 11 additions & 23 deletions canaille/app/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import smtplib
import socket
import sys

from flask import current_app
from pydantic import ValidationError
Expand All @@ -12,6 +11,12 @@

from canaille.core.configuration import CoreSettings

try:
import tomlkit

HAS_TOMLKIT = True
except ImportError:
HAS_TOMLKIT = False
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


Expand Down Expand Up @@ -129,24 +134,6 @@ class ConfigurationException(Exception):
pass


def toml_content(file_path):
try:
if sys.version_info < (3, 11): # pragma: no cover
import toml

return toml.load(file_path)

import tomllib

with open(file_path, "rb") as fd:
return tomllib.load(fd)

except ImportError as exc:
raise Exception(
"toml library not installed. Cannot load configuration."
) from exc


def setup_config(app, config=None, test_config=True, env_file=None, env_prefix=""):
from canaille.oidc.installation import install

Expand All @@ -155,19 +142,20 @@ def setup_config(app, config=None, test_config=True, env_file=None, env_prefix="
"SESSION_COOKIE_NAME": "canaille",
}
)
if not config and "CONFIG" in os.environ:
config = toml_content(os.environ.get("CONFIG"))
if HAS_TOMLKIT and not config and "CONFIG" in os.environ:
with open(os.environ.get("CONFIG")) as fd:
config = tomlkit.load(fd)

env_file = env_file or os.getenv("ENV_FILE")
try:
config_obj = settings_factory(
app.config_obj = settings_factory(
config or {}, env_file=env_file, env_prefix=env_prefix
)
except ValidationError as exc: # pragma: no cover
app.logger.critical(str(exc))
return False

config_dict = config_obj.model_dump()
config_dict = app.config_obj.model_dump()
app.no_secret_key = config_dict["SECRET_KEY"] is None
app.config.from_mapping(config_dict)

Expand Down
5 changes: 3 additions & 2 deletions doc/doc_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from urllib.parse import urlparse
from wsgiref.simple_server import WSGIRequestHandler

from canaille.app.configuration import toml_content
import tomlkit

sys.path.insert(0, os.path.abspath(".."))
from demo.demoapp import create_app
Expand All @@ -16,7 +16,8 @@ def create_doc_app(sphinx_app):
conf_path = (
pathlib.Path(__file__).parent.parent / "demo" / "conf" / "canaille-memory.toml"
)
conf = toml_content(str(conf_path))
with open(conf_path) as fd:
conf = dict(tomlkit.load(fd))
conf["CANAILLE"]["SECRET_KEY"] = "doc"
conf["CANAILLE"]["SMTP"] = {"HOST": "localhost"}
conf["CANAILLE"]["LANGUAGE"] = sphinx_app.config["language"]
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ front = [
"flask-themer >= 2.0.0",
"pycountry >= 23.12.7",
"pytz >= 2022.7",
"toml >= 0.10.0",
"tomlkit>=0.13.2",
"zxcvbn-rs-py >= 0.2.0",
]

Expand Down Expand Up @@ -127,7 +127,6 @@ dev = [
"scim2-tester>=0.1.7",
"slapd >= 0.1.5",
"time-machine >= 2.14.1",
"toml >= 0.10.0",
"tox-uv >= 1.16.0",
# Babel 2.14 does not directly depend on setuptools
# https://github.com/python-babel/babel/blob/40e60a1f6cf178d9f57fcc14f157ea1b2ab77361/CHANGES.rst?plain=1#L22-L24
Expand All @@ -137,6 +136,7 @@ dev = [
]
doc = [
"autodoc-pydantic >= 2.0.1",
"jinja-autodoc>=0.1.2",
# used to compute sphinx uuids
# https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-gettext_uuid
"levenshtein>=0.26.1",
Expand All @@ -147,7 +147,7 @@ doc = [
"sphinx-issues >= 5.0.0",
"sphinx-click >= 6.0.0",
"sphinx-intl>=2.3.0",
"jinja-autodoc>=0.1.2",
"tomlkit>=0.13.2",
]
demo = [
"faker",
Expand Down
4 changes: 2 additions & 2 deletions tests/app/test_flaskutils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

import toml
import tomlkit

from canaille import create_app
from canaille.app.flask import set_parameter_in_url_query
Expand All @@ -26,7 +26,7 @@ def test_set_parameter_in_url_query():
def test_environment_configuration(configuration, tmp_path):
config_path = os.path.join(tmp_path, "config.toml")
with open(config_path, "w") as fd:
toml.dump(configuration, fd)
tomlkit.dump(configuration, fd)

os.environ["CONFIG"] = config_path
app = create_app()
Expand Down
26 changes: 13 additions & 13 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 574660b

Please sign in to comment.