Revision 1b93846c kamaki/utils.py

b/kamaki/utils.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
def print_addresses(addresses, margin):
35
    for address in addresses:
36
        if address['id'] == 'public':
37
            net = 'public'
38
        else:
39
            net = '%s/%s' % (address['id'], address['network_id'])
40
        print '%s:' % net.rjust(margin + 4)
41

  
42
        ether = address.get('mac', None)
43
        if ether:
44
            print '%s: %s' % ('ether'.rjust(margin + 8), ether)
45

  
46
        firewall = address.get('firewallProfile', None)
47
        if firewall:
48
            print '%s: %s' % ('firewall'.rjust(margin + 8), firewall)
49

  
50
        for ip in address.get('values', []):
51
            key = 'inet' if ip['version'] == 4 else 'inet6'
52
            print '%s: %s' % (key.rjust(margin + 8), ip['addr'])
53

  
54
def print_dict(d, exclude=()):
55
    if not d:
56
        return
57
    margin = max(len(key) for key in d) + 1
58

  
34
def print_dict(d, exclude=(), ident= 0):
35
    margin = max(max(len(key) for key in d) + 1, ident)
59 36
    for key, val in sorted(d.items()):
60 37
        if key in exclude:
61 38
            continue
39
        print_str = '%s:' % unicode(key)
40
        if isinstance(val, dict):
41
            print(print_str.rjust(margin)+' {')
42
            print_dict(val, exclude = exclude, ident = margin + 8)
43
            print '}'.rjust(margin)
44
        elif isinstance(val, list):
45
            print(print_str.rjust(margin)+' [')
46
            print_list(val, exclude = exclude, ident = margin + 8)
47
            print ']'.rjust(margin)
48
        else:
49
            print print_str.rjust(margin+4)+' '+unicode(val)
62 50

  
63
        #if key == 'addresses':
64
        #    print '%s:' % 'addresses'.rjust(margin)
65
        #    print_addresses(val.get('values', []), margin)
66
        #    continue
67
        if key == 'attachments':
68
            print '%s:' % 'attachments'.rjust(margin)
69
            print_addresses(val.get('values', []), margin)
70
            continue
71
        elif key == 'servers':
72
            val = ', '.join(unicode(x) for x in val['values'])
73
        elif isinstance(val, dict):
74
            if val.keys() == ['values']:
75
                val = val['values']
76
            print '%s:' % key.rjust(margin)
77
            for key, val in val.items():
78
                print '%s: %s' % (key.rjust(margin + 4), val)
51
def print_list(l, exclude=(), ident = 0):
52
    margin = max(max(len(item) for item in l) + 1, ident)
53
    for item in sorted(l):
54
        if item in exclude:
79 55
            continue
80

  
81
        print '%s: %s' % (key.rjust(margin), val)
82

  
56
        if isinstance(item, dict):
57
            print('{'.rjust(margin))
58
            print_dict(item, exclude = exclude, ident = margin + 8)
59
            print '}'.rjust(margin)
60
        elif isinstance(item, list):
61
            print '['.rjust(margin)
62
            print_list(item, exclude = exclude, ident = margin + 8)
63
            print ']'.rjust(margin)
64
        else:
65
            print unicode(val).rjust(margin + 4)
83 66

  
84 67
def print_items(items, title=('id', 'name')):
85 68
    for item in items:

Also available in: Unified diff