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 should_escape(self): Assert(self.writer.should_escape(None)) == True Assert(self.writer.should_escape(True)) == True Assert(self.writer.should_escape(False)) == False writer = TerminalWriter(self.stream, autoescape=False) Assert(writer.should_escape(None)) == False Assert(writer.should_escape(True)) == True Assert(writer.should_escape(False)) == False
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 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 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 get_dimensions(self): with Assert.raises(NotImplementedError): self.writer.get_dimensions() writer = TerminalWriter(sys.__stdout__) with Assert.not_raising(NotImplementedError): dimensions = writer.get_dimensions() height, width = dimensions Assert.isinstance(height, int) Assert.isinstance(width, int) Assert(height) == dimensions.height Assert(width) == dimensions.width
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 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''
class BrownieConsoleLogOutputter(ConsoleLogOutputter): level_colors = { "DEBUG": "green", "INFO": "black", "WARNING": "yellow", "CRITICAL": "purple", "ERROR": "red", "EXCEPTION": "red", } def __init__(self): self.terminal_writer = TerminalWriter(stream=sys.stdout) def write(self, out, level): color = self.level_colors.get(level, "black") self.terminal_writer.writeline(out, text_colour=color)
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 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_width(self): with Assert.not_raising(Exception): self.writer.get_width() writer = TerminalWriter(sys.__stdout__) Assert(writer.get_width()) == writer.get_dimensions()[1] writer = TerminalWriter(StringIO()) os.environ['COLUMNS'] = '50' Assert(writer.get_width()) == 50 del os.environ['COLUMNS'] Assert(writer.get_width()) == TerminalWriter.default_width default_width = TerminalWriter.default_width Assert(writer.get_width(default_width + 1)) == default_width + 1
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 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 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 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 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 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 contextmanager_behaviour(self): class MyProgressBar(ProgressBar): init_called = False finish_called = False def init(self): self.init_called = True def finish(self): self.finish_called = True writer = TerminalWriter(StringIO()) progressbar = MyProgressBar([], writer) with progressbar as foo: pass Assert(foo).is_(progressbar) assert progressbar.init_called assert progressbar.finish_called
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 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 get_usable_width(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([TextWidget('foobar')], writer) Assert(progressbar.get_usable_width()) == writer.get_usable_width() - 6
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)
# coding: utf-8 """ brownie.terminal ~~~~~~~~~~~~~~~~ :copyright: 2010-2011 by Daniel Neuhäuser :license: BSD, see LICENSE.rst for details """ import sys from brownie.terminal import TerminalWriter, _colour_names, ATTRIBUTES writer = TerminalWriter(sys.stdout) for name in _colour_names: with writer.line(): writer.write(name, text_colour=name) with writer.line(): writer.write(name, background_colour=name) for name in ATTRIBUTES: if name == 'reset': continue writer.writeline(name, **{name: True}) with writer.line(): with writer.options(underline=True): writer.write('underline') with writer.options(background_colour='red'): writer.write('background') writer.write('text', text_colour='green')
def __init__(self): self.terminal_writer = TerminalWriter(stream=sys.stdout)
def iter(self): writer = TerminalWriter(StringIO()) progressbar = ProgressBar([], writer) Assert(iter(progressbar)).is_(progressbar)
def init(self): writer = TerminalWriter(StringIO()) sized_widgets = PercentageWidget, PercentageBarWidget for sized in sized_widgets: with Assert.raises(ValueError): ProgressBar([sized()], writer)
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 = DataTransferSpeedWidget() Assert(widget.init(progressbar, 100)) == '0kb/s'
def get_usable_width(self): writer = TerminalWriter(StringIO(), prefix='spam') Assert(writer.get_usable_width()) == writer.get_width() - len('spam') writer.indent() Assert(writer.get_usable_width()) == writer.get_width() - 8