Revision 46d130c9 kamaki/cli/utils/__init__.py

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

  

Also available in: Unified diff