Skip to content

Commit

Permalink
add bw metadata —table
Browse files Browse the repository at this point in the history
  • Loading branch information
trehn committed Jun 29, 2017
1 parent 2ad308d commit e5f86cb
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 12 deletions.
45 changes: 34 additions & 11 deletions bundlewrap/cmdline/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,40 @@
from json import dumps

from ..metadata import MetadataJSONEncoder, value_at_key_path
from ..utils.cmdline import get_node
from ..utils.text import force_text
from ..utils.ui import io
from ..utils.cmdline import get_node, get_target_nodes
from ..utils.table import ROW_SEPARATOR, render_table
from ..utils.text import bold, force_text, mark_for_translation as _, red
from ..utils.ui import io, page_lines


def bw_metadata(repo, args):
node = get_node(repo, args['node'], adhoc_nodes=args['adhoc_nodes'])
for line in dumps(
value_at_key_path(node.metadata, args['keys']),
cls=MetadataJSONEncoder,
indent=4,
sort_keys=True,
).splitlines():
io.stdout(force_text(line))
if args['table']:
if not args['keys']:
io.stdout(_("{x} at least one key is required with --table").format(x=red("!!!")))
exit(1)
target_nodes = get_target_nodes(repo, args['target'], adhoc_nodes=args['adhoc_nodes'])
key_paths = [path.strip().split(" ") for path in " ".join(args['keys']).split(",")]
table = [[bold(_("node"))] + [bold(" ".join(path)) for path in key_paths], ROW_SEPARATOR]
for node in target_nodes:
values = []
for key_path in key_paths:
try:
value = value_at_key_path(node.metadata, key_path)
except KeyError:
value = red(_("<missing>"))
if isinstance(value, (dict, list, tuple)):
value = ", ".join([str(item) for item in value])
elif isinstance(value, set):
value = ", ".join(sorted(value))
values.append(str(value))
table.append([bold(node.name)] + values)
page_lines(render_table(table))
else:
node = get_node(repo, args['target'], adhoc_nodes=args['adhoc_nodes'])
for line in dumps(
value_at_key_path(node.metadata, args['keys']),
cls=MetadataJSONEncoder,
indent=4,
sort_keys=True,
).splitlines():
io.stdout(force_text(line))
13 changes: 12 additions & 1 deletion bundlewrap/cmdline/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def build_parser_bw():
)
parser_metadata.set_defaults(func=bw_metadata)
parser_metadata.add_argument(
'node',
'target',
metavar=_("NODE"),
type=str,
help=_("node to print JSON-formatted metadata for"),
Expand All @@ -423,6 +423,17 @@ def build_parser_bw():
type=str,
help=_("print only partial metadata from the given space-separated key path"),
)
parser_metadata.add_argument(
"-t",
"--table",
action='store_true',
dest='table',
help=_(
"show a table of selected metadata values from multiple nodes instead; "
"allows for multiple comma-separated paths in KEY; "
"allows for node selectors in NODE (e.g. 'NODE1,NODE2,GROUP1,bundle:BUNDLE1...')"
),
)

# bw nodes
help_nodes = _("List all nodes in this repository")
Expand Down
51 changes: 51 additions & 0 deletions tests/integration/bw_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,54 @@ def test_metadatapy_loop(tmpdir):
""")
stdout, stderr, rcode = run("bw metadata node1", path=str(tmpdir))
assert rcode == 1


def test_table(tmpdir):
make_repo(
tmpdir,
nodes={
"node1": {
'metadata': {
"foo_dict": {
"bar": "baz",
},
"foo_list": ["bar", 1],
"foo_int": 47,
"foo_umlaut": "föö",
},
},
"node2": {
'metadata': {
"foo_dict": {
"baz": "bar",
},
"foo_list": [],
"foo_int": -3,
"foo_umlaut": "füü",
},
},
},
groups={
"all": {
'members': ["node1", "node2"],
},
},
)
stdout, stderr, rcode = run("BW_TABLE_STYLE=grep bw metadata --table all foo_dict bar, foo_list, foo_int, foo_umlaut", path=str(tmpdir))
assert stdout.decode('utf-8') == """node\tfoo_dict bar\tfoo_list\tfoo_int\tfoo_umlaut
node1\tbaz\tbar, 1\t47\tföö
node2\t<missing>\t\t-3\tfüü
"""
assert stderr == b""
assert rcode == 0


def test_table_no_key(tmpdir):
make_repo(
tmpdir,
nodes={
"node1": {},
},
)
stdout, stderr, rcode = run("bw metadata --table node1", path=str(tmpdir))
assert rcode == 1

0 comments on commit e5f86cb

Please sign in to comment.