-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added JSON and YAML file loading.
- Loading branch information
1 parent
c11a338
commit e4f920f
Showing
16 changed files
with
155 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"config": { | ||
"name": "Hello World!", | ||
"reference": { | ||
"number": 42, | ||
"numbers": [ | ||
41, | ||
43 | ||
], | ||
"string": "42" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
config: | ||
name: "Hello World!" | ||
reference: | ||
number: 42 | ||
numbers: | ||
- 41 | ||
- 43 | ||
string: "42" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"config": { | ||
"name": "Hello World!", | ||
"reference": { | ||
"number": 42, | ||
"numbers": [ | ||
41, | ||
43 | ||
], | ||
"string": "42" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[config] | ||
name = "Hello World!" | ||
|
||
[config.reference] | ||
number = 42 | ||
numbers = [41, 43] | ||
string = "42" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
config: | ||
name: "Hello World!" | ||
reference: | ||
number: 42 | ||
numbers: | ||
- 41 | ||
- 43 | ||
string: "42" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import typing | ||
|
||
T_config = dict[str, typing.Any] | ||
|
||
|
||
def as_tconfig(data: typing.Any) -> T_config: | ||
""" | ||
Does not actually do anything, but tells mypy the 'data' of type Any (json, pyyaml, tomlkit) \ | ||
is actually a dict of string keys and Any values. | ||
""" | ||
return typing.cast(T_config, data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
File loaders that work regardless of Python version. | ||
""" | ||
|
||
import json as json_lib | ||
from typing import BinaryIO | ||
|
||
import yaml as yaml_lib | ||
|
||
from ._types import T_config, as_tconfig | ||
|
||
|
||
def json(f: BinaryIO) -> T_config: | ||
""" | ||
Load a JSON file. | ||
""" | ||
data = json_lib.load(f) | ||
return as_tconfig(data) | ||
|
||
|
||
def yaml(f: BinaryIO) -> T_config: | ||
""" | ||
Load a YAML file. | ||
""" | ||
data = yaml_lib.load(f, yaml_lib.SafeLoader) | ||
return as_tconfig(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from src.configuraptor import load_into | ||
from tests.constants import PYTEST_EXAMPLES | ||
|
||
|
||
class SomeRegularClass: | ||
number: int | ||
numbers: list[int] | ||
string: str | ||
|
||
|
||
class Config: | ||
name: str | ||
reference: SomeRegularClass | ||
|
||
|
||
def test_basic_json_and_yaml(): | ||
toml = load_into(Config, PYTEST_EXAMPLES / "example.toml") | ||
json = load_into(Config, PYTEST_EXAMPLES / "example.json") | ||
yaml = load_into(Config, PYTEST_EXAMPLES / "example.yaml") | ||
|
||
assert toml.reference.numbers and toml.reference.numbers == json.reference.numbers == yaml.reference.numbers |