~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/progress.py

  • Committer: Blake Winton
  • Date: 2007-10-16 16:02:01 UTC
  • mto: This revision was merged to the branch mainline in revision 2921.
  • Revision ID: bwinton@latte.ca-20071016160201-os2bci2ujf7in7an
Change 'print >> f,'s to 'f.write('s.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
18
18
 
19
 
"""Progress indicators.
20
 
 
21
 
The usual way to use this is via bzrlib.ui.ui_factory.nested_progress_bar which
22
 
will maintain a ProgressBarStack for you.
23
 
 
24
 
For direct use, the factory ProgressBar will return an auto-detected progress
25
 
bar that should match your terminal type. You can manually create a
26
 
ProgressBarStack too if you need multiple levels of cooperating progress bars.
27
 
Note that bzrlib's internal functions use the ui module, so if you are using
28
 
bzrlib it really is best to use bzrlib.ui.ui_factory.
 
19
"""Simple text-mode progress indicator.
 
20
 
 
21
To display an indicator, create a ProgressBar object.  Call it,
 
22
passing Progress objects indicating the current state.  When done,
 
23
call clear().
 
24
 
 
25
Progress is suppressed when output is not sent to a terminal, so as
 
26
not to clutter log files.
29
27
"""
30
28
 
 
29
# TODO: should be a global option e.g. --silent that disables progress
 
30
# indicators, preferably without needing to adjust all code that
 
31
# potentially calls them.
 
32
 
 
33
# TODO: If not on a tty perhaps just print '......' for the benefit of IDEs, etc
 
34
 
31
35
# TODO: Optionally show elapsed time instead/as well as ETA; nicer
32
36
# when the rate is unpredictable
33
37
 
294
298
        self.child_fraction = 0
295
299
        self._have_output = False
296
300
    
 
301
 
297
302
    def throttle(self, old_msg):
298
303
        """Return True if the bar was updated too recently"""
299
304
        # time.time consistently takes 40/4000 ms = 0.01 ms.
315
320
        return False
316
321
        
317
322
    def tick(self):
318
 
        self.update(self.last_msg, self.last_cnt, self.last_total,
 
323
        self.update(self.last_msg, self.last_cnt, self.last_total, 
319
324
                    self.child_fraction)
320
325
 
321
326
    def child_update(self, message, current, total):
331
336
            self.last_msg = ''
332
337
        self.tick()
333
338
 
334
 
    def update(self, msg, current_cnt=None, total_cnt=None,
 
339
    def update(self, msg, current_cnt=None, total_cnt=None, 
335
340
               child_fraction=0):
336
341
        """Update and redraw progress bar."""
337
342
        if msg is None:
433
438
        self._have_output = True
434
439
        #self.to_file.flush()
435
440
            
436
 
    def clear(self):
 
441
    def clear(self):        
437
442
        if self._have_output:
438
443
            self.to_file.write('\r%s\r' % (' ' * (self.width - 1)))
439
444
        self._have_output = False
456
461
 
457
462
    def update(self, msg, current_cnt=None, total_cnt=None):
458
463
        self.current = current_cnt
459
 
        if total_cnt is not None:
460
 
            self.total = total_cnt
 
464
        self.total = total_cnt
461
465
        self.message = msg
462
466
        self.child_fraction = 0
463
467
        self.tick()
486
490
    def note(self, *args, **kwargs):
487
491
        self.parent.note(*args, **kwargs)
488
492
 
489
 
 
490
 
class InstrumentedProgress(TTYProgressBar):
491
 
    """TTYProgress variant that tracks outcomes"""
492
 
 
493
 
    def __init__(self, *args, **kwargs):
494
 
        self.always_throttled = True
495
 
        self.never_throttle = False
496
 
        TTYProgressBar.__init__(self, *args, **kwargs)
497
 
 
498
 
    def throttle(self, old_message):
499
 
        if self.never_throttle:
500
 
            result =  False
501
 
        else:
502
 
            result = TTYProgressBar.throttle(self, old_message)
503
 
        if result is False:
504
 
            self.always_throttled = False
505
 
 
506
 
 
 
493
 
507
494
def str_tdelta(delt):
508
495
    if delt is None:
509
496
        return "-:--:--"
561
548
            self.cur_phase = 0
562
549
        else:
563
550
            self.cur_phase += 1
564
 
        assert self.cur_phase < self.total
 
551
        assert self.cur_phase < self.total 
565
552
        self.pb.update(self.message, self.cur_phase, self.total)
 
553
 
 
554
 
 
555
def run_tests():
 
556
    import doctest
 
557
    result = doctest.testmod()
 
558
    if result[1] > 0:
 
559
        if result[0] == 0:
 
560
            print "All tests passed"
 
561
    else:
 
562
        print "No tests to run"
 
563
 
 
564
 
 
565
def demo():
 
566
    sleep = time.sleep
 
567
    
 
568
    print 'dumb-terminal test:'
 
569
    pb = DotsProgressBar()
 
570
    for i in range(100):
 
571
        pb.update('Leoparden', i, 99)
 
572
        sleep(0.1)
 
573
    sleep(1.5)
 
574
    pb.clear()
 
575
    sleep(1.5)
 
576
    
 
577
    print 'smart-terminal test:'
 
578
    pb = ProgressBar(show_pct=True, show_bar=True, show_spinner=False)
 
579
    for i in range(100):
 
580
        pb.update('Elephanten', i, 99)
 
581
        sleep(0.1)
 
582
    sleep(2)
 
583
    pb.clear()
 
584
    sleep(1)
 
585
 
 
586
    print 'done!'
 
587
 
 
588
if __name__ == "__main__":
 
589
    demo()