~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/progress.py

  • Committer: John Arbash Meinel
  • Date: 2006-07-10 23:00:44 UTC
  • mto: This revision was merged to the branch mainline in revision 1854.
  • Revision ID: john@arbash-meinel.com-20060710230044-8bbfddd920940ffe
new env var 'BZR_PROGRESS_BAR' to select the exact progress type

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
import os
42
42
 
43
43
import bzrlib.errors as errors
44
 
from bzrlib.trace import mutter 
 
44
from bzrlib.trace import mutter
45
45
 
46
46
 
47
47
def _supports_progress(f):
48
 
    if not hasattr(f, 'isatty'):
 
48
    isatty = getattr(f, 'isatty', None)
 
49
    if isatty is None:
49
50
        return False
50
 
    if not f.isatty():
 
51
    if not isatty():
51
52
        return False
52
53
    if os.environ.get('TERM') == 'dumb':
53
54
        # e.g. emacs compile window
55
56
    return True
56
57
 
57
58
 
 
59
_progress_bar_types = {}
 
60
 
58
61
 
59
62
def ProgressBar(to_file=None, **kwargs):
60
63
    """Abstract factory"""
61
64
    if to_file is None:
62
65
        to_file = sys.stderr
63
 
    if _supports_progress(to_file):
64
 
        return TTYProgressBar(to_file=to_file, **kwargs)
 
66
    requested_bar_type = os.environ.get('BZR_PROGRESS_BAR')
 
67
    # An value of '' or not set reverts to standard processing
 
68
    if requested_bar_type in (None, ''):
 
69
        if _supports_progress(to_file):
 
70
            return TTYProgressBar(to_file=to_file, **kwargs)
 
71
        else:
 
72
            return DotsProgressBar(to_file=to_file, **kwargs)
65
73
    else:
66
 
        return DotsProgressBar(to_file=to_file, **kwargs)
67
 
    
 
74
        # Minor sanitation to prevent spurious errors
 
75
        requested_bar_type = requested_bar_type.lower().strip()
 
76
        # TODO: jam 20060710 Arguably we shouldn't raise an exception
 
77
        #       but should instead just disable progress bars if we
 
78
        #       don't recognize the type
 
79
        if requested_bar_type not in _progress_bar_types:
 
80
            raise errors.InvalidProgressBarType(requested_bar_type,
 
81
                                                _progress_bar_types.keys())
 
82
        return _progress_bar_types[requested_bar_type](to_file=to_file, **kwargs)
 
83
 
68
84
 
69
85
class ProgressBarStack(object):
70
86
    """A stack of progress bars."""
204
220
        return DummyProgress(**kwargs)
205
221
 
206
222
 
 
223
_progress_bar_types['dummy'] = DummyProgress
 
224
_progress_bar_types['none'] = DummyProgress
 
225
 
 
226
 
207
227
class DotsProgressBar(_BaseProgressBar):
208
228
 
209
229
    def __init__(self, **kwargs):
231
251
    def child_update(self, message, current, total):
232
252
        self.tick()
233
253
 
 
254
 
 
255
_progress_bar_types['dots'] = DotsProgressBar
 
256
 
234
257
    
235
258
class TTYProgressBar(_BaseProgressBar):
236
259
    """Progress bar display object.
417
440
        #self.to_file.flush()        
418
441
 
419
442
 
 
443
_progress_bar_types['tty'] = TTYProgressBar
 
444
 
 
445
 
420
446
class ChildProgress(_BaseProgressBar):
421
447
    """A progress indicator that pushes its data to the parent"""
422
448