Commit 0399ac7e authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Add json formated output for file list

- Implement a print_json auxiliary method in kamaki.cli.utils
- Add a (-j, --json) flag to file list
- When -j or --json flag is on, only json is printed

Refs: #3732
parent 36778d79
...@@ -40,7 +40,7 @@ from kamaki.cli.command_tree import CommandTree ...@@ -40,7 +40,7 @@ from kamaki.cli.command_tree import CommandTree
from kamaki.cli.errors import raiseCLIError, CLISyntaxError from kamaki.cli.errors import raiseCLIError, CLISyntaxError
from kamaki.cli.utils import ( from kamaki.cli.utils import (
format_size, to_bytes, print_dict, print_items, pretty_keys, format_size, to_bytes, print_dict, print_items, pretty_keys,
page_hold, bold, ask_user, get_path_size) page_hold, bold, ask_user, get_path_size, print_json)
from kamaki.cli.argument import FlagArgument, ValueArgument, IntArgument from kamaki.cli.argument import FlagArgument, ValueArgument, IntArgument
from kamaki.cli.argument import KeyValueArgument, DateArgument from kamaki.cli.argument import KeyValueArgument, DateArgument
from kamaki.cli.argument import ProgressBarArgument from kamaki.cli.argument import ProgressBarArgument
...@@ -329,10 +329,14 @@ class file_list(_file_container_command): ...@@ -329,10 +329,14 @@ class file_list(_file_container_command):
exact_match=FlagArgument( exact_match=FlagArgument(
'Show only objects that match exactly with path', 'Show only objects that match exactly with path',
'--exact-match'), '--exact-match'),
enum=FlagArgument('Enumerate results', '--enumerate') enum=FlagArgument('Enumerate results', '--enumerate'),
json_output=FlagArgument('show output in json', ('-j', '--json'))
) )
def print_objects(self, object_list): def print_objects(self, object_list):
if self['json_output']:
print_json(object_list)
return
limit = int(self['limit']) if self['limit'] > 0 else len(object_list) limit = int(self['limit']) if self['limit'] > 0 else len(object_list)
for index, obj in enumerate(object_list): for index, obj in enumerate(object_list):
if self['exact_match'] and self.path and not ( if self['exact_match'] and self.path and not (
...@@ -362,6 +366,9 @@ class file_list(_file_container_command): ...@@ -362,6 +366,9 @@ class file_list(_file_container_command):
page_hold(index, limit, len(object_list)) page_hold(index, limit, len(object_list))
def print_containers(self, container_list): def print_containers(self, container_list):
if self['json_output']:
print_json(container_list)
return
limit = int(self['limit']) if self['limit'] > 0\ limit = int(self['limit']) if self['limit'] > 0\
else len(container_list) else len(container_list)
for index, container in enumerate(container_list): for index, container in enumerate(container_list):
......
...@@ -35,6 +35,7 @@ from sys import stdout, stdin ...@@ -35,6 +35,7 @@ from sys import stdout, stdin
from re import compile as regex_compile from re import compile as regex_compile
from time import sleep from time import sleep
from os import walk, path from os import walk, path
from json import dumps
from kamaki.cli.errors import raiseCLIError from kamaki.cli.errors import raiseCLIError
...@@ -100,6 +101,14 @@ def pretty_keys(d, delim='_', recurcive=False): ...@@ -100,6 +101,14 @@ def pretty_keys(d, delim='_', recurcive=False):
return new_d return new_d
def print_json(data):
"""Print a list or dict as json in console
:param data: json-dumpable data
"""
print(dumps(data, indent=2))
def print_dict( def print_dict(
d, exclude=(), ident=0, d, exclude=(), ident=0,
with_enumeration=False, recursive_enumeration=False): with_enumeration=False, recursive_enumeration=False):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment