Revision a6a44506

b/kamaki/clients/__init__.py
497 497
class Waiter(object):
498 498

  
499 499
    def _wait(
500
            self, item_id, current_status, get_status,
501
            delay=1, max_wait=100, wait_cb=None):
502
        """Wait for item while its status is current_status
500
            self, item_id, wait_status, get_status,
501
            delay=1, max_wait=100, wait_cb=None, wait_for_status=False):
502
        """Wait while the item is still in wait_status or to reach it
503 503

  
504 504
        :param server_id: integer (str or int)
505 505

  
506
        :param current_status: (str)
506
        :param wait_status: (str)
507 507

  
508 508
        :param get_status: (method(self, item_id)) if called, returns
509 509
            (status, progress %) If no way to tell progress, return None
......
513 513
        :param wait_cb: (method(total steps)) returns a generator for
514 514
            reporting progress or timeouts i.e., for a progress bar
515 515

  
516
        :param wait_for_status: (bool) wait FOR (True) or wait WHILE (False)
517

  
516 518
        :returns: (str) the new mode if successful, (bool) False if timed out
517 519
        """
518 520
        status, progress = get_status(self, item_id)
......
521 523
            wait_gen = wait_cb(max_wait // delay)
522 524
            wait_gen.next()
523 525

  
524
        if status != current_status:
525
            if wait_cb:
526
                try:
527
                    wait_gen.next()
528
                except Exception:
529
                    pass
526
        if wait_for_status ^ (status != wait_status):
527
            # if wait_cb:
528
            #     try:
529
            #         wait_gen.next()
530
            #     except Exception:
531
            #         pass
530 532
            return status
531 533
        old_wait = total_wait = 0
532 534

  
533
        while status == current_status and total_wait <= max_wait:
535
        while (wait_for_status ^ (status == wait_status)) and (
536
                total_wait <= max_wait):
534 537
            if wait_cb:
535 538
                try:
536 539
                    for i in range(total_wait - old_wait):
......
549 552
                        wait_gen.next()
550 553
                except:
551 554
                    pass
552
        return status if status != current_status else False
555
        return status if (wait_for_status ^ (status != wait_status)) else False
556

  
557
    def wait_for(
558
            self, item_id, target_status, get_status,
559
            delay=1, max_wait=100, wait_cb=None):
560
        self._wait(
561
            item_id, target_status, get_status, delay, max_wait, wait_cb,
562
            wait_for_status=True)
563

  
564
    def wait_while(
565
            self, item_id, target_status, get_status,
566
            delay=1, max_wait=100, wait_cb=None):
567
        self._wait(
568
            item_id, target_status, get_status, delay, max_wait, wait_cb,
569
            wait_for_status=False)

Also available in: Unified diff