501
501
+# arch-tag: d1541a25-eac5-4de9-a476-08a7cecd5683
503
*** added file 'bzrlib/progress.py'
505
+++ bzrlib/progress.py
507
+# Copyright (C) 2005 Aaron Bentley
508
+# <aaron.bentley@utoronto.ca>
510
+# This program is free software; you can redistribute it and/or modify
511
+# it under the terms of the GNU General Public License as published by
512
+# the Free Software Foundation; either version 2 of the License, or
513
+# (at your option) any later version.
515
+# This program is distributed in the hope that it will be useful,
516
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
517
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
518
+# GNU General Public License for more details.
520
+# You should have received a copy of the GNU General Public License
521
+# along with this program; if not, write to the Free Software
522
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
527
+class Progress(object):
528
+ def __init__(self, units, current, total=None):
530
+ self.current = current
533
+ def _get_percent(self):
534
+ if self.total is not None and self.current is not None:
535
+ return 100.0 * self.current / self.total
537
+ percent = property(_get_percent)
540
+ if self.total is not None:
541
+ return "%i of %i %s %.1f%%" % (self.current, self.total, self.units,
544
+ return "%i %s" (self.current, self.units)
546
+class ProgressBar(object):
547
+ def __init__(self):
549
+ object.__init__(self)
551
+ def __call__(self, progress):
552
+ if self.start is None:
553
+ self.start = datetime.datetime.now()
554
+ progress_bar(progress, start_time=self.start)
556
+def divide_timedelta(delt, divisor):
557
+ """Divides a timedelta object"""
558
+ return datetime.timedelta(float(delt.days)/divisor,
559
+ float(delt.seconds)/divisor,
560
+ float(delt.microseconds)/divisor)
562
+def str_tdelta(delt):
565
+ return str(datetime.timedelta(delt.days, delt.seconds))
567
+def get_eta(start_time, progress, enough_samples=20):
568
+ if start_time is None or progress.current == 0:
570
+ elif progress.current < enough_samples:
572
+ elapsed = datetime.datetime.now() - start_time
573
+ total_duration = divide_timedelta((elapsed) * long(progress.total),
575
+ if elapsed < total_duration:
576
+ eta = total_duration - elapsed
578
+ eta = total_duration - total_duration
581
+def progress_bar(progress, start_time=None):
582
+ eta = get_eta(start_time, progress)
583
+ if start_time is not None:
584
+ eta_str = " "+str_tdelta(eta)
588
+ fmt = " %i of %i %s (%.1f%%)"
589
+ f = fmt % (progress.total, progress.total, progress.units, 100.0)
592
+ if start_time is not None:
593
+ cols -= len(eta_str)
594
+ markers = int (float(cols) * progress.current / progress.total)
595
+ txt = fmt % (progress.current, progress.total, progress.units,
597
+ sys.stderr.write("\r[%s%s]%s%s" % ('='*markers, ' '*(cols-markers), txt,
600
+def clear_progress_bar():
601
+ sys.stderr.write('\r%s\r' % (' '*79))
603
+def spinner_str(progress, show_text=False):
605
+ Produces the string for a textual "spinner" progress indicator
606
+ :param progress: an object represinting current progress
607
+ :param show_text: If true, show progress text as well
608
+ :return: The spinner string
610
+ >>> spinner_str(Progress("baloons", 0))
612
+ >>> spinner_str(Progress("baloons", 5))
614
+ >>> spinner_str(Progress("baloons", 6), show_text=True)
617
+ positions = ('|', '/', '-', '\\')
618
+ text = positions[progress.current % 4]
620
+ text+=" %i %s" % (progress.current, progress.units)
623
+def spinner(progress, show_text=False, output=sys.stderr):
625
+ Update a spinner progress indicator on an output
626
+ :param progress: The progress to display
627
+ :param show_text: If true, show text as well as spinner
628
+ :param output: The output to write to
630
+ >>> spinner(Progress("baloons", 6), show_text=True, output=sys.stdout)
633
+ output.write('\r%s' % spinner_str(progress, show_text))
637
+ result = doctest.testmod()
640
+ print "All tests passed"
642
+ print "No tests to run"
643
+if __name__ == "__main__":
646
503
*** added directory 'testdata'
647
504
*** added file 'testdata/diff'