~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/progress.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

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
 
70
58
    Code updating the task may also set fields as hints about how to display
71
59
    it: show_pct, show_spinner, show_eta, show_count, show_bar.  UIs
72
60
    will not necessarily respect all these fields.
73
 
    
 
61
 
 
62
    The message given when updating a task must be unicode, not bytes.
 
63
 
74
64
    :ivar update_latency: The interval (in seconds) at which the PB should be
75
65
        updated.  Setting this to zero suggests every update should be shown
76
66
        synchronously.
118
108
            self.msg)
119
109
 
120
110
    def update(self, msg, current_cnt=None, total_cnt=None):
 
111
        """Report updated task message and if relevent progress counters
 
112
 
 
113
        The message given must be unicode, not a byte string.
 
114
        """
121
115
        self.msg = msg
122
116
        self.current_cnt = current_cnt
123
117
        if total_cnt:
157
151
                own_fraction = 0.0
158
152
            return self._parent_task._overall_completion_fraction(own_fraction)
159
153
 
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
154
    def clear(self):
174
155
        # TODO: deprecate this method; the model object shouldn't be concerned
175
156
        # with whether it's shown or not.  Most callers use this because they
184
165
            self.ui_factory.clear_term()
185
166
 
186
167
 
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
168
class DummyProgress(object):
236
169
    """Progress-bar standin that does nothing.
237
170
 
253
186
    def clear(self):
254
187
        pass
255
188
 
256
 
    def note(self, fmt_string, *args, **kwargs):
257
 
        """See _BaseProgressBar.note()."""
258
 
 
259
189
    def child_progress(self, **kwargs):
260
190
        return DummyProgress(**kwargs)
261
191
 
316
246
        else:
317
247
            self.cur_phase += 1
318
248
        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