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

get and unset paths by multiple arguments #20

Closed
Closed
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
10 changes: 5 additions & 5 deletions tests/toml_cli/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ def test_get_value(tmp_path: pathlib.Path):
assert result.exit_code == 0
assert "{'name': 'MyName', 'age': 12, 'education': {'name': 'University'}}" in result.stdout

result = runner.invoke(app, ["get", "--toml-path", str(test_toml_path), "person.education"])
result = runner.invoke(app, ["get", "--toml-path", str(test_toml_path), "person", "education"])
assert result.exit_code == 0
assert "{'name': 'University'}" in result.stdout

result = runner.invoke(app, ["get", "--toml-path", str(test_toml_path), "person.education.name"])
result = runner.invoke(app, ["get", "--toml-path", str(test_toml_path), "person", "education", "name"])
assert result.exit_code == 0
assert "University" in result.stdout

result = runner.invoke(app, ["get", "--toml-path", str(test_toml_path), "person.age"])
result = runner.invoke(app, ["get", "--toml-path", str(test_toml_path), "person", "age"])
assert result.exit_code == 0
assert "12" in result.stdout

Expand Down Expand Up @@ -109,12 +109,12 @@ def test_unset(tmp_path: pathlib.Path):
"""
)

result = runner.invoke(app, ["unset", "--toml-path", str(test_toml_path), "person.education.name"])
result = runner.invoke(app, ["unset", "--toml-path", str(test_toml_path), "person", "education", "name"])
assert result.exit_code == 0
assert "[person.education]" in test_toml_path.read_text()
assert "University" not in test_toml_path.read_text()

result = runner.invoke(app, ["unset", "--toml-path", str(test_toml_path), "person.education"])
result = runner.invoke(app, ["unset", "--toml-path", str(test_toml_path), "person", "education"])
assert result.exit_code == 0
assert "[persion.education]" not in test_toml_path.read_text()

Expand Down
26 changes: 17 additions & 9 deletions toml_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import pathlib
from typing import Optional
from typing import Optional, List

import tomlkit
import tomlkit.exceptions
import typer

app = typer.Typer()
app = typer.Typer(no_args_is_help=True)


@app.command("get")
def get(key: Optional[str] = typer.Argument(None), toml_path: pathlib.Path = typer.Option(pathlib.Path("config.toml"))):
def get(
key: Optional[List[str]] = typer.Argument(None),
toml_path: pathlib.Path = typer.Option(pathlib.Path("config.toml"))
):
"""Get a value from a toml file"""
toml_part = tomlkit.parse(toml_path.read_text())

if key is not None:
for key_part in key.split("."):
toml_part = toml_part[key_part]
for key_part in key:
toml_part = toml_part[key_part]

typer.echo(toml_part)

Expand Down Expand Up @@ -66,20 +68,26 @@ def add_section(


@app.command("unset")
def unset(key: str, toml_path: pathlib.Path = typer.Option(pathlib.Path("config.toml"))):
def unset(
key: Optional[List[str]] = typer.Argument(None),
toml_path: pathlib.Path = typer.Option(pathlib.Path("config.toml"))
):
"""Unset a value from a toml file"""
toml_part = toml_file = tomlkit.parse(toml_path.read_text())

for key_part in key.split(".")[:-1]:
for key_part in key[:-1]:
try:
toml_part = toml_part[key_part]
except tomlkit.exceptions.NonExistentKey:
typer.echo(f"Key {key} can not unset", err=True)

del toml_part[key.split(".")[-1]]
del toml_part[key[-1]]

toml_path.write_text(tomlkit.dumps(toml_file))


def main():
app()

if __name__ == "__main__":
app()