def get_widgets_by_priority(self): class ComparableWidget(Widget): def __eq__(self, other): return self.__class__ is other.__class__ def __ne__(self, other): return not self.__eq__(other) __hash__ = None class FooWidget(ComparableWidget): priority = 1 class BarWidget(ComparableWidget): priority = 2 class BazWidget(ComparableWidget): priority = 3 widgets = [BarWidget(), FooWidget(), BazWidget()] writer = TerminalWriter(StringIO()) progressbar = ProgressBar(widgets, writer) Assert(progressbar.get_widgets_by_priority()) == [(2, BazWidget()), (0, BarWidget()), (1, FooWidget())]
def get_widgets_by_priority(self): class ComparableWidget(Widget): def __eq__(self, other): return self.__class__ is other.__class__ def __ne__(self, other): return not self.__eq__(other) __hash__ = None class FooWidget(ComparableWidget): priority = 1 class BarWidget(ComparableWidget): priority = 2 class BazWidget(ComparableWidget): priority = 3 widgets = [BarWidget(), FooWidget(), BazWidget()] writer = TerminalWriter(StringIO()) progressbar = ProgressBar(widgets, writer) Assert(progressbar.get_widgets_by_priority()) == [ (2, BazWidget()), (0, BarWidget()), (1, FooWidget()) ]
def write(self): stream = StringIO() writer = TerminalWriter(stream, prefix='spam') writer.indent() progressbar = ProgressBar([], writer) progressbar.write('foo', update=False) Assert(stream.getvalue()) == 'spam foo' progressbar.write('bar') Assert(stream.getvalue()) == 'spam foo\rspam bar'
def repr(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer) Assert( repr(progressbar)) == 'ProgressBar([], %r, maxsteps=None)' % writer progressbar = ProgressBar([], writer, maxsteps=100) Assert( repr(progressbar)) == 'ProgressBar([], %r, maxsteps=100)' % writer
def units(self): class FooStepWidget(StepWidget): units = {'foo': lambda x: str(x) + 'spam'} writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer, maxsteps=20) widget = FooStepWidget('foo') Assert(widget.init(progressbar, 100)) == '0spam of 20spam' progressbar.step +=1 Assert(widget.init(progressbar, 100)) == '1spam of 20spam' progressbar.step = progressbar.maxsteps Assert(widget.finish(progressbar, 100)) == '20spam of 20spam'
def size_hint(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer, maxsteps=20) widget = PercentageWidget() assert widget.provides_size_hint Assert(widget.size_hint(progressbar)) == 2 progressbar.step = 1 Assert(widget.size_hint(progressbar)) == 2 progressbar.step = 2 Assert(widget.size_hint(progressbar)) == 3 progressbar.step = 20 Assert(widget.size_hint(progressbar)) == 4
def units(self): class FooStepWidget(StepWidget): units = {'foo': lambda x: str(x) + 'spam'} writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer, maxsteps=20) widget = FooStepWidget('foo') Assert(widget.init(progressbar, 100)) == '0spam of 20spam' progressbar.step += 1 Assert(widget.init(progressbar, 100)) == '1spam of 20spam' progressbar.step = progressbar.maxsteps Assert(widget.finish(progressbar, 100)) == '20spam of 20spam'
def from_string(self): stream = StringIO() writer = TerminalWriter(stream) with Assert.raises(ValueError) as exc: ProgressBar.from_string('$foo', writer) Assert(exc.args[0]) == 'widget not found: foo' progressbar = ProgressBar.from_string('hello $hint:world $percentage', writer, maxsteps=10) progressbar.init() progressbar.finish(hint='me') Assert(stream.getvalue()) == 'hello world 0%\rhello me 100%\n'
def from_string(self): stream = StringIO() writer = TerminalWriter(stream) with Assert.raises(ValueError) as exc: ProgressBar.from_string('$foo', writer) Assert(exc.args[0]) == 'widget not found: foo' progressbar = ProgressBar.from_string( 'hello $hint:world $percentage', writer, maxsteps=10 ) progressbar.init() progressbar.finish(hint='me') Assert(stream.getvalue()) == 'hello world 0%\rhello me 100%\n'
def step(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer) Assert(progressbar.step) == 0 progressbar.step = 100 Assert(progressbar.step) == 100 progressbar = ProgressBar([], writer, maxsteps=100) Assert(progressbar.step) == 0 progressbar.step = 100 Assert(progressbar.step) == 100 with Assert.raises(ValueError): progressbar.step = 200
def update(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer) widget = TimeWidget() widget.init(progressbar, 100) time.sleep(1) Assert(widget.update(progressbar, 100)) == '00:00:01'
def update(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer) widget = Widget() with Assert.raises(NotImplementedError) as exc: widget.update(progressbar, writer.get_width()) Assert(exc.args[0]) == 'Widget.update'
def update(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer, maxsteps=20) widget = PercentageWidget() widget.init(progressbar, writer.get_width()) for i in xrange(5, 96, 5): progressbar.step += 1 result = widget.update(progressbar, writer.get_width()) Assert(result) == '%i%%' % i
def update(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer, maxsteps=20) widget = StepWidget() widget.init(progressbar, writer.get_width()) for i in xrange(1, 21): progressbar.step += 1 result = widget.update(progressbar, writer.get_width()) Assert(len(result)) == widget.size_hint(progressbar) Assert(result) == '%i of 20' % i
def test_text_widget(): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer) widget = TextWidget('foobar') assert widget.provides_size_hint Assert(widget.size_hint(progressbar)) == len('foobar') Assert(widget.init(progressbar, writer.get_width())) == 'foobar' Assert(widget.update(progressbar, writer.get_width())) == 'foobar' Assert(widget.finish(progressbar, writer.get_width())) == 'foobar' Assert(repr(widget)) == "TextWidget('foobar')"
def update(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer, maxsteps=10) widget = PercentageBarWidget() states = [ '[%s]' % (x + '.' * (10 - len(x))) for x in ('#' * i for i in xrange(1, 11)) ] for state in states: progressbar.step += 1 Assert(widget.update(progressbar, 12)) == state
def finish(self): class MyWidget(Widget): update_called = False def update(self, writer, remaining_width, **kwargs): self.update_called = True writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer) widget = MyWidget() widget.finish(progressbar, writer.get_width()) assert widget.update_called
def init(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer, maxsteps=20) widget = StepWidget() Assert(widget.init(progressbar, writer.get_width())) == '0 of 20' Assert(widget.size_hint(progressbar)) == 7 with Assert.raises(ValueError): StepWidget('foo') with Assert.not_raising(ValueError): StepWidget('bytes')
def test_hint_widget(): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer) widget = HintWidget('foo') assert not widget.provides_size_hint Assert(widget.init(progressbar, writer.get_width())) == 'foo' Assert(widget.update(progressbar, writer.get_width(), hint='bar')) == 'bar' Assert(widget.update(progressbar, writer.get_width(), hint='baz')) == 'baz' Assert(widget.finish(progressbar, writer.get_width(), hint='spam')) == 'spam' Assert(repr(widget)) == "HintWidget('foo')" widget.finish(progressbar, writer.get_width()) == u''
def update(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer) widget = DataTransferSpeedWidget() widget.init(progressbar, 100) time.sleep(1) progressbar.step += 50 speed = float(widget.update(progressbar, 100)[:-4]) Assert(speed) > 45.0 Assert(speed) < 55.0 time.sleep(2) progressbar.step += 50 speed = float(widget.update(progressbar, 100)[:-4]) Assert(speed) > 20.0 Assert(speed) < 30.0
def update(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer) states = [ '[.###..]', '[..###.]', '[...###]', '[..###.]', '[.###..]', '[###...]', '[.###..]' ] widget = BarWidget() for state in states: Assert(widget.update(progressbar, 8)) == state widget = BarWidget() widget.position = 10 Assert(widget.update(progressbar, 8)) == '[..###.]' Assert(widget.update(progressbar, 8)) == '[.###..]'
def size_hint(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer) widget = Widget() assert not widget.provides_size_hint Assert(widget.size_hint(progressbar)).is_(None)
def progress(self, description, maxsteps=None, widgets=None): """ Returns a :class:`~brownie.terminal.progress.ProgressBar` object which can be used to write the current progress to the stream. The progress bar is created from the `description` which is a string with the following syntax: Widgets -- the parts of the progress bar which are changed with each update -- are represented in the form ``$[a-zA-Z]+``. Some widgets required that you provide an initial value, this can be done by adding ``:string`` where string is either ``[a-zA-Z]+`` or a double-quoted string. If you want to escape a ``$`` you simply precede it with another ``$``, so ``$$foo` will not be treated as a widget and in the progress bar ``$foo`` will be shown. Quotes (``"``) in strings can be escaped with a backslash (``\``). The following widgets are available: `hint` Shows a string of text that can be given using the `hint` argument at any update performed with :meth:`.ProgressBar.init`, :meth:`.ProgressBar.next` or :meth:`.ProgressBar.finish`. If the argument is not given an empty string is used instead. `percentage` Shows the progress in percent; this requires `maxsteps` to be set. `bar` Shows a simple bar which moves which each update not corresponding with the progress being made. This is useful if you just want to show that something is happening. `sizedbar` Shows a simple progress bar which is being filled corresponding to the percentage of progress. This requires `maxsteps` to be set. `step` Shows the current at maximum number of steps as ``step of steps``, this method takes an initial value determining the unit of each step e.g. if each step represents a byte and you choose `bytes` as a unit a reasonable prefix will be chosen. Supported units: - `bytes` - Uses a binary prefix. This requires `maxsteps` to be set. `time` Shows the elapsed time in hours, minutes and seconds. `speed` Shows the speed in bytes (or with a reasonable prefix) per seconds, this assumes that each `step` represents a byte. If you want to implement your own widget(s) take a look at :class:`brownie.terminal.progress.Widget`, you can use them by passing them in a dictionary (mapping the name to the widget class) via the `widgets` argument. You might also want to take a look at the source code of the built-in widgets. There are two things you have to look out for: :class:`~brownie.terminal.progress.ProgressBar` objects are not reusable if you need another object, call this method again. If you attempt to write to the :attr:`stream` while using a progress bar the behaviour is undefined. .. seealso:: :ref:`creating-widgets` """ return ProgressBar.from_string( description, self, maxsteps=maxsteps, widgets=None )
def get_usable_width(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([TextWidget('foobar')], writer) Assert(progressbar.get_usable_width()) == writer.get_usable_width() - 6
def progress(self, description, maxsteps=None, widgets=None): """ Returns a :class:`~brownie.terminal.progress.ProgressBar` object which can be used to write the current progress to the stream. The progress bar is created from the `description` which is a string with the following syntax: Widgets -- the parts of the progress bar which are changed with each update -- are represented in the form ``$[a-zA-Z]+``. Some widgets required that you provide an initial value, this can be done by adding ``:string`` where string is either ``[a-zA-Z]+`` or a double-quoted string. If you want to escape a ``$`` you simply precede it with another ``$``, so ``$$foo` will not be treated as a widget and in the progress bar ``$foo`` will be shown. Quotes (``"``) in strings can be escaped with a backslash (``\``). The following widgets are available: `hint` Shows a string of text that can be given using the `hint` argument at any update performed with :meth:`.ProgressBar.init`, :meth:`.ProgressBar.next` or :meth:`.ProgressBar.finish`. If the argument is not given an empty string is used instead. `percentage` Shows the progress in percent; this requires `maxsteps` to be set. `bar` Shows a simple bar which moves which each update not corresponding with the progress being made. This is useful if you just want to show that something is happening. `sizedbar` Shows a simple progress bar which is being filled corresponding to the percentage of progress. This requires `maxsteps` to be set. `step` Shows the current at maximum number of steps as ``step of steps``, this method takes an initial value determining the unit of each step e.g. if each step represents a byte and you choose `bytes` as a unit a reasonable prefix will be chosen. Supported units: - `bytes` - Uses a binary prefix. This requires `maxsteps` to be set. `time` Shows the elapsed time in hours, minutes and seconds. `speed` Shows the speed in bytes (or with a reasonable prefix) per seconds, this assumes that each `step` represents a byte. If you want to implement your own widget(s) take a look at :class:`brownie.terminal.progress.Widget`, you can use them by passing them in a dictionary (mapping the name to the widget class) via the `widgets` argument. You might also want to take a look at the source code of the built-in widgets. There are two things you have to look out for: :class:`~brownie.terminal.progress.ProgressBar` objects are not reusable if you need another object, call this method again. If you attempt to write to the :attr:`stream` while using a progress bar the behaviour is undefined. .. seealso:: :ref:`creating-widgets` """ return ProgressBar.from_string(description, self, maxsteps=maxsteps, widgets=None)
def iter(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer) Assert(iter(progressbar)).is_(progressbar)
def finish(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer, maxsteps=20) widget = StepWidget() progressbar.step = progressbar.maxsteps Assert(widget.finish(progressbar, writer.get_width())) == '20 of 20'
def init(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer) widget = DataTransferSpeedWidget() Assert(widget.init(progressbar, 100)) == '0kb/s'
def finish(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer, maxsteps=100) widget = PercentageWidget() widget.init(progressbar, writer.get_width()) Assert(widget.finish(progressbar, writer.get_width())) == '100%'
def init(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer) widget = TimeWidget() Assert(widget.init(progressbar, 100)) == '00:00:00'
def init(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer) widget = BarWidget() Assert(widget.init(progressbar, 8)) == '[###...]'
def init(self): writer = TerminalWriter(StringIO()) sized_widgets = PercentageWidget, PercentageBarWidget for sized in sized_widgets: with Assert.raises(ValueError): ProgressBar([sized()], writer)
def finish(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer, maxsteps=10) widget = PercentageBarWidget() Assert(widget.finish(progressbar, 12)) == '[##########]'