add list command

This commit is contained in:
2026-09-25 19:19:34 +02:00
parent d4c78cc29d
commit de223ca303
8 changed files with 49 additions and 12 deletions
+1
View File
@@ -6,3 +6,4 @@ dist/
wheels/ wheels/
*.egg-info *.egg-info
.venv .venv
.coverage
+1
View File
@@ -52,6 +52,7 @@ def generate_node_from_click(ctx, additional_properties=False):
if param.multiple: if param.multiple:
props[name] = {"type": "array", "items": props[name]} props[name] = {"type": "array", "items": props[name]}
if param.help:
props[name]["description"] = param.help props[name]["description"] = param.help
default = param.to_info_dict()["default"] default = param.to_info_dict()["default"]
+8 -5
View File
@@ -33,6 +33,8 @@ else:
import yaml import yaml
from dbyaml.db import dump, load
class FormatType(str, enum.Enum): class FormatType(str, enum.Enum):
@staticmethod @staticmethod
def _generate_next_value_(name, start, count, last_values): def _generate_next_value_(name, start, count, last_values):
@@ -127,6 +129,7 @@ else:
"format_type", "format_type",
help="Format type.", help="Format type.",
type=click.Choice(FormatType, case_sensitive=False), type=click.Choice(FormatType, case_sensitive=False),
default=FormatType.yaml,
envvar="DBYAML_FORMAT", # instead of auto DBYAML_FORMAT_TYPE envvar="DBYAML_FORMAT", # instead of auto DBYAML_FORMAT_TYPE
show_envvar=True, show_envvar=True,
) )
@@ -159,12 +162,12 @@ else:
@pass_context @pass_context
def main(ctx, filename, config, format_type, quiet, verbose, debug): def main(ctx, filename, config, format_type, quiet, verbose, debug):
"""The uncompromising db(yaml) utility.""" """The uncompromising db(yaml) utility."""
ctx.obj["DEBUG"] = debug
if ctx.invoked_subcommand is None: if ctx.invoked_subcommand is None:
click.echo("I was invoked without subcommand") ctx.invoke(list)
else:
click.echo(f"I am about to invoke {ctx.invoked_subcommand}")
@main.command @main.command
@pass_context @pass_context
def list(ctx): ... def list(ctx):
data = load(ctx.obj["filename"])
res = dump(data, format_type=ctx.obj["format_type"])
print(res)
+31
View File
@@ -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)
+3
View File
@@ -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: ...
+2 -2
View File
@@ -11,7 +11,8 @@
"yaml", "yaml",
"json" "json"
], ],
"description": "Format type." "description": "Format type.",
"default": "yaml"
}, },
"quiet": { "quiet": {
"type": "boolean", "type": "boolean",
@@ -25,7 +26,6 @@
}, },
"debug": { "debug": {
"type": "boolean", "type": "boolean",
"description": null,
"default": false "default": false
}, },
"list": { "list": {
+1 -2
View File
@@ -2,9 +2,8 @@ import importlib.resources
import json import json
def get_schema(tool_name="dbyaml"): def get_schema():
"""Get the stored complete schema for dbyaml's db files.""" """Get the stored complete schema for dbyaml's db files."""
assert tool_name == "dbyaml", "Only yamldb is supported."
pkg = "dbyaml.resources" pkg = "dbyaml.resources"
fname = "dbyaml.schema.json" fname = "dbyaml.schema.json"
+1 -2
View File
@@ -8,8 +8,7 @@ def test_schema_entrypoint() -> None:
dbyaml_fn = dbyaml_ep.load() dbyaml_fn = dbyaml_ep.load()
schema = dbyaml_fn() schema = dbyaml_fn()
assert schema == dbyaml_fn("dbyaml") assert schema == dbyaml_fn()
print(schema["properties"])
assert schema["type"] == "object" assert schema["type"] == "object"
assert schema["properties"]["verbose"]["type"] == "boolean" assert schema["properties"]["verbose"]["type"] == "boolean"
assert schema["properties"]["list"]["type"] == "object" assert schema["properties"]["list"]["type"] == "object"