/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/progress.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-06-26 19:35:47 UTC
  • mfrom: (1813.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20060626193547-43661d1377f72b4d
(robertc) Misc minor typos and the like.

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
 
59
59
 
60
60
 
61
 
def ProgressBar(to_file=sys.stderr, **kwargs):
 
61
def ProgressBar(to_file=None, **kwargs):
62
62
    """Abstract factory"""
 
63
    if to_file is None:
 
64
        to_file = sys.stderr
63
65
    if _supports_progress(to_file):
64
66
        return TTYProgressBar(to_file=to_file, **kwargs)
65
67
    else:
70
72
    """A stack of progress bars."""
71
73
 
72
74
    def __init__(self,
73
 
                 to_file=sys.stderr,
 
75
                 to_file=None,
74
76
                 show_pct=False,
75
77
                 show_spinner=True,
76
78
                 show_eta=False,
77
79
                 show_bar=True,
78
80
                 show_count=True,
79
 
                 to_messages_file=sys.stdout,
 
81
                 to_messages_file=None,
80
82
                 klass=None):
81
83
        """Setup the stack with the parameters the progress bars should have."""
 
84
        if to_file is None:
 
85
            to_file = sys.stderr
 
86
        if to_messages_file is None:
 
87
            to_messages_file = sys.stdout
82
88
        self._to_file = to_file
83
89
        self._show_pct = show_pct
84
90
        self._show_spinner = show_spinner
128
134
class _BaseProgressBar(object):
129
135
 
130
136
    def __init__(self,
131
 
                 to_file=sys.stderr,
 
137
                 to_file=None,
132
138
                 show_pct=False,
133
139
                 show_spinner=False,
134
 
                 show_eta=True,
 
140
                 show_eta=False,
135
141
                 show_bar=True,
136
142
                 show_count=True,
137
 
                 to_messages_file=sys.stdout,
 
143
                 to_messages_file=None,
138
144
                 _stack=None):
139
145
        object.__init__(self)
 
146
        if to_file is None:
 
147
            to_file = sys.stderr
 
148
        if to_messages_file is None:
 
149
            to_messages_file = sys.stdout
140
150
        self.to_file = to_file
141
151
        self.to_messages_file = to_messages_file
142
152
        self.last_msg = None
195
205
    def child_progress(self, **kwargs):
196
206
        return DummyProgress(**kwargs)
197
207
 
 
208
 
198
209
class DotsProgressBar(_BaseProgressBar):
199
210
 
200
211
    def __init__(self, **kwargs):
209
220
        if msg and msg != self.last_msg:
210
221
            if self.need_nl:
211
222
                self.to_file.write('\n')
212
 
            
213
223
            self.to_file.write(msg + ': ')
214
224
            self.last_msg = msg
215
225
        self.need_nl = True
218
228
    def clear(self):
219
229
        if self.need_nl:
220
230
            self.to_file.write('\n')
 
231
        self.need_nl = False
221
232
        
222
233
    def child_update(self, message, current, total):
223
234
        self.tick()
 
235
 
224
236
    
225
237
class TTYProgressBar(_BaseProgressBar):
226
238
    """Progress bar display object.
250
262
        _BaseProgressBar.__init__(self, **kwargs)
251
263
        self.spin_pos = 0
252
264
        self.width = terminal_width()
253
 
        self.start_time = None
254
265
        self.last_updates = deque()
255
266
        self.child_fraction = 0
256
267
    
257
268
 
258
 
    def throttle(self):
 
269
    def throttle(self, old_msg):
259
270
        """Return True if the bar was updated too recently"""
260
271
        # time.time consistently takes 40/4000 ms = 0.01 ms.
261
272
        # but every single update to the pb invokes it.
262
273
        # so we use time.clock which takes 20/4000 ms = 0.005ms
263
274
        # on the downside, time.clock() appears to have approximately
264
275
        # 10ms granularity, so we treat a zero-time change as 'throttled.'
265
 
        
266
276
        now = time.clock()
 
277
        if self.start_time is not None and (now - self.start_time) < 1:
 
278
            return True
 
279
        if old_msg != self.last_msg:
 
280
            return False
267
281
        interval = now - self.last_update
268
282
        # if interval > 0
269
283
        if interval < self.MIN_PAUSE:
273
287
        self.last_update = now
274
288
        return False
275
289
        
276
 
 
277
290
    def tick(self):
278
291
        self.update(self.last_msg, self.last_cnt, self.last_total, 
279
292
                    self.child_fraction)
291
304
            self.last_msg = ''
292
305
        self.tick()
293
306
 
294
 
 
295
307
    def update(self, msg, current_cnt=None, total_cnt=None, 
296
308
               child_fraction=0):
297
309
        """Update and redraw progress bar."""
 
310
        if msg is None:
 
311
            msg = self.last_msg
 
312
 
 
313
        if total_cnt is None:
 
314
            total_cnt = self.last_total
298
315
 
299
316
        if current_cnt < 0:
300
317
            current_cnt = 0
326
343
        # but multiple that by 4000 calls -> starts to cost.
327
344
        # so anything to make this function call faster
328
345
        # will improve base 'diff' time by up to 0.1 seconds.
329
 
        if old_msg == self.last_msg and self.throttle():
 
346
        if self.throttle(old_msg):
330
347
            return
331
348
 
332
349
        if self.show_eta and self.start_time and self.last_total:
397
414
 
398
415
class ChildProgress(_BaseProgressBar):
399
416
    """A progress indicator that pushes its data to the parent"""
 
417
 
400
418
    def __init__(self, _stack, **kwargs):
401
419
        _BaseProgressBar.__init__(self, _stack=_stack, **kwargs)
402
420
        self.parent = _stack.top()