Statistics
| Branch: | Tag: | Revision:

root / kamaki / utils.py @ c4ab2af9

History | View | Annotate | Download (3.9 kB)

1 eb3ca8ca Giorgos Verigakis
# Copyright 2011 GRNET S.A. All rights reserved.
2 eb3ca8ca Giorgos Verigakis
#
3 eb3ca8ca Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 eb3ca8ca Giorgos Verigakis
# without modification, are permitted provided that the following
5 eb3ca8ca Giorgos Verigakis
# conditions are met:
6 eb3ca8ca Giorgos Verigakis
#
7 eb3ca8ca Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 eb3ca8ca Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 eb3ca8ca Giorgos Verigakis
#      disclaimer.
10 eb3ca8ca Giorgos Verigakis
#
11 eb3ca8ca Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 eb3ca8ca Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 eb3ca8ca Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 eb3ca8ca Giorgos Verigakis
#      provided with the distribution.
15 eb3ca8ca Giorgos Verigakis
#
16 eb3ca8ca Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 eb3ca8ca Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 eb3ca8ca Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 eb3ca8ca Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 eb3ca8ca Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 eb3ca8ca Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 eb3ca8ca Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 eb3ca8ca Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 eb3ca8ca Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 eb3ca8ca Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 eb3ca8ca Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 eb3ca8ca Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 eb3ca8ca Giorgos Verigakis
#
29 eb3ca8ca Giorgos Verigakis
# The views and conclusions contained in the software and
30 eb3ca8ca Giorgos Verigakis
# documentation are those of the authors and should not be
31 eb3ca8ca Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 eb3ca8ca Giorgos Verigakis
# or implied, of GRNET S.A.
33 eb3ca8ca Giorgos Verigakis
34 eb3ca8ca Giorgos Verigakis
class OrderedDict(dict):
35 eb3ca8ca Giorgos Verigakis
    """An ordered dict implementation for Python versions prior to 2.7"""
36 eb3ca8ca Giorgos Verigakis
    
37 eb3ca8ca Giorgos Verigakis
    def __init__(self):
38 eb3ca8ca Giorgos Verigakis
        dict.__init__(self)
39 eb3ca8ca Giorgos Verigakis
        self._keys = []
40 eb3ca8ca Giorgos Verigakis
    
41 eb3ca8ca Giorgos Verigakis
    def __delitem__(self, key):
42 eb3ca8ca Giorgos Verigakis
        dict.__delitem__(self, key)
43 eb3ca8ca Giorgos Verigakis
        self._keys.remove(key)
44 eb3ca8ca Giorgos Verigakis
    
45 eb3ca8ca Giorgos Verigakis
    def __iter__(self):
46 eb3ca8ca Giorgos Verigakis
        return iter(self._keys)
47 eb3ca8ca Giorgos Verigakis
    
48 eb3ca8ca Giorgos Verigakis
    def __repr__(self):
49 eb3ca8ca Giorgos Verigakis
        return repr(self.items())
50 eb3ca8ca Giorgos Verigakis
    
51 eb3ca8ca Giorgos Verigakis
    def __setitem__(self, key, value):
52 eb3ca8ca Giorgos Verigakis
        if key not in self:
53 eb3ca8ca Giorgos Verigakis
            self._keys.append(key)
54 eb3ca8ca Giorgos Verigakis
        dict.__setitem__(self, key, value)
55 eb3ca8ca Giorgos Verigakis
    
56 eb3ca8ca Giorgos Verigakis
    def keys(self):
57 eb3ca8ca Giorgos Verigakis
        return self._keys
58 eb3ca8ca Giorgos Verigakis
    
59 eb3ca8ca Giorgos Verigakis
    def iteritems(self):
60 eb3ca8ca Giorgos Verigakis
        for key in self._keys:
61 eb3ca8ca Giorgos Verigakis
            yield key, self[key]
62 eb3ca8ca Giorgos Verigakis
    
63 eb3ca8ca Giorgos Verigakis
    def items(self):
64 eb3ca8ca Giorgos Verigakis
        return list(self.iteritems())
65 eb3ca8ca Giorgos Verigakis
66 eb3ca8ca Giorgos Verigakis
67 eb3ca8ca Giorgos Verigakis
def print_addresses(addresses, margin):
68 eb3ca8ca Giorgos Verigakis
    for address in addresses:
69 eb3ca8ca Giorgos Verigakis
        if address['id'] == 'public':
70 eb3ca8ca Giorgos Verigakis
            net = 'public'
71 eb3ca8ca Giorgos Verigakis
        else:
72 eb3ca8ca Giorgos Verigakis
            net = '%s/%s' % (address['id'], address['name'])
73 eb3ca8ca Giorgos Verigakis
        print '%s:' % net.rjust(margin + 4)
74 eb3ca8ca Giorgos Verigakis
75 eb3ca8ca Giorgos Verigakis
        ether = address.get('mac', None)
76 eb3ca8ca Giorgos Verigakis
        if ether:
77 eb3ca8ca Giorgos Verigakis
            print '%s: %s' % ('ether'.rjust(margin + 8), ether)
78 eb3ca8ca Giorgos Verigakis
79 eb3ca8ca Giorgos Verigakis
        firewall = address.get('firewallProfile', None)
80 eb3ca8ca Giorgos Verigakis
        if firewall:
81 eb3ca8ca Giorgos Verigakis
            print '%s: %s' % ('firewall'.rjust(margin + 8), firewall)
82 eb3ca8ca Giorgos Verigakis
83 eb3ca8ca Giorgos Verigakis
        for ip in address.get('values', []):
84 eb3ca8ca Giorgos Verigakis
            key = 'inet' if ip['version'] == 4 else 'inet6'
85 eb3ca8ca Giorgos Verigakis
            print '%s: %s' % (key.rjust(margin + 8), ip['addr'])
86 eb3ca8ca Giorgos Verigakis
87 eb3ca8ca Giorgos Verigakis
88 eb3ca8ca Giorgos Verigakis
def print_dict(d, exclude=()):
89 eb3ca8ca Giorgos Verigakis
    if not d:
90 eb3ca8ca Giorgos Verigakis
        return
91 eb3ca8ca Giorgos Verigakis
    margin = max(len(key) for key in d) + 1
92 a6757cbc Giorgos Verigakis
    
93 eb3ca8ca Giorgos Verigakis
    for key, val in sorted(d.items()):
94 eb3ca8ca Giorgos Verigakis
        if key in exclude:
95 eb3ca8ca Giorgos Verigakis
            continue
96 a6757cbc Giorgos Verigakis
        
97 eb3ca8ca Giorgos Verigakis
        if key == 'addresses':
98 eb3ca8ca Giorgos Verigakis
            print '%s:' % 'addresses'.rjust(margin)
99 eb3ca8ca Giorgos Verigakis
            print_addresses(val.get('values', []), margin)
100 eb3ca8ca Giorgos Verigakis
            continue
101 eb3ca8ca Giorgos Verigakis
        elif key == 'servers':
102 eb3ca8ca Giorgos Verigakis
            val = ', '.join(str(x) for x in val['values'])
103 a6757cbc Giorgos Verigakis
        elif isinstance(val, dict):
104 a6757cbc Giorgos Verigakis
            if val.keys() == ['values']:
105 a6757cbc Giorgos Verigakis
                val = val['values']
106 a6757cbc Giorgos Verigakis
            print '%s:' % key.rjust(margin)
107 a6757cbc Giorgos Verigakis
            for key, val in val.items():
108 a6757cbc Giorgos Verigakis
                print '%s: %s' % (key.rjust(margin + 4), val)
109 a6757cbc Giorgos Verigakis
            continue
110 a6757cbc Giorgos Verigakis
        
111 eb3ca8ca Giorgos Verigakis
        print '%s: %s' % (key.rjust(margin), val)
112 eb3ca8ca Giorgos Verigakis
113 eb3ca8ca Giorgos Verigakis
114 a6757cbc Giorgos Verigakis
def print_items(items, title=('id', 'name')):
115 eb3ca8ca Giorgos Verigakis
    for item in items:
116 a6757cbc Giorgos Verigakis
        print ' '.join(str(item.pop(key)) for key in title if key in item)
117 a6757cbc Giorgos Verigakis
        if item:
118 a6757cbc Giorgos Verigakis
            print_dict(item)
119 eb3ca8ca Giorgos Verigakis
            print