Rev 3029: Initial new ProgressBar facade. in http://people.ubuntu.com/~robertc/baz2.0/nested-pb

Robert Collins robertc at robertcollins.net
Tue Nov 20 21:29:28 GMT 2007


At http://people.ubuntu.com/~robertc/baz2.0/nested-pb

------------------------------------------------------------
revno: 3029
revision-id:robertc at robertcollins.net-20071120212915-6cxesjsxd86q7swe
parent: robertc at robertcollins.net-20071120094136-gh6uaxitdm0psyzd
committer: Robert Collins <robertc at robertcollins.net>
branch nick: pb.simplify
timestamp: Wed 2007-11-21 08:29:15 +1100
message:
  Initial new ProgressBar facade.
modified:
  bzrlib/progress.py             progress.py-20050610070202-df9faaab791964c0
  bzrlib/tests/test_progress.py  test_progress.py-20060308160359-978c397bc79b7fda
=== modified file 'bzrlib/progress.py'
--- a/bzrlib/progress.py	2007-11-20 09:41:36 +0000
+++ b/bzrlib/progress.py	2007-11-20 21:29:15 +0000
@@ -36,6 +36,7 @@
   * SilentTaskDisplay: For headless operations.
   * DotsTaskDisplay: For non-TTY file stream display.
   * TTYTaskDisplay: For TTY file based display.
+ * ProgressBar: Combines a Task and a Display in a simple to use facade.
 """
 
 # TODO: Optionally show elapsed time instead/as well as ETA; nicer
@@ -769,6 +770,27 @@
         self.pb.update(self.message, self.cur_phase, self.total)
 
 
+class ProgressBar(object):
+    """Combines a Task and a TaskDisplay to yield a simple-to-use bar."""
+
+    def __init__(self, task, display):
+        self._task = task
+        self._display = display
+
+    def clear(self):
+        """Hide the display - the display is finished with for now."""
+        return self._display.clear()
+
+    def note(self, fmt, *args, **kwargs):
+        """Pass a note onto the display."""
+        return self._display.note(fmt, *args, **kwargs)
+
+    def tick(self):
+        """Tick the task forward, and update the display."""
+        self._task.tick()
+        self._display.task_changed()
+
+
 def run_tests():
     import doctest
     result = doctest.testmod()

=== modified file 'bzrlib/tests/test_progress.py'
--- a/bzrlib/tests/test_progress.py	2007-11-20 09:41:36 +0000
+++ b/bzrlib/tests/test_progress.py	2007-11-20 21:29:15 +0000
@@ -27,6 +27,7 @@
         DotsTaskDisplay,
         DummyProgress,
         KnownLengthTask,
+        ProgressBar,
         ProgressBarStack,
         SilentTaskDisplay,
         TTYProgressBar,
@@ -57,6 +58,42 @@
             self.always_throttled = False
         
 
+class RecordingTask(object):
+    """A tool for testing, records the calls made to a task."""
+
+    def __init__(self, task):
+        self._calls = []
+        self._task = task
+
+    def get_message(self):
+        self._calls.append(("get_message",))
+        return self._task.get_message()
+
+    def tick(self):
+        self._calls.append(("tick",))
+        return self._task.tick()
+
+
+class RecordingDisplay(object):
+    """A tool for testing, records the calls made to a TaskDisplay."""
+
+    def __init__(self, display):
+        self._calls = []
+        self._display = display
+
+    def clear(self):
+        self._calls.append(("clear",))
+        return self._display.clear()
+
+    def note(self, fmt, *args, **kwargs):
+        self._calls.append(("note", fmt, args, kwargs))
+        return self._display.note(fmt, *args, **kwargs)
+
+    def task_changed(self):
+        self._calls.append(("task_changed",))
+        return self._display.task_changed()
+
+
 class _TTYStringIO(StringIO):
     """A helper class which makes a StringIO look like a terminal"""
 
@@ -612,3 +649,38 @@
         # The same caveats apply here as for test_via_non_tty_file.
         a_file = _TTYStringIO()
         self.assertEqual(TTYTaskDisplay, detect_display(a_file))
+
+
+class TestProgressBar(TestCase):
+
+    def test_construct(self):
+        task = CountedTask('')
+        display = SilentTaskDisplay(task)
+        bar = ProgressBar(task, display)
+
+    def get_test_bar(self):
+        task = RecordingTask(CountedTask(''))
+        display = RecordingDisplay(SilentTaskDisplay(task))
+        bar = ProgressBar(task, display)
+        return task, display, bar
+
+    def test_clear(self):
+        """clear calls clear on the display."""
+        task, display, bar = self.get_test_bar()
+        bar.clear()
+        self.assertEqual([], task._calls)
+        self.assertEqual([('clear',)], display._calls)
+
+    def test_note(self):
+        """note calls note on the display."""
+        task, display, bar = self.get_test_bar()
+        bar.note("fmt", "arg")
+        self.assertEqual([], task._calls)
+        self.assertEqual([('note', "fmt", ("arg",), {})], display._calls)
+
+    def test_tick(self):
+        """tick calls tick on the task and task_changed on the display."""
+        task, display, bar = self.get_test_bar()
+        bar.tick()
+        self.assertEqual([('tick',)], task._calls)
+        self.assertEqual([('task_changed',)], display._calls)



More information about the bazaar-commits mailing list