-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use stdlib toml provider on Python >= 3.11. Switch to tomli otherwise (…
…#20)
- Loading branch information
Showing
4 changed files
with
86 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import io | ||
|
||
try: | ||
import tomllib | ||
except ImportError: | ||
import tomli as tomllib | ||
|
||
|
||
def load(f, *, parse_float=float): | ||
# wrapper around tomllib.load to be more forgiving of streams opened in text mode | ||
if isinstance(f, io.TextIOWrapper): | ||
return tomllib.load(f.buffer, parse_float=parse_float) | ||
elif isinstance(f, io.StringIO): | ||
return tomllib.loads(f.getvalue(), parse_float=parse_float) | ||
else: | ||
return tomllib.load(f, parse_float=parse_float) |
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,65 @@ | ||
import pytest | ||
from testfixtures import compare | ||
|
||
from configurator import Config | ||
|
||
|
||
def test_load_toml_from_path_implicit_parser(tmp_path): | ||
path = tmp_path / 'test.toml' | ||
path.write_bytes(b'k = "v"') | ||
config = Config.from_path(path) | ||
compare(config.k, "v") | ||
|
||
|
||
def test_load_toml_from_path_explicit_parser(tmp_path): | ||
path = tmp_path / 'test.toml' | ||
path.write_bytes(b'k = "v"') | ||
parser = Config.parsers['toml'] | ||
config = Config.from_path(path, parser) | ||
compare(config.k, "v") | ||
|
||
|
||
def test_load_toml_from_byte_stream_implicit_parser(tmp_path): | ||
path = tmp_path / 'test.toml' | ||
path.write_bytes(b'k = "v"') | ||
with path.open(mode="rb") as stream: | ||
config = Config.from_stream(stream) | ||
compare(config.k, "v") | ||
|
||
|
||
def test_load_toml_from_byte_stream_explicit_parser(tmp_path): | ||
path = tmp_path / 'test.toml' | ||
path.write_bytes(b'k = "v"') | ||
parser = Config.parsers['toml'] | ||
with path.open(mode="rb") as stream: | ||
config = Config.from_stream(stream, parser) | ||
compare(config.k, "v") | ||
|
||
|
||
def test_load_toml_from_text_stream_implicit_parser(tmp_path): | ||
path = tmp_path / 'test.toml' | ||
path.write_bytes(b'k = "v"') | ||
with path.open(mode="rt") as stream: | ||
config = Config.from_stream(stream) | ||
compare(config.k, "v") | ||
|
||
|
||
def test_load_toml_from_text_stream_explicit_parser(tmp_path): | ||
path = tmp_path / 'test.toml' | ||
path.write_bytes(b'k = "v"') | ||
parser = Config.parsers['toml'] | ||
with path.open(mode="rt") as stream: | ||
config = Config.from_stream(stream, parser) | ||
compare(config.k, "v") | ||
|
||
|
||
def test_load_toml_from_text(): | ||
parser = Config.parsers['toml'] | ||
config = Config.from_text('k = "v"', parser) | ||
compare(config.k, "v") | ||
|
||
|
||
def test_load_toml_from_bytes(): | ||
parser = Config.parsers['toml'] | ||
config = Config.from_text(b'k = "v"', parser) | ||
compare(config.k, "v") |