diff --git a/.gitignore b/.gitignore index 2bf16d0..99f75a5 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ dist/ wheels/ *.egg-info .venv +.coverage diff --git a/scripts/generate_schema.py b/scripts/generate_schema.py index a96b159..5f686ee 100644 --- a/scripts/generate_schema.py +++ b/scripts/generate_schema.py @@ -52,7 +52,8 @@ def generate_node_from_click(ctx, additional_properties=False): if param.multiple: props[name] = {"type": "array", "items": props[name]} - props[name]["description"] = param.help + if param.help: + props[name]["description"] = param.help default = param.to_info_dict()["default"] if default is not None and not param.multiple: diff --git a/src/dbyaml/cli.py b/src/dbyaml/cli.py index 02d3a31..d86f18c 100644 --- a/src/dbyaml/cli.py +++ b/src/dbyaml/cli.py @@ -33,6 +33,8 @@ else: import yaml + from dbyaml.db import dump, load + class FormatType(str, enum.Enum): @staticmethod def _generate_next_value_(name, start, count, last_values): @@ -127,6 +129,7 @@ else: "format_type", help="Format type.", type=click.Choice(FormatType, case_sensitive=False), + default=FormatType.yaml, envvar="DBYAML_FORMAT", # instead of auto DBYAML_FORMAT_TYPE show_envvar=True, ) @@ -159,12 +162,12 @@ else: @pass_context def main(ctx, filename, config, format_type, quiet, verbose, debug): """The uncompromising db(yaml) utility.""" - ctx.obj["DEBUG"] = debug if ctx.invoked_subcommand is None: - click.echo("I was invoked without subcommand") - else: - click.echo(f"I am about to invoke {ctx.invoked_subcommand}") + ctx.invoke(list) @main.command @pass_context - def list(ctx): ... + def list(ctx): + data = load(ctx.obj["filename"]) + res = dump(data, format_type=ctx.obj["format_type"]) + print(res) diff --git a/src/dbyaml/db.py b/src/dbyaml/db.py new file mode 100644 index 0000000..2734be1 --- /dev/null +++ b/src/dbyaml/db.py @@ -0,0 +1,31 @@ +import json +import os +import subprocess + +import yaml + + +def load(filename): + with open(filename) as f: + data_str = f.read() + data = yaml.safe_load(data_str) + return data + + +def dump(data, format_type="raw"): + if format_type == "yaml": + res = yaml.dump(data) + elif format_type == "json": + res = json.dumps(data) + else: + res = str(data) + return res + + +def run_shebang(script): + """ + Run script string as file while honoring the shebang line dynamically. + """ + fd = os.memfd_create("inline_script") + os.write(fd, script.encode()) + subprocess.run([f"/dev/fd/{fd}"], check=False) diff --git a/src/dbyaml/db.pyi b/src/dbyaml/db.pyi new file mode 100644 index 0000000..246d53f --- /dev/null +++ b/src/dbyaml/db.pyi @@ -0,0 +1,3 @@ +def load(filename: str) -> list | dict: ... +def dump(data: list | dict, format_type: str) -> str: ... +def run_shebang(script: str) -> None: ... diff --git a/src/dbyaml/resources/dbyaml.schema.json b/src/dbyaml/resources/dbyaml.schema.json index 98b1e87..f45c2b0 100644 --- a/src/dbyaml/resources/dbyaml.schema.json +++ b/src/dbyaml/resources/dbyaml.schema.json @@ -11,7 +11,8 @@ "yaml", "json" ], - "description": "Format type." + "description": "Format type.", + "default": "yaml" }, "quiet": { "type": "boolean", @@ -25,7 +26,6 @@ }, "debug": { "type": "boolean", - "description": null, "default": false }, "list": { diff --git a/src/dbyaml/schema.py b/src/dbyaml/schema.py index f1d4127..2840e4e 100644 --- a/src/dbyaml/schema.py +++ b/src/dbyaml/schema.py @@ -2,9 +2,8 @@ import importlib.resources import json -def get_schema(tool_name="dbyaml"): +def get_schema(): """Get the stored complete schema for dbyaml's db files.""" - assert tool_name == "dbyaml", "Only yamldb is supported." pkg = "dbyaml.resources" fname = "dbyaml.schema.json" diff --git a/tests/test_schema.py b/tests/test_schema.py index a585f60..ec591d6 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -8,8 +8,7 @@ def test_schema_entrypoint() -> None: dbyaml_fn = dbyaml_ep.load() schema = dbyaml_fn() - assert schema == dbyaml_fn("dbyaml") - print(schema["properties"]) + assert schema == dbyaml_fn() assert schema["type"] == "object" assert schema["properties"]["verbose"]["type"] == "boolean" assert schema["properties"]["list"]["type"] == "object"