From d6600b2a7927ae8c4b6d674bebde9a80174e0902 Mon Sep 17 00:00:00 2001 From: Bruno Rocha Date: Fri, 2 Sep 2022 20:20:34 +0100 Subject: [PATCH] Add test and docs about includes closes #794 --- Makefile | 1 + docs/configuration.md | 5 +++ docs/settings_files.md | 4 +++ dynaconf/base.py | 9 +++-- example/issues/794_includes/app.py | 36 +++++++++++++++++++ .../794_includes/conf/database/postgre.toml | 1 + .../issues/794_includes/conf/settings.toml | 2 ++ 7 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 example/issues/794_includes/app.py create mode 100644 example/issues/794_includes/conf/database/postgre.toml create mode 100644 example/issues/794_includes/conf/settings.toml diff --git a/Makefile b/Makefile index 4887eea81..d44bad4aa 100644 --- a/Makefile +++ b/Makefile @@ -132,6 +132,7 @@ test_examples: cd example/issues/729_use_default_when_setting_is_blank;pwd;python app.py cd example/issues/741_envvars_ignored;pwd;sh recreate.sh cd example/issues/705_flask_dynaconf_init;pwd;make test;make clean + cd example/issues/794_includes;pwd;python app.py test_vault: # @cd example/vault;pwd;python write.py diff --git a/docs/configuration.md b/docs/configuration.md index 113c52979..6eabcaa5f 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -208,6 +208,11 @@ After loading the files specified in `settings_files` dynaconf will load all the ``` **Includes allows the use of globs** +!!! warning + includes are loaded relative to the first loaded file, so if you have a `settings_files=['conf/settings.toml']` and `includes=["*.yaml"]` + the yaml files will be loaded relative to the `conf` folder. Unless you specify the absolute path or pass `root_path` to the Dynaconf + initializer. + --- ### **loaders** diff --git a/docs/settings_files.md b/docs/settings_files.md index ddd885a4a..2012c192f 100644 --- a/docs/settings_files.md +++ b/docs/settings_files.md @@ -133,6 +133,10 @@ key = value anotherkey = value ``` +!!! note + The paths passed to includes are relative to the `root_path` of Dynaconf instance or if that is not set, relative + to the directory where the first loaded file is located, includes also accepts globs and absolute paths. + --- ## Layered environments on files diff --git a/dynaconf/base.py b/dynaconf/base.py index 722298c77..cdccda02c 100644 --- a/dynaconf/base.py +++ b/dynaconf/base.py @@ -1074,18 +1074,16 @@ def load_file(self, path=None, env=None, silent=True, key=None): # continue the loop. continue - # python 3.6 does not resolve Pathlib basedirs - # issue #494 root_dir = str(self._root_path or os.getcwd()) + + # Issue #494 if ( isinstance(_filename, Path) and str(_filename.parent) in root_dir ): # pragma: no cover filepath = str(_filename) else: - filepath = os.path.join( - self._root_path or os.getcwd(), str(_filename) - ) + filepath = os.path.join(root_dir, str(_filename)) paths = [ p @@ -1095,6 +1093,7 @@ def load_file(self, path=None, env=None, silent=True, key=None): local_paths = [ p for p in sorted(glob.glob(filepath)) if ".local." in p ] + # Handle possible *.globs sorted alphanumeric for path in paths + local_paths: if path in already_loaded: # pragma: no cover diff --git a/example/issues/794_includes/app.py b/example/issues/794_includes/app.py new file mode 100644 index 000000000..cf43c054a --- /dev/null +++ b/example/issues/794_includes/app.py @@ -0,0 +1,36 @@ +from __future__ import annotations + +import os + +from dynaconf import Dynaconf + +# ISSUE 794 +# WHEN USING INCLUDES +# THE INCLUDES ARE LOADED RELATIVE TO +# THE FIRST LOADED FILE `conf/settings.toml` +settings = Dynaconf( + settings_files=["conf/settings.toml", "conf/.secrets.toml"], + includes=["**/*.toml"], +) + +assert settings.a == 3 +assert settings.b == 2 +assert settings.included_host == "0.0.0.0" + + +# SO.. +# WHEN USING INCLUDES IT IS RECOMMENDED +# TO SET THE ROOT PATH TO THE DIRECTORY +# WHERE THE SCRIPT IS RUNNING +# OR PASS AN ABSOLUTE PATH TO THE root_path or includes + +HERE = os.path.dirname(os.path.abspath(__file__)) +settings_with_root_path = Dynaconf( + root_path=HERE, + settings_files=["conf/settings.toml", "conf/.secrets.toml"], + includes=["conf/**/*.toml"], +) + +assert settings_with_root_path.a == 3 +assert settings_with_root_path.b == 2 +assert settings_with_root_path.included_host == "0.0.0.0" diff --git a/example/issues/794_includes/conf/database/postgre.toml b/example/issues/794_includes/conf/database/postgre.toml new file mode 100644 index 000000000..e293ebe8a --- /dev/null +++ b/example/issues/794_includes/conf/database/postgre.toml @@ -0,0 +1 @@ +included_host="0.0.0.0" diff --git a/example/issues/794_includes/conf/settings.toml b/example/issues/794_includes/conf/settings.toml new file mode 100644 index 000000000..56f6ebbbc --- /dev/null +++ b/example/issues/794_includes/conf/settings.toml @@ -0,0 +1,2 @@ +a=3 +b=2