Exemplo n.º 1
0
 def formatMessage(self, record):
     if not hasattr(record, "levelcolor"):
         record.levelcolor = get_level_color(record.levelno)
     msg = super().formatMessage(record)
     if IS_A_TTY:
         msg = '\r' + msg + CLEAR_EOL
     return colorize(msg) if LOG_WITH_COLOR else uncolored(msg)
Exemplo n.º 2
0
def test_empty_markup():
    from easypy.colors import Colorized, colorize

    opts = {
        colorize("BLUE<<>>"),
        colorize("BLUE@[]@"),
        colorize("BLUE@{}@"),
        str(Colorized("BLUE<<>>")),
        str(Colorized("BLUE@[]@")),
        str(Colorized("BLUE@{}@")),
    }

    assert len(opts) == 1
    [ret] = opts

    assert ret == ""
Exemplo n.º 3
0
 def generate():
     for key in actual.keys() | expected.keys():
         actual_count = actual.get(key, 0)
         expected_count = expected.get(key, 0)
         if actual_count == 0 and expected_count == 0:
             continue
         actual_percentage = 100 * actual_count / actual_total
         expected_percentage = 100 * expected_count / expected_total
         diff = actual_percentage - expected_percentage
         abs_diff = abs(diff)
         color = color_for(abs_diff)
         if color:
             yield Bunch(key=colorize(key, color),
                         actual=colorize('%.1f%%' % actual_percentage, color),
                         expected=colorize('%.1f%%' % expected_percentage, color),
                         diff=colorize('%.1f%%' % diff, color),
                         abs_diff=abs_diff)
Exemplo n.º 4
0
def test_colors(content):
    opts = {
        str(Colorized("RED(BLUE)<<%s>>" % content)),
        str(Colorized("RED(BLUE)@[%s]@" % content)),
        str(Colorized("RED(BLUE)@{%s}@" % content)),

        str(colorize("RED(BLUE)<<%s>>" % content)),
        str(colorize("RED(BLUE)@[%s]@" % content)),
        str(colorize("RED(BLUE)@{%s}@" % content)),

        str(colorize("BAD<<%s>>" % content)),
        str(colorize("BAD@[%s]@" % content)),
        str(colorize("BAD@{%s}@" % content)),
    }

    assert len(opts) == 1
    [ret] = opts
    assert ret == ("\x1b[1;44;31m%s\x1b[0m" % content if content else '')
    assert uncolored(ret) == content
Exemplo n.º 5
0
def test_colors():
    from easypy.colors import Colorized, uncolored, colorize, register_colorizers

    register_colorizers(bad=("red", "blue"))

    opts = {
        str(Colorized("RED(BLUE)<<XXX>>")),
        str(Colorized("RED(BLUE)@[XXX]@")),
        str(Colorized("RED(BLUE)@{XXX}@")),
        str(colorize("RED(BLUE)<<XXX>>")),
        str(colorize("RED(BLUE)@[XXX]@")),
        str(colorize("RED(BLUE)@{XXX}@")),
        str(colorize("BAD<<XXX>>")),
        str(colorize("BAD@[XXX]@")),
        str(colorize("BAD@{XXX}@")),
    }

    assert len(opts) == 1
    [ret] = opts
    assert ret == "\x1b[1;44;31mXXX\x1b[0m"
    assert uncolored(ret) == "XXX"
Exemplo n.º 6
0
def test_uncolored(content):
    uncolored(str(Colorized("RED(BLUE)<<%s>>" % content)))
    uncolored(str(Colorized("RED(BLUE)@[%s]@" % content)))
    uncolored(str(Colorized("RED(BLUE)@{%s}@" % content)))

    uncolored(str(colorize("RED(BLUE)<<%s>>" % content)))
    uncolored(str(colorize("RED(BLUE)@[%s]@" % content)))
    uncolored(str(colorize("RED(BLUE)@{%s}@" % content)))

    uncolored(str(colorize("BAD<<%s>>" % content)))
    uncolored(str(colorize("BAD@[%s]@" % content)))
    uncolored(str(colorize("BAD@{%s}@" % content)))

    uncolored("RED(BLUE)<<%s>>" % content)
    uncolored("RED(BLUE)@[%s]@" % content)
    uncolored("RED(BLUE)@{%s}@" % content)

    uncolored("RED(BLUE)<<%s>>" % content)
    uncolored("RED(BLUE)@[%s]@" % content)
    uncolored("RED(BLUE)@{%s}@" % content)

    uncolored("BAD<<%s>>" % content)
    uncolored("BAD@[%s]@" % content)
    uncolored("BAD@{%s}@" % content)
Exemplo n.º 7
0
 def render(self, *, width=80, color=True, **kw):
     buff = self._get_buffer(color=color, **kw)
     text = buff.render(width=width, edges=not color)
     return colorize("\n" + text)
Exemplo n.º 8
0
    def render(self):
        rendered = defaultlist(list)
        columns = []

        def _get_value(data, value):
            ret = data.get(value)
            if ret is None:
                ret = ''
            return ret

        for column in self.columns:
            if not column.visible:
                continue
            rows = [_get_value(data, column.name) for data in self.data]
            if not any(filter(lambda i: i != '',
                              rows)) and column.drop_if_empty:
                continue
            columns.append(column)

            if column.max_width is None:
                column.max_width = self.max_col_width
            if column.align is None:
                column.align = self.align
            if column.header_align is None:
                column.header_align = self.header_align
            if column.padding is None:
                column.padding = self.padding

            raw_data = [column.title] + rows
            colored_data = [colorize(str(data)) for data in raw_data]
            uncolored_data = [uncolored(data) for data in colored_data]
            max_width = column.max_width or max(
                len(data) for data in uncolored_data)
            for i, data in enumerate(colored_data):
                align = column.header_align if i == 0 else column.align
                coloring_spacing = len(colored_data[i]) - len(
                    uncolored_data[i])
                spacing = max_width + coloring_spacing
                format_string = "{{data:{align}{spacing}}}".format(
                    align=self._ALIGN_MAP[align], spacing=spacing)
                rendered[i].append(format_string.format(data=data))

        output = StringIO()
        for r_i, row in enumerate(rendered):
            r_parts = []

            sep = self.HEADER_SEP if r_i == 0 else self.SEPARATORS[r_i % len(
                self.SEPARATORS)]

            for col_i, col in enumerate(row):
                column = columns[col_i]
                padding = column.padding * " "
                if column.max_width and r_i > 0:
                    col = compact(col,
                                  column.max_width,
                                  suffix_length=column.max_width // 10)
                r_parts.append("{padding}{col}{padding}".format(
                    col=col, padding=padding))

            output.write(sep.join(r_parts))
            output.write("\n")

            if r_i == 0:
                r_parts = [self.BAR * len(uncolored(part)) for part in r_parts]
                output.write(self.BAR_SEP.join(r_parts))
                output.write("\n")

        output.seek(0)
        return output.read()