root / kamaki / utils.py @ 1cb061f2
History | View | Annotate | Download (3.3 kB)
1 |
# Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
def print_dict(d, exclude=(), ident= 0): |
35 |
if 0 == len(d): |
36 |
return
|
37 |
margin = max(1 + max(len(key) for key in d), ident) |
38 |
for key, val in d.items(): |
39 |
if key in exclude: |
40 |
continue
|
41 |
print_str = '%s:' % unicode(key) |
42 |
if isinstance(val, dict): |
43 |
print(print_str.rjust(margin)+' {')
|
44 |
print_dict(val, exclude = exclude, ident = margin + 6)
|
45 |
print '}'.rjust(margin) |
46 |
elif isinstance(val, list): |
47 |
print(print_str.rjust(margin)+' [')
|
48 |
print_list(val, exclude = exclude, ident = margin + 6)
|
49 |
print ']'.rjust(margin) |
50 |
else:
|
51 |
print print_str.rjust(margin)+' '+unicode(val) |
52 |
|
53 |
def print_list(l, exclude=(), ident = 0): |
54 |
if 0 == len(l): |
55 |
return
|
56 |
margin = max(1 + max(len(item) for item in l), ident) |
57 |
for item in l: |
58 |
if item in exclude: |
59 |
continue
|
60 |
if isinstance(item, dict): |
61 |
print('{'.rjust(margin))
|
62 |
print_dict(item, exclude = exclude, ident = margin + 6)
|
63 |
print '}'.rjust(margin) |
64 |
elif isinstance(item, list): |
65 |
print '['.rjust(margin) |
66 |
print_list(item, exclude = exclude, ident = margin + 6)
|
67 |
print ']'.rjust(margin) |
68 |
else:
|
69 |
print unicode(item).rjust(margin) |
70 |
|
71 |
def print_items(items, title=('id', 'name')): |
72 |
for item in items: |
73 |
print ' '.join(unicode(item.pop(key)) for key in title if key in item) |
74 |
if item:
|
75 |
print_dict(item) |
76 |
print
|
77 |
|
78 |
def format_size(size): |
79 |
units = ('B', 'K', 'M', 'G', 'T') |
80 |
size = float(size)
|
81 |
for unit in units: |
82 |
if size <= 1024: |
83 |
break
|
84 |
size /= 1024
|
85 |
s = ('%.1f' % size).rstrip('.0') |
86 |
return s + unit
|
87 |
|
88 |
def dict_from_args(*args): |
89 |
return {args[i]:args[i+1] for i in range(0, len(args)/2)} |
90 |
|