From 4cca0e18f912763228d1bf6eba44fe89122f3aa1 Mon Sep 17 00:00:00 2001 From: Joren Retel Date: Thu, 18 Aug 2022 11:05:45 +0200 Subject: [PATCH 1/2] Adding documentation and example to makefile. --- Makefile | 1 + docs/envvars.md | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/Makefile b/Makefile index 1708f7c23..4887eea81 100644 --- a/Makefile +++ b/Makefile @@ -60,6 +60,7 @@ test_examples: cd example/settings_file/;pwd;rm -rf /tmp/settings_file_test/settings.py;mkdir -p /tmp/settings_file_test/;echo "MESSAGE = 'Hello from tmp'" > /tmp/settings_file_test/settings.py;python app.py;rm -rf /tmp/settings_file_test/ cd example/configure/;pwd;rm -rf /tmp/configure_test/settings.py;mkdir -p /tmp/configure_test/;echo "MESSAGE = 'Hello from tmp'" > /tmp/configure_test/settings.py;python app.py;rm -rf /tmp/configure_test/ cd example/-m_case/;pwd;python -m module + cd example/custom_cast_token;pwd;python app.py @echo '############### Calling from outer folder ###############' python example/common/program.py diff --git a/docs/envvars.md b/docs/envvars.md index 0960c9dc8..668a81f57 100644 --- a/docs/envvars.md +++ b/docs/envvars.md @@ -144,6 +144,35 @@ export PREFIX_PATH='@format {env{"HOME"}/.config/{this.DB_NAME}' export PREFIX_PATH='@jinja {{env.HOME}}/.config/{{this.DB_NAME}} | abspath' ``` +## Adding a Custom Casting Token + +If you would like to add a custom casting token, you can do so by adding a +converter. For example, if we would like to cast strings to a pathlib.Path +object we can add in our python code: + +```python +# app.py +from pathlib import Path +from dynaconf.utils import parse_conf + +parse_conf.converters["@path"] = ( + lambda value: value.set_casting(Path) + if isinstance(value, parse_conf.Lazy) + else Path(value) +) +``` + +In the settings file we can now use teh @path casting token. Like with other +casting tokens you can also combine them: + +```toml +# settings.toml +my_path = "@path /home/foo/example.txt" +parent = "@path @format {env[HOME]}/parent" +child = "@path @format {this.parent}/child" +``` + + ## Environment variables filtering All environment variables (naturally, accounting for prefix rules) will be From 9491745535a0b4e487e7fd1e28bf967cd3d3a719 Mon Sep 17 00:00:00 2001 From: Joren Retel Date: Thu, 18 Aug 2022 11:13:02 +0200 Subject: [PATCH 2/2] Put header one level down in docs. --- docs/envvars.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/envvars.md b/docs/envvars.md index 668a81f57..ee9ef082e 100644 --- a/docs/envvars.md +++ b/docs/envvars.md @@ -144,7 +144,7 @@ export PREFIX_PATH='@format {env{"HOME"}/.config/{this.DB_NAME}' export PREFIX_PATH='@jinja {{env.HOME}}/.config/{{this.DB_NAME}} | abspath' ``` -## Adding a Custom Casting Token +### Adding a Custom Casting Token If you would like to add a custom casting token, you can do so by adding a converter. For example, if we would like to cast strings to a pathlib.Path