Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/detect casting comb token from converters #784

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion dynaconf/utils/parse_conf.py
Expand Up @@ -309,7 +309,8 @@ 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)
Expand Down
23 changes: 23 additions & 0 deletions 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
3 changes: 3 additions & 0 deletions example/custom_cast_token/settings.toml
@@ -0,0 +1,3 @@
[default]
parent = "@path @format {env[HOME]}/parent"
child = "@path @format {this.parent}/child"