From b765ee50d31b1b629b7572c231d2cf2840a4146f Mon Sep 17 00:00:00 2001 From: "Jose M. Guisado" Date: Tue, 17 May 2022 11:24:49 +0200 Subject: utils: add print_json Adds a pretty printing function for JSON data. Replaces print for print_json in corresponding cli objects. Follows commit 828d6c0ce7d4a6b4bcd95e97155460fd59856bdd ("list scopes: pretty print scope tree") --- cli/objects/client.py | 8 +++++--- cli/objects/disks.py | 4 +++- cli/objects/images.py | 2 +- cli/objects/scopes.py | 7 ++++--- cli/utils.py | 6 ++++++ 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/cli/objects/client.py b/cli/objects/client.py index bd84dd8..c250470 100644 --- a/cli/objects/client.py +++ b/cli/objects/client.py @@ -7,12 +7,14 @@ import argparse +from cli.utils import print_json + class OgClient(): @staticmethod def list_clients(rest): r = rest.get('/clients') - print(r.text) + print_json(r.text) @staticmethod def list_client_hardware(rest, args): @@ -26,7 +28,7 @@ class OgClient(): payload = {'client': parsed_args.client_ip} r = rest.get('/hardware', payload=payload) - print(r.text) + print_json(r.text) @staticmethod def get_client_properties(rest, args): @@ -39,4 +41,4 @@ class OgClient(): payload = {'client': parsed_args.client_ip} r = rest.get('/client/info', payload=payload) - print(r.text) + print_json(r.text) diff --git a/cli/objects/disks.py b/cli/objects/disks.py index 064a43e..77955fa 100644 --- a/cli/objects/disks.py +++ b/cli/objects/disks.py @@ -8,6 +8,8 @@ import argparse import re +from cli.utils import print_json + class OgDisk(): @staticmethod @@ -21,7 +23,7 @@ class OgDisk(): payload = {'client': [parsed_args.client_ip]} r = rest.get('/client/setup', payload=payload) - print(r.text) + print_json(r.text) @staticmethod def setup_disk(rest, args): diff --git a/cli/objects/images.py b/cli/objects/images.py index c696d53..8299923 100644 --- a/cli/objects/images.py +++ b/cli/objects/images.py @@ -15,7 +15,7 @@ class OgImage(): @staticmethod def list_images(rest): r = rest.get('/images') - print(r.text) + print_json(r.text) @staticmethod def restore_image(rest, args): diff --git a/cli/objects/scopes.py b/cli/objects/scopes.py index 3517e43..5383932 100644 --- a/cli/objects/scopes.py +++ b/cli/objects/scopes.py @@ -5,12 +5,13 @@ # Free Software Foundation; either version 3 of the License, or # (at your option) any later version. -import json +import argparse + +from cli.utils import print_json class OgScope(): @staticmethod def list_scopes(rest): r = rest.get('/scopes') - payload = json.loads(r.text) - print(json.dumps(payload, sort_keys=True, indent=2)) + print_json(r.text) diff --git a/cli/utils.py b/cli/utils.py index 9c472c3..208be68 100644 --- a/cli/utils.py +++ b/cli/utils.py @@ -5,6 +5,8 @@ # Free Software Foundation; either version 3 of the License, or # (at your option) any later version. +import json + def scope_lookup(scope_id, scope_type, d): if scope_id == d.get('id') and scope_type == d.get('type'): return d @@ -23,3 +25,7 @@ def ips_in_scope(scope): for child in scope['scope']: ips += ips_in_scope(child) return ips + +def print_json(text): + payload = json.loads(text) + print(json.dumps(payload, sort_keys=True, indent=2)) -- cgit v1.2.3-18-g5258