Rev 3020: Add bzrlib.progress.detect_display. in http://people.ubuntu.com/~robertc/baz2.0/nested-pb

Robert Collins robertc at robertcollins.net
Tue Nov 20 01:50:54 GMT 2007


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

------------------------------------------------------------
revno: 3020
revision-id:robertc at robertcollins.net-20071120015047-ynshd1ecwt7acrou
parent: robertc at robertcollins.net-20071120010256-vj0q61gjvejvnhfa
committer: Robert Collins <robertc at robertcollins.net>
branch nick: pb.simplify
timestamp: Tue 2007-11-20 12:50:47 +1100
message:
  Add bzrlib.progress.detect_display.
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 01:02:56 +0000
+++ b/bzrlib/progress.py	2007-11-20 01:50:47 +0000
@@ -72,9 +72,31 @@
     return True
 
 
+display_types = {}
 _progress_bar_types = {}
 
 
+def detect_display(to_file=None):
+    """Detect the display type needed.
+
+    :param to_file: An optional file that the display will be used to output
+        to. If supplied, a text display is assumed, otherwise a GUI may be
+        probed for.
+    """
+    requested_bar_type = os.environ.get('BZR_PROGRESS_BAR')
+    if requested_bar_type:
+        factory = display_types.get(requested_bar_type.lower(), None)
+        if factory is not None:
+            return factory
+        else:
+            raise errors.InvalidProgressBarType(requested_bar_type,
+                display_types.keys())
+    if to_file is None:
+        # XXX: Allow gui etc probing here
+        to_file = sys.stderr
+    return DotsTaskDisplay
+
+
 def ProgressBar(to_file=None, **kwargs):
     """Abstract factory"""
     if to_file is None:
@@ -363,6 +385,13 @@
         pass
 
 
+display_types['none'] = SilentTaskDisplay
+display_types['dummy'] = SilentTaskDisplay
+display_types['silent'] = SilentTaskDisplay
+display_types['dots'] = DotsTaskDisplay
+# display_types['tty'] = TTYTaskDisplay
+
+
 class DotsProgressBar(_BaseProgressBar):
 
     def __init__(self, **kwargs):

=== modified file 'bzrlib/tests/test_progress.py'
--- a/bzrlib/tests/test_progress.py	2007-11-20 01:02:56 +0000
+++ b/bzrlib/tests/test_progress.py	2007-11-20 01:50:47 +0000
@@ -19,6 +19,8 @@
 
 from bzrlib import errors
 from bzrlib.progress import (
+        detect_display,
+        display_types,
         ChildProgress,
         CountedTask,
         DotsProgressBar,
@@ -517,3 +519,24 @@
         # But now it should add a dot.
         display.task_changed()
         self.assertEqual('message:.', output.getvalue())
+
+
+class TestDisplayTypeSelection(TestCase):
+
+    def test_via_environment(self):
+        os.environ['BZR_PROGRESS_BAR'] = 'foo'
+        old_display = display_types.copy()
+        try:
+            display_types['foo'] = 'result'
+            self.assertEqual('result', detect_display(None))
+        finally:
+            display_types.clear()
+            display_types.update(old_display)
+
+    def test_via_non_tty_file(self):
+        # Note that in future we probably want to allow plugins to change the
+        # lookup if they have better facilities. That may break this test, and
+        # if so we should hook the test into the method plugins use to extend
+        # the defaults.
+        a_file = StringIO()
+        self.assertEqual(DotsTaskDisplay, detect_display(a_file))



More information about the bazaar-commits mailing list