~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/progress.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-30 12:52:54 UTC
  • mto: This revision was merged to the branch mainline in revision 6418.
  • Revision ID: jelmer@samba.org-20111230125254-igy1abnixsvulfqd
Simplify code a bit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
 
18
17
"""Progress indicators.
19
18
 
20
19
The usual way to use this is via bzrlib.ui.ui_factory.nested_progress_bar which
21
20
will manage a conceptual stack of nested activities.
22
21
"""
23
22
 
 
23
from __future__ import absolute_import
24
24
 
25
 
import sys
26
25
import time
27
26
import os
28
27
 
29
28
 
30
 
from bzrlib import (
31
 
    errors,
32
 
    )
33
 
from bzrlib.trace import mutter
34
 
from bzrlib.symbol_versioning import (
35
 
    deprecated_function,
36
 
    deprecated_in,
37
 
    deprecated_method,
38
 
    )
39
 
 
40
 
 
41
29
def _supports_progress(f):
42
30
    """Detect if we can use pretty progress bars on file F.
43
31
 
157
145
                own_fraction = 0.0
158
146
            return self._parent_task._overall_completion_fraction(own_fraction)
159
147
 
160
 
    @deprecated_method(deprecated_in((2, 1, 0)))
161
 
    def note(self, fmt_string, *args):
162
 
        """Record a note without disrupting the progress bar.
163
 
        
164
 
        Deprecated: use ui_factory.note() instead or bzrlib.trace.  Note that
165
 
        ui_factory.note takes just one string as the argument, not a format
166
 
        string and arguments.
167
 
        """
168
 
        if args:
169
 
            self.ui_factory.note(fmt_string % args)
170
 
        else:
171
 
            self.ui_factory.note(fmt_string)
172
 
 
173
148
    def clear(self):
174
149
        # TODO: deprecate this method; the model object shouldn't be concerned
175
150
        # with whether it's shown or not.  Most callers use this because they
184
159
            self.ui_factory.clear_term()
185
160
 
186
161
 
187
 
# NOTE: This is also deprecated; you should provide a ProgressView instead.
188
 
class _BaseProgressBar(object):
189
 
 
190
 
    def __init__(self,
191
 
                 to_file=None,
192
 
                 show_pct=False,
193
 
                 show_spinner=False,
194
 
                 show_eta=False,
195
 
                 show_bar=True,
196
 
                 show_count=True,
197
 
                 to_messages_file=None,
198
 
                 _stack=None):
199
 
        object.__init__(self)
200
 
        if to_file is None:
201
 
            to_file = sys.stderr
202
 
        if to_messages_file is None:
203
 
            to_messages_file = sys.stdout
204
 
        self.to_file = to_file
205
 
        self.to_messages_file = to_messages_file
206
 
        self.last_msg = None
207
 
        self.last_cnt = None
208
 
        self.last_total = None
209
 
        self.show_pct = show_pct
210
 
        self.show_spinner = show_spinner
211
 
        self.show_eta = show_eta
212
 
        self.show_bar = show_bar
213
 
        self.show_count = show_count
214
 
        self._stack = _stack
215
 
        # seed throttler
216
 
        self.MIN_PAUSE = 0.1 # seconds
217
 
        now = time.time()
218
 
        # starting now
219
 
        self.start_time = now
220
 
        # next update should not throttle
221
 
        self.last_update = now - self.MIN_PAUSE - 1
222
 
 
223
 
    def finished(self):
224
 
        """Return this bar to its progress stack."""
225
 
        self.clear()
226
 
        self._stack.return_pb(self)
227
 
 
228
 
    def note(self, fmt_string, *args, **kwargs):
229
 
        """Record a note without disrupting the progress bar."""
230
 
        self.clear()
231
 
        self.to_messages_file.write(fmt_string % args)
232
 
        self.to_messages_file.write('\n')
233
 
 
234
 
 
235
162
class DummyProgress(object):
236
163
    """Progress-bar standin that does nothing.
237
164
 
253
180
    def clear(self):
254
181
        pass
255
182
 
256
 
    def note(self, fmt_string, *args, **kwargs):
257
 
        """See _BaseProgressBar.note()."""
258
 
 
259
183
    def child_progress(self, **kwargs):
260
184
        return DummyProgress(**kwargs)
261
185
 
316
240
        else:
317
241
            self.cur_phase += 1
318
242
        self.pb.update(self.message, self.cur_phase, self.total)
319
 
 
320
 
 
321
 
_progress_bar_types = {}
322
 
_progress_bar_types['dummy'] = DummyProgress
323
 
_progress_bar_types['none'] = DummyProgress