From fd40f796094d23bdf3f364cd990ba1423b9ae220 Mon Sep 17 00:00:00 2001 From: Joren Retel Date: Thu, 4 Aug 2022 18:50:12 +0200 Subject: [PATCH 1/2] Changed regex that detects cating combination token --- dynaconf/utils/parse_conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynaconf/utils/parse_conf.py b/dynaconf/utils/parse_conf.py index 902ba2725..7085a9231 100644 --- a/dynaconf/utils/parse_conf.py +++ b/dynaconf/utils/parse_conf.py @@ -309,7 +309,7 @@ def _parse_conf_data(data, tomlfy=False, box_settings=None): ): # Check combination token is used comb_token = re.match( - r"^@(str|int|float|bool|json) @(jinja|format)", data + f"^({'|'.join(converters.keys())}) @(jinja|format)", data, ) if comb_token: tokens = comb_token.group(0) From 503ddc071575507547e12cad17cd3b0eb0ea385e Mon Sep 17 00:00:00 2001 From: Joren Retel Date: Thu, 4 Aug 2022 19:35:05 +0200 Subject: [PATCH 2/2] Added example of adding custom casting token --- dynaconf/utils/parse_conf.py | 3 ++- example/custom_cast_token/app.py | 23 +++++++++++++++++++++++ example/custom_cast_token/settings.toml | 3 +++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 example/custom_cast_token/app.py create mode 100644 example/custom_cast_token/settings.toml diff --git a/dynaconf/utils/parse_conf.py b/dynaconf/utils/parse_conf.py index 7085a9231..5605393b5 100644 --- a/dynaconf/utils/parse_conf.py +++ b/dynaconf/utils/parse_conf.py @@ -309,7 +309,8 @@ def _parse_conf_data(data, tomlfy=False, box_settings=None): ): # Check combination token is used comb_token = re.match( - f"^({'|'.join(converters.keys())}) @(jinja|format)", data, + f"^({'|'.join(converters.keys())}) @(jinja|format)", + data, ) if comb_token: tokens = comb_token.group(0) diff --git a/example/custom_cast_token/app.py b/example/custom_cast_token/app.py new file mode 100644 index 000000000..403e548d3 --- /dev/null +++ b/example/custom_cast_token/app.py @@ -0,0 +1,23 @@ +from __future__ import annotations + +from pathlib import Path + +from dynaconf import Dynaconf +from dynaconf.utils import parse_conf + +settings = Dynaconf( + envvar_prefix="DYNACONF", + settings_files=["settings.toml"], + environments=True, +) + +# Add a custom casting token casting string to pathlib.Path object +parse_conf.converters["@path"] = ( + lambda value: value.set_casting(Path) + if isinstance(value, parse_conf.Lazy) + else Path(value) +) + +assert isinstance(settings.parent, Path) +assert isinstance(settings.child, Path) +assert str(settings.child).find("@format") == -1 diff --git a/example/custom_cast_token/settings.toml b/example/custom_cast_token/settings.toml new file mode 100644 index 000000000..bdda15da3 --- /dev/null +++ b/example/custom_cast_token/settings.toml @@ -0,0 +1,3 @@ +[default] +parent = "@path @format {env[HOME]}/parent" +child = "@path @format {this.parent}/child"