Revision a494a741
b/kamaki/cli/commands/cyclades_cli.py | ||
---|---|---|
1 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
|
1 |
# Copyright 2011-2013 GRNET S.A. All rights reserved.
|
|
2 | 2 |
# |
3 | 3 |
# Redistribution and use in source and binary forms, with or |
4 | 4 |
# without modification, are permitted provided that the following |
... | ... | |
33 | 33 |
|
34 | 34 |
from kamaki.cli import command |
35 | 35 |
from kamaki.cli.command_tree import CommandTree |
36 |
from kamaki.cli.utils import print_dict, print_list, bold |
|
36 |
from kamaki.cli.utils import print_dict, print_list, print_items, bold
|
|
37 | 37 |
from kamaki.cli.errors import raiseCLIError, CLISyntaxError |
38 | 38 |
from kamaki.clients.cyclades import CycladesClient, ClientError |
39 | 39 |
from kamaki.cli.argument import FlagArgument, ValueArgument, KeyValueArgument |
... | ... | |
437 | 437 |
super(flavor_list, self).__init__(arguments) |
438 | 438 |
self.arguments['detail'] = FlagArgument('show detailed output', '-l') |
439 | 439 |
|
440 |
@classmethod |
|
441 |
def _print(self, flist): |
|
442 |
for i, flavor in enumerate(flist): |
|
443 |
print(bold('%s. %s' % (i, flavor['name']))) |
|
444 |
print_dict(flavor, exclude=('name'), ident=1) |
|
445 |
|
|
446 | 440 |
def main(self): |
447 | 441 |
super(self.__class__, self).main() |
448 | 442 |
try: |
449 | 443 |
flavors = self.client.list_flavors(self.get_argument('detail')) |
450 | 444 |
except Exception as err: |
451 | 445 |
raiseCLIError(err) |
452 |
self._print(flavors)
|
|
446 |
print_items(flavors, with_redundancy=self.get_argument('detail'))
|
|
453 | 447 |
|
454 | 448 |
|
455 | 449 |
@command(flavor_cmds) |
b/kamaki/cli/commands/test_cli.py | ||
---|---|---|
121 | 121 |
|
122 | 122 |
l1 = [1, 'string', '3', 'many (2 or 3) numbers and strings combined', 5] |
123 | 123 |
|
124 |
d2 = {'key1a': 'val0a', 'key1b': d1, 'key1c': l1}
|
|
124 |
d2 = {'id': 'val0a', 'key0b': d1, 'title': l1}
|
|
125 | 125 |
|
126 | 126 |
l2 = [d2, l1, d1] |
127 | 127 |
|
... | ... | |
130 | 130 |
' check the effects on total result': l1} |
131 | 131 |
|
132 | 132 |
def main(self): |
133 |
from kamaki.cli.utils import print_dict, print_list |
|
133 |
from kamaki.cli.utils import print_dict, print_list, print_items
|
|
134 | 134 |
print('Test simple dict:\n- - -') |
135 | 135 |
print_dict(self.d1) |
136 | 136 |
print('- - -\n') |
... | ... | |
157 | 157 |
print('- - -\n') |
158 | 158 |
print('\nTest non-trivial enumerated list:\n- - -') |
159 | 159 |
print_list(self.l2, with_enumeration=True) |
160 |
print('- - -\n') |
|
161 |
print('\nTest print_items with id:\n- - -') |
|
162 |
print_items([{'id': '42', 'title': 'lalakis 1', 'content': self.d1}, |
|
163 |
{'id': '142', 'title': 'lalakis 2', 'content': self.d2}]) |
|
164 |
print('- - -') |
|
165 |
print('\nTest print_items with id and enumeration:\n- - -') |
|
166 |
print_items([{'id': '42', 'title': 'lalakis 1', 'content': self.d1}, |
|
167 |
{'id': '142', 'title': 'lalakis 2', 'content': self.d2}], |
|
168 |
with_enumeration=True) |
|
169 |
print('- - -') |
|
170 |
print('\nTest print_items with id, title and redundancy:\n- - -') |
|
171 |
print_items([{'id': '42', 'title': 'lalakis 1', 'content': self.d1}, |
|
172 |
{'id': '142', 'title': 'lalakis 2', 'content': self.d2}], |
|
173 |
title=('id', 'title'), |
|
174 |
with_redundancy=True) |
|
175 |
print('- - -') |
|
176 |
print('\nTest print_items with lists- - -') |
|
177 |
print_items([['i00', 'i01', 'i02'], [self.l2, 'i11', self.d1], 3]) |
|
160 | 178 |
print('- - -') |
b/kamaki/cli/utils.py | ||
---|---|---|
191 | 191 |
print('%s%s' % (prefix, item)) |
192 | 192 |
|
193 | 193 |
|
194 |
def print_items(items, title=('id', 'name'), with_enumeration=False): |
|
194 |
def print_items(items, |
|
195 |
title=('id', 'name'), |
|
196 |
with_enumeration=False, |
|
197 |
with_redundancy=False): |
|
198 |
"""print dict or list items in a list, using some values as title |
|
199 |
Objects of next level don't inherit enumeration (default: off) or titles |
|
200 |
|
|
201 |
:param items: (list) items are lists or dict |
|
202 |
:param title: (tuple) keys to use their values as title |
|
203 |
:param with_enumeration: (boolean) enumerate items (order id on title) |
|
204 |
:param with_redundancy: (boolean) values in title also appear on body |
|
205 |
""" |
|
195 | 206 |
for i, item in enumerate(items): |
196 |
if isinstance(item, dict) or isinstance(item, list): |
|
197 |
header = ' '.join(unicode(item.pop(key))\ |
|
198 |
for key in title if key in item) |
|
199 |
if with_enumeration: |
|
200 |
stdout.write('%s. ' % (i + 1)) |
|
207 |
if with_enumeration: |
|
208 |
stdout.write('%s. ' % (i + 1)) |
|
209 |
if isinstance(item, dict): |
|
210 |
title = sorted(set(title).intersection(item.keys())) |
|
211 |
if with_redundancy: |
|
212 |
header = ' '.join(unicode(item[key]) for key in title) |
|
213 |
else: |
|
214 |
header = ' '.join(unicode(item.pop(key)) for key in title) |
|
201 | 215 |
print(bold(header)) |
216 |
else: |
|
217 |
print('- - -') |
|
202 | 218 |
if isinstance(item, dict): |
203 | 219 |
print_dict(item, ident=1) |
204 | 220 |
elif isinstance(item, list): |
205 | 221 |
print_list(item, ident=1) |
222 |
else: |
|
223 |
print(' %s' % item) |
|
206 | 224 |
|
207 | 225 |
|
208 | 226 |
def format_size(size): |
Also available in: Unified diff