-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdot-notation-parser.py
executable file
·71 lines (55 loc) · 2.05 KB
/
dot-notation-parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
# Author: github.com/danielhoherd
# License: MIT
"""Take a dotted notation string and convert it to json, yaml, or python.
Huge caveat: the dotted notation string must only be a dict, with no lists or other types."""
import json
from enum import Enum
from typing import Annotated
import typer
import yaml
app = typer.Typer(pretty_exceptions_enable=False)
class DestinationType(str, Enum):
yaml = "yaml"
python = "python"
json = "json"
def convert_to_python(data: str, default_value: str | bool | None = None):
"""Convert from a dotted notation string to json."""
data = data.removeprefix(".").removesuffix(".")
if "." in data:
key, _, value = data.partition(".")
return {key: convert_to_python(value, default_value)}
if "=" in data:
key, _, value = data.partition("=")
return {key: value}
return {data: default_value}
def convert_to_json(data: str, default_value: str | bool | None = None):
"""Convert from a dotted notation string to json."""
return json.dumps(convert_to_python(data, default_value))
def convert_to_yaml(data: str, default_value: str | bool | None = None):
"""Convert from a dotted notation string to yaml."""
return yaml.dump(convert_to_python(data, default_value), default_flow_style=False)
def main(
dotted_str: Annotated[str, typer.Argument()],
default_value: Annotated[str | None, typer.Argument()] = "None",
format: DestinationType = "json",
):
"""
Convert from dot notation to various formats.
"""
match format:
case "yaml":
typer.echo(convert_to_yaml(dotted_str, default_value))
case "json":
typer.echo(convert_to_json(dotted_str, default_value))
case "python":
typer.echo(convert_to_python(dotted_str, default_value))
case _:
typer.echo("Invalid format", err=True)
raise typer.Exit(1)
if __name__ == "__main__":
main.__doc__ = __doc__
try:
typer.run(main)
except OSError as e:
raise SystemExit(e) from e