Revision 46d130c9
b/kamaki/cli/utils/__init__.py | ||
---|---|---|
62 | 62 |
print w |
63 | 63 |
|
64 | 64 |
|
65 |
def _write(w): |
|
66 |
"""stdout.write wrapper is used to help unittests check what is printed""" |
|
67 |
stdout.write(w) |
|
68 |
|
|
69 |
|
|
65 | 70 |
def suggest_missing(miss=None, exclude=[]): |
66 | 71 |
global suggest |
67 | 72 |
sgs = dict(suggest) |
... | ... | |
229 | 234 |
|
230 | 235 |
def page_hold(index, limit, maxlen): |
231 | 236 |
"""Check if there are results to show, and hold the page when needed |
232 |
:param index: (int) > 0 |
|
237 |
:param index: (int) > 0, index of current element
|
|
233 | 238 |
:param limit: (int) 0 < limit <= max, page hold if limit mod index == 0 |
234 | 239 |
:param maxlen: (int) Don't hold if index reaches maxlen |
235 | 240 |
|
236 | 241 |
:returns: True if there are more to show, False if all results are shown |
237 | 242 |
""" |
238 |
if index >= limit and index % limit == 0: |
|
239 |
if index >= maxlen: |
|
240 |
return False |
|
241 |
else: |
|
242 |
print('(%s listed - %s more - "enter" to continue)' % ( |
|
243 |
index, |
|
244 |
maxlen - index)) |
|
245 |
c = ' ' |
|
246 |
while c != '\n': |
|
247 |
c = stdin.read(1) |
|
243 |
if index >= maxlen: |
|
244 |
return False |
|
245 |
if index and index % limit == 0: |
|
246 |
raw_input('(%s listed - %s more - "enter" to continue)' % ( |
|
247 |
index, maxlen - index)) |
|
248 | 248 |
return True |
249 | 249 |
|
250 | 250 |
|
... | ... | |
266 | 266 |
:param page_size: (int) show results in pages of page_size items, enter to |
267 | 267 |
continue |
268 | 268 |
""" |
269 |
if not items: |
|
270 |
return |
|
271 |
elif not ( |
|
272 |
isinstance(items, dict) or isinstance( |
|
273 |
items, list) or isinstance(items, dict)): |
|
274 |
print '%s' % items |
|
269 |
if not (isinstance(items, dict) or isinstance(items, list) or isinstance( |
|
270 |
items, tuple)): |
|
271 |
_print('%s' % items if items is not None else '') |
|
275 | 272 |
return |
276 | 273 |
|
274 |
page_size = int(page_size) |
|
277 | 275 |
try: |
278 |
page_size = int(page_size) if int(page_size) > 0 else len(items)
|
|
276 |
page_size = page_size if page_size > 0 else len(items)
|
|
279 | 277 |
except: |
280 | 278 |
page_size = len(items) |
281 | 279 |
num_of_pages = len(items) // page_size |
282 | 280 |
num_of_pages += 1 if len(items) % page_size else 0 |
283 | 281 |
for i, item in enumerate(items): |
284 | 282 |
if with_enumeration: |
285 |
stdout.write('%s. ' % (i + 1)) |
|
286 |
if isinstance(item, dict): |
|
287 |
title = sorted(set(title).intersection(item.keys())) |
|
288 |
if with_redundancy: |
|
289 |
header = ' '.join('%s' % item[key] for key in title) |
|
290 |
else: |
|
291 |
header = ' '.join('%s' % item.pop(key) for key in title) |
|
292 |
print(bold(header)) |
|
283 |
_write('%s. ' % (i + 1)) |
|
293 | 284 |
if isinstance(item, dict): |
285 |
item = dict(item) |
|
286 |
title = sorted(set(title).intersection(item)) |
|
287 |
pick = item.get if with_redundancy else item.pop |
|
288 |
header = ' '.join('%s' % pick(key) for key in title) |
|
289 |
_print(bold(header)) |
|
294 | 290 |
print_dict(item, indent=INDENT_TAB) |
295 |
elif isinstance(item, list): |
|
291 |
elif isinstance(item, list) or isinstance(item, tuple):
|
|
296 | 292 |
print_list(item, indent=INDENT_TAB) |
297 | 293 |
else: |
298 |
print(' %s' % item) |
|
294 |
_print(' %s' % item)
|
|
299 | 295 |
page_hold(i + 1, page_size, len(items)) |
300 | 296 |
|
301 | 297 |
|
b/kamaki/cli/utils/test.py | ||
---|---|---|
201 | 201 |
call_counter = len(PR.mock_calls) |
202 | 202 |
self.assertEqual(sorted(real_calls), sorted(exp_calls)) |
203 | 203 |
|
204 |
@patch('__builtin__.raw_input') |
|
205 |
def test_page_hold(self, RI): |
|
206 |
from kamaki.cli.utils import page_hold |
|
207 |
ri_counter = 0 |
|
208 |
for args, expected in ( |
|
209 |
((0, 0, 0), False), |
|
210 |
((1, 3, 10), True), |
|
211 |
((3, 3, 10), True), |
|
212 |
((5, 3, 10), True), |
|
213 |
((6, 3, 10), True), |
|
214 |
((10, 3, 10), False), |
|
215 |
((11, 3, 10), False)): |
|
216 |
self.assertEqual(page_hold(*args), expected) |
|
217 |
index, limit, maxlen = args |
|
218 |
if index and index < maxlen and index % limit == 0: |
|
219 |
self.assertEqual(ri_counter + 1, len(RI.mock_calls)) |
|
220 |
self.assertEqual(RI.mock_calls[-1], call( |
|
221 |
'(%s listed - %s more - "enter" to continue)' % ( |
|
222 |
index, maxlen - index))) |
|
223 |
else: |
|
224 |
self.assertEqual(ri_counter, len(RI.mock_calls)) |
|
225 |
ri_counter = len(RI.mock_calls) |
|
226 |
|
|
227 |
@patch('kamaki.cli.utils._print') |
|
228 |
@patch('kamaki.cli.utils._write') |
|
229 |
@patch('kamaki.cli.utils.print_dict') |
|
230 |
@patch('kamaki.cli.utils.print_list') |
|
231 |
@patch('kamaki.cli.utils.page_hold') |
|
232 |
@patch('kamaki.cli.utils.bold', return_value='bold') |
|
233 |
def test_print_items(self, bold, PH, PL, PD, WR, PR): |
|
234 |
from kamaki.cli.utils import print_items, INDENT_TAB |
|
235 |
for args in product( |
|
236 |
( |
|
237 |
42, None, 'simple outputs', |
|
238 |
[1, 2, 3], {1: 1, 2: 2}, (3, 4), |
|
239 |
({'k': 1, 'id': 2}, [5, 6, 7], (8, 9), '10')), |
|
240 |
(('id', 'name'), ('something', 2), ('lala', )), |
|
241 |
(False, True), |
|
242 |
(False, True), |
|
243 |
(0, 1, 2, 10)): |
|
244 |
items, title, with_enumeration, with_redundancy, page_size = args |
|
245 |
wr_counter, pr_counter = len(WR.mock_calls), len(PR.mock_calls) |
|
246 |
pl_counter, pd_counter = len(PL.mock_calls), len(PD.mock_calls) |
|
247 |
bold_counter, ph_counter = len(bold.mock_calls), len(PH.mock_calls) |
|
248 |
print_items(*args) |
|
249 |
if not (isinstance(items, dict) or isinstance( |
|
250 |
items, list) or isinstance(items, tuple)): |
|
251 |
self.assertEqual(PR.mock_calls[-1], call( |
|
252 |
'%s' % items if items is not None else '')) |
|
253 |
else: |
|
254 |
for i, item in enumerate(items): |
|
255 |
if with_enumeration: |
|
256 |
self.assertEqual( |
|
257 |
WR.mock_calls[wr_counter], |
|
258 |
call('%s. ' % (i + 1))) |
|
259 |
wr_counter += 1 |
|
260 |
if isinstance(item, dict): |
|
261 |
title = sorted(set(title).intersection(item)) |
|
262 |
pick = item.get if with_redundancy else item.pop |
|
263 |
header = ' '.join('%s' % pick(key) for key in title) |
|
264 |
self.assertEqual( |
|
265 |
bold.mock_calls[bold_counter], call(header)) |
|
266 |
self.assertEqual( |
|
267 |
PR.mock_calls[pr_counter], call('bold')) |
|
268 |
self.assertEqual( |
|
269 |
PD.mock_calls[pd_counter], |
|
270 |
call(item, indent=INDENT_TAB)) |
|
271 |
pr_counter += 1 |
|
272 |
pd_counter += 1 |
|
273 |
bold_counter += 1 |
|
274 |
elif isinstance(item, list) or isinstance(item, tuple): |
|
275 |
self.assertEqual( |
|
276 |
PL.mock_calls[pl_counter], |
|
277 |
call(item, indent=INDENT_TAB)) |
|
278 |
pl_counter += 1 |
|
279 |
else: |
|
280 |
self.assertEqual( |
|
281 |
PR.mock_calls[pr_counter], call(' %s' % item)) |
|
282 |
pr_counter += 1 |
|
283 |
page_size = page_size if page_size > 0 else len(items) |
|
284 |
self.assertEqual( |
|
285 |
PH.mock_calls[ph_counter], |
|
286 |
call(i + 1, page_size, len(items))) |
|
287 |
ph_counter += 1 |
|
288 |
|
|
204 | 289 |
|
205 | 290 |
if __name__ == '__main__': |
206 | 291 |
from sys import argv |
Also available in: Unified diff