From 9ee1e459b4e5a32af59a8acbb5f6f09f20dfb73e Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Thu, 3 Oct 2024 12:52:26 -0700 Subject: [PATCH] add support for outputing json objects from list --- ui/fixtures/cmd.basic.json | 5 +++++ ui/medashare/cli.py | 11 ++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/ui/fixtures/cmd.basic.json b/ui/fixtures/cmd.basic.json index 9da2e40..f7dbd64 100644 --- a/ui/fixtures/cmd.basic.json +++ b/ui/fixtures/cmd.basic.json @@ -67,6 +67,11 @@ "cmd": [ "list", "newfile.txt" ], "stdout_re": "dc:creator:\tJohn-Mark Gurney\nhashes:\tsha512:90f8342520f0ac57fb5a779f5d331c2fa87aa40f8799940257f9ba619940951e67143a8d746535ed0284924b2b7bc1478f095198800ba96d01847d7b56ca465c\nsig:\t.*\ntag:\t\n" }, +{ + "title": "verify --json works", + "cmd": [ "list", "--json", "newfile.txt" ], + "stdout_check": [ { "dc:creator": [ "John-Mark Gurney" ], "hashes": [ "sha512:90f8342520f0ac57fb5a779f5d331c2fa87aa40f8799940257f9ba619940951e67143a8d746535ed0284924b2b7bc1478f095198800ba96d01847d7b56ca465c" ], "tag": [ "" ], "type": "metadata" } ] +}, { "title": "add duplicate ms:tag's", "cmd": [ "modify", "+ms:tag=foo", "+ms:tag=foo", "newfile.txt" ] diff --git a/ui/medashare/cli.py b/ui/medashare/cli.py index 0895c5c..0e85042 100644 --- a/ui/medashare/cli.py +++ b/ui/medashare/cli.py @@ -1426,8 +1426,11 @@ def cmd_list(options, persona, objstr, cache): continue for j in objstr.by_file(i): - for k, v in _iterdictlist(j): - print('%s:\t%s' % (k, v)) + if options.json: + print(j.encode('json')) + else: + for k, v in _iterdictlist(j): + print('%s:\t%s' % (k, v)) if exit: sys.exit(exit) @@ -1810,6 +1813,8 @@ def main(): parser_auto.set_defaults(func=cmd_auto) parser_list = subparsers.add_parser('list', help='list tags on file(s)') + parser_list.add_argument('--json', action='store_true', + help='output data as series of JSON objects') parser_list.add_argument('files', nargs='+', help='files to modify') parser_list.set_defaults(func=cmd_list) @@ -2478,7 +2483,7 @@ class _TestCases(unittest.TestCase): def objcompare(self, fullobjs, partialobjs): fullobjs = list(fullobjs) - #_debprint('objs:', repr(fullobjs)) + #_debprint('objs:', repr(fullobjs), repr(partialobjs)) self.assertEqual(len(fullobjs), len(partialobjs)) missing = []