Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🩹 Refactor stores to support different serialization modes (#57) #58

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion argdantic/stores/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Callable, Optional, Set, Union
from typing import Callable, Literal, Optional, Set, Union

from pydantic import BaseModel
from pydantic_settings import BaseSettings
Expand All @@ -18,6 +18,7 @@ def __init__(
self,
path: Union[str, Path],
*,
mode: Literal["python", "json"] = "python",
encoding: str = "utf-8",
include: Optional[Set[str]] = None,
exclude: Optional[Set[str]] = None,
Expand All @@ -27,6 +28,7 @@ def __init__(
exclude_none: bool = False,
) -> None:
self.path = Path(path)
self.mode = mode
self.encoding = encoding
self.include = include
self.exclude = exclude
Expand Down
27 changes: 27 additions & 0 deletions argdantic/stores/json.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from pathlib import Path
from typing import Optional, Set, Union

from pydantic_settings import BaseSettings

from argdantic.stores.base import BaseSettingsStore
Expand All @@ -9,6 +12,30 @@ class JsonSettingsStore(BaseSettingsStore):
Orjson is used if available, otherwise the standard json module is used.
"""

def __init__(
self,
path: Union[str, Path],
*,
encoding: str = "utf-8",
include: Optional[Set[str]] = None,
exclude: Optional[Set[str]] = None,
by_alias: bool = False,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
) -> None:
super().__init__(
path,
mode="json",
encoding=encoding,
include=include,
exclude=exclude,
by_alias=by_alias,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
)

def __call__(self, settings: BaseSettings) -> None:
with self.path.open("wb") as f:
text = settings.model_dump_json(
Expand Down
1 change: 1 addition & 0 deletions argdantic/stores/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __call__(self, settings: BaseSettings) -> None:
with self.path.open("wb") as f:
text = toml.dumps(
settings.model_dump(
mode=self.mode,
include=self.include,
exclude=self.exclude,
by_alias=self.by_alias,
Expand Down
1 change: 1 addition & 0 deletions argdantic/stores/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __call__(self, settings: BaseSettings) -> None:

with self.path.open("w") as f:
data = settings.model_dump(
mode=self.mode,
include=self.include,
exclude=self.exclude,
by_alias=self.by_alias,
Expand Down
17 changes: 17 additions & 0 deletions tests/test_stores/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,20 @@ def main(foo: str = "baz", bar: int = 42) -> None:
result = runner.invoke(cli, [])
assert result.exception is None
assert result.return_value == ("baz", 42)


def test_parser_using_json_store_complex_data(tmp_path: Path, runner: CLIRunner) -> None:
from pathlib import Path

from argdantic import ArgParser

cli = ArgParser()
path = tmp_path / "settings.json"

@cli.command(stores=[JsonSettingsStore(path)])
def main(foo: Path = "baz", bar: int = 42) -> None:
return foo, bar

result = runner.invoke(cli, [])
assert result.exception is None
assert result.return_value == ("baz", 42)
16 changes: 16 additions & 0 deletions tests/test_stores/test_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,19 @@ def main(foo: str = "baz", bar: int = 42) -> None:
result = runner.invoke(parser, [])
assert result.exception is None
assert result.return_value == ("baz", 42)


def test_parser_using_toml_store_complex_data(tmp_path: Path, runner: CLIRunner) -> None:
from argdantic import ArgParser
from argdantic.stores.toml import TomlSettingsStore

path = tmp_path / "settings.toml"
parser = ArgParser()

@parser.command(stores=[TomlSettingsStore(path, mode="json")])
def main(foo: Path = "baz", bar: int = 42) -> None:
return foo, bar

result = runner.invoke(parser, [])
assert result.exception is None
assert result.return_value == ("baz", 42)
21 changes: 21 additions & 0 deletions tests/test_stores/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,24 @@ def main(foo: str = "baz", bar: int = 42) -> None:
assert result.exception is None
assert result.return_value == ("qux", 42)
assert result.return_value == ("qux", 42)


def test_parser_using_yaml_store_complex_data(tmp_path: Path, runner: CLIRunner) -> None:
from argdantic import ArgParser
from argdantic.stores.yaml import YamlSettingsStore

path = tmp_path / "settings.yaml"
parser = ArgParser()

@parser.command(stores=[YamlSettingsStore(path, mode="json")])
def main(foo: Path = "baz", bar: int = 42) -> None:
return str(foo), bar

result = runner.invoke(parser, [])
assert result.exception is None
assert result.return_value == ("baz", 42)

result = runner.invoke(parser, ["--foo", "qux", "--bar", "24"])
assert result.exception is None
print(result.return_value)
assert result.return_value == ("qux", 24)
Loading