diff --git a/benedict/dicts/io/io_dict.py b/benedict/dicts/io/io_dict.py index 84a2c2ad..77aa8c75 100644 --- a/benedict/dicts/io/io_dict.py +++ b/benedict/dicts/io/io_dict.py @@ -8,12 +8,14 @@ def __init__(self, *args, **kwargs): """ Constructs a new instance. """ - # if first argument is data-string, url or filepath try to decode it. + # if first argument is data-string, url or filepath (str or pathlib.Path) try to decode it. # use 'format' kwarg to specify the decoder to use, default 'json'. - if len(args) == 1 and type_util.is_string(args[0]): - d = IODict._decode_init(args[0], **kwargs) - super().__init__(d) - return + if len(args) == 1: + arg = args[0] + if type_util.is_string(arg) or type_util.is_path(arg): + d = IODict._decode_init(arg, **kwargs) + super().__init__(d) + return super().__init__(*args, **kwargs) @staticmethod diff --git a/benedict/dicts/io/io_util.py b/benedict/dicts/io/io_util.py index 1ae321f7..61ca645d 100644 --- a/benedict/dicts/io/io_util.py +++ b/benedict/dicts/io/io_util.py @@ -10,12 +10,14 @@ def autodetect_format(s): + s = str(s) if any([is_url(s), is_s3(s), is_filepath(s)]): return get_format_by_path(s) return None def decode(s, format, **kwargs): + s = str(s) serializer = get_serializer_by_format(format) if not serializer: raise ValueError(f"Invalid format: {format}.") @@ -34,6 +36,7 @@ def encode(d, format, filepath=None, **kwargs): options = kwargs.copy() content = serializer.encode(d, **options) if filepath: + filepath = str(filepath) write_content(filepath, content, **options) return content diff --git a/benedict/serializers/__init__.py b/benedict/serializers/__init__.py index 0c6ae678..48a35fed 100644 --- a/benedict/serializers/__init__.py +++ b/benedict/serializers/__init__.py @@ -65,6 +65,7 @@ def get_format_by_path(path): + path = str(path) path = path.lower() for extension in _SERIALIZERS_EXTENSIONS: if path.endswith(extension): diff --git a/benedict/utils/type_util.py b/benedict/utils/type_util.py index a3b17fa3..476d66e5 100644 --- a/benedict/utils/type_util.py +++ b/benedict/utils/type_util.py @@ -1,3 +1,4 @@ +import pathlib import re from datetime import datetime from decimal import Decimal @@ -70,6 +71,10 @@ def is_not_none(val): return val is not None +def is_path(val): + return isinstance(val, pathlib.Path) + + def is_regex(val): return isinstance(val, regex) diff --git a/requirements.txt b/requirements.txt index bc3100d2..05f6b61d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,7 @@ mailchecker == 5.0.5 openpyxl == 3.0.10 phonenumbers == 8.13.3 python-dateutil == 2.8.2 -python-fsutil == 0.8.0 +python-fsutil == 0.9.0 python-slugify == 7.0.0 pyyaml == 6.0 requests == 2.28.1 diff --git a/setup.cfg b/setup.cfg index ffd78784..dc79bc3a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -99,7 +99,7 @@ install_requires = openpyxl >= 3.0.0, < 4.0.0 phonenumbers >= 8.12.0, < 9.0.0 python-dateutil >= 2.8.0, < 3.0.0 - python-fsutil >= 0.6.0, < 1.0.0 + python-fsutil >= 0.9.0, < 1.0.0 python-slugify >= 6.0.1, < 8.0.0 pyyaml >= 6.0, < 7.0 requests >= 2.26.0, < 3.0.0 diff --git a/tests/github/test_issue_0144.json b/tests/github/test_issue_0144.json new file mode 100644 index 00000000..138e3fbd --- /dev/null +++ b/tests/github/test_issue_0144.json @@ -0,0 +1 @@ +{"a": 1, "b": 2, "c": 3, "x": 7, "y": 8, "z": 9} diff --git a/tests/github/test_issue_0144.py b/tests/github/test_issue_0144.py new file mode 100644 index 00000000..f44346a2 --- /dev/null +++ b/tests/github/test_issue_0144.py @@ -0,0 +1,25 @@ +import pathlib +import unittest + +from benedict import benedict + + +class github_issue_0144_test_case(unittest.TestCase): + """ + This class describes a github issue 0144 test case. + https://github.com/fabiocaccamo/python-benedict/issues/144 + + To run this specific test: + - Run python -m unittest tests.github.test_issue_0144 + """ + + def test_init_with_pathlib_path_object_and_valid_path(self): + # print(pathlib.Path("./test_issue_0144.json")) + d = benedict(pathlib.Path("tests/github/test_issue_0144.json"), format="json") + self.assertEqual(d, {"a": 1, "b": 2, "c": 3, "x": 7, "y": 8, "z": 9}) + + def test_init_with_pathlib_path_object_and_invalid_path(self): + with self.assertRaises(ValueError): + benedict( + pathlib.Path("tests/github/test_issue_0144_invalid.json"), format="json" + )