source: ogCli-Git/cli/utils.py @ f88bcf7

Last change on this file since f88bcf7 was b765ee5, checked in by Jose M. Guisado <jguisado@…>, 2 years ago

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")

  • Property mode set to 100644
File size: 864 bytes
Line 
1# Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu>
2#
3# This program is free software: you can redistribute it and/or modify it under
4# the terms of the GNU Affero General Public License as published by the
5# Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7
8import json
9
10def scope_lookup(scope_id, scope_type, d):
11        if scope_id == d.get('id') and scope_type == d.get('type'):
12                return d
13        for scope in d['scope']:
14                lookup = scope_lookup(scope_id, scope_type, scope)
15                if lookup is not None:
16                        return lookup
17        return None
18
19def ips_in_scope(scope):
20        if scope is None:
21                return []
22        if 'ip' in scope:
23                return [scope['ip']]
24        ips = []
25        for child in scope['scope']:
26                ips += ips_in_scope(child)
27        return ips
28
29def print_json(text):
30        payload = json.loads(text)
31        print(json.dumps(payload, sort_keys=True, indent=2))
Note: See TracBrowser for help on using the repository browser.