Skip to content

Commit

Permalink
Allow load_file to accept pathlib.Path (#525)
Browse files Browse the repository at this point in the history
* Allow load_file to accept pathlib.Path

Fix #494

* python 3.6 can't handle Pathlib base path addition to os.path
  • Loading branch information
rochacbruno committed Feb 27, 2021
1 parent 293656f commit b077737
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ test_examples:
cd example/issues/430_same_name;pwd;python app.py
cd example/issues/443_object_merge;pwd;python app.py
cd example/issues/445_casting;pwd;python app.py
cd example/issues/494_using_pathlib;pwd;python app.py
cd example/issues/519_underscore_in_name;pwd;ATC_BLE__device_id=42 EXPECTED_VALUE=42 python app.py
cd example/issues/519_underscore_in_name;pwd;ATC_BLE__DEVICE_ID=42 EXPECTED_VALUE=42 python app.py

Expand Down
17 changes: 14 additions & 3 deletions dynaconf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import warnings
from contextlib import contextmanager
from contextlib import suppress
from pathlib import Path

from dynaconf import default_settings
from dynaconf.loaders import default_loader
Expand Down Expand Up @@ -1001,9 +1002,19 @@ def load_file(self, path=None, env=None, silent=True, key=None):
# continue the loop.
continue

filepath = os.path.join(
self._root_path or os.getcwd(), _filename
)
# python 3.6 does not resolve Pathlib basedirs
# issue #494
root_dir = str(self._root_path or os.getcwd())
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)
)

paths = [
p
for p in sorted(glob.glob(filepath))
Expand Down
2 changes: 1 addition & 1 deletion dynaconf/loaders/py_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def try_to_load_from_py_module_name(
ctx = suppress(ImportError, TypeError) if silent else suppress()

with ctx:
mod = importlib.import_module(name)
mod = importlib.import_module(str(name))
load_from_python_object(obj, mod, name, key, identifier)
return True # loaded ok!
# if it reaches this point that means exception occurred, module not found.
Expand Down
16 changes: 16 additions & 0 deletions example/issues/494_using_pathlib/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from pathlib import Path

from dynaconf import Dynaconf

base_dir = Path(__file__).parent / Path("folder")

settings = Dynaconf(settings_files=[base_dir / "settings.yaml"])

# __import__("pdbr").set_trace()
settings.load_file(path=base_dir / Path("new.yaml"))

print(settings._loaded_files)
assert len(settings._loaded_files) == 2

assert settings.NAME == "Booba"
assert settings.NUMBER == 42
1 change: 1 addition & 0 deletions example/issues/494_using_pathlib/folder/new.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NUMBER: 42
1 change: 1 addition & 0 deletions example/issues/494_using_pathlib/folder/settings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NAME: Booba
2 changes: 1 addition & 1 deletion requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pre-commit
ipython
ipdb
python-minifier
pdbr
pdbr[ipython]

# documentation
sphinx
Expand Down

0 comments on commit b077737

Please sign in to comment.