Skip to content

Commit

Permalink
refactor: rename _load_data to load_data and expose it for the library
Browse files Browse the repository at this point in the history
  • Loading branch information
robinvandernoord committed Sep 26, 2023
1 parent 48085c4 commit 7f3a534
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/configuraptor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
check_and_convert_data,
convert_config,
ensure_types,
load_data,
load_into,
load_into_class,
load_into_instance,
Expand Down Expand Up @@ -38,6 +39,7 @@
"check_and_convert_data",
"convert_config",
"ensure_types",
"load_data",
"load_into",
"load_into_class",
"load_into_instance",
Expand Down
14 changes: 7 additions & 7 deletions src/configuraptor/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def from_url(url: str, _dummy: bool = False) -> tuple[io.BytesIO, str]:
return io.BytesIO(data.encode()), filetype


def __load_data(
def _load_data(
data: T_data,
key: str = None,
classname: str = None,
Expand All @@ -136,7 +136,7 @@ def __load_data(

final_data: dict[str, typing.Any] = {}
for source in data:
final_data |= _load_data(source, key=key, classname=classname, lower_keys=True, allow_types=allow_types)
final_data |= load_data(source, key=key, classname=classname, lower_keys=True, allow_types=allow_types)

return final_data

Expand Down Expand Up @@ -180,7 +180,7 @@ def __load_data(
return data


def _load_data(
def load_data(
data: T_data,
key: str = None,
classname: str = None,
Expand All @@ -195,10 +195,10 @@ def _load_data(
data = find_pyproject_toml()

try:
return __load_data(data, key, classname, lower_keys=lower_keys, allow_types=allow_types)
return _load_data(data, key, classname, lower_keys=lower_keys, allow_types=allow_types)
except Exception as e:
if key != "":
return __load_data(data, "", classname, lower_keys=lower_keys, allow_types=allow_types)
return _load_data(data, "", classname, lower_keys=lower_keys, allow_types=allow_types)
else: # pragma: no cover
warnings.warn(f"Data could not be loaded: {e}", source=e)
# key already was "", just return data!
Expand Down Expand Up @@ -499,7 +499,7 @@ def load_into_class(
Shortcut for _load_data + load_into_recurse.
"""
allow_types = (dict, bytes) if issubclass(cls, BinaryConfig) else (dict,)
to_load = _load_data(data, key, cls.__name__, lower_keys=lower_keys, allow_types=allow_types)
to_load = load_data(data, key, cls.__name__, lower_keys=lower_keys, allow_types=allow_types)
return _load_into_recurse(cls, to_load, init=init, strict=strict, convert_types=convert_types)


Expand All @@ -518,7 +518,7 @@ def load_into_instance(
"""
cls = inst.__class__
allow_types = (dict, bytes) if issubclass(cls, BinaryConfig) else (dict,)
to_load = _load_data(data, key, cls.__name__, lower_keys=lower_keys, allow_types=allow_types)
to_load = load_data(data, key, cls.__name__, lower_keys=lower_keys, allow_types=allow_types)
return _load_into_instance(inst, cls, to_load, init=init, strict=strict, convert_types=convert_types)


Expand Down
16 changes: 8 additions & 8 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ def test_no_data():

# data must be a dict:
with pytest.raises(ValueError):
configuraptor.core._load_data(42, key=None)
configuraptor.core.load_data(42, key=None)
with pytest.raises(ValueError):
configuraptor.core._load_data([], key=None)
configuraptor.core.load_data([], key=None)

# but other than that, it should be fine:
configuraptor.core._load_data({}, key="")
configuraptor.core._load_data({}, key=None)
configuraptor.core._load_data({"-": 0, "+": None}, key="joe", classname="-.+")
configuraptor.core._load_data({"-": 0, "+": None}, key="+", classname="-.+")
configuraptor.core._load_data({"-": 0, "+": None}, key="", classname="-.+")
configuraptor.core._load_data({"-": 0, "+": None}, key=None, classname="-.+")
configuraptor.core.load_data({}, key="")
configuraptor.core.load_data({}, key=None)
configuraptor.core.load_data({"-": 0, "+": None}, key="joe", classname="-.+")
configuraptor.core.load_data({"-": 0, "+": None}, key="+", classname="-.+")
configuraptor.core.load_data({"-": 0, "+": None}, key="", classname="-.+")
configuraptor.core.load_data({"-": 0, "+": None}, key=None, classname="-.+")


def test_str_to_none():
Expand Down

0 comments on commit 7f3a534

Please sign in to comment.