def render(self, params=True, context=True, traceback=True, timestamp=True, color=True): text = "" if self.message: text += "".join("WHITE<<%s>>\n" % line for line in self.message.splitlines()) if params and self._params: tip = self._params.pop('tip', None) text += indent("".join(make_block(self._params)), " " * 4) if tip: tip = tip.format(**self._params) lines = tip.splitlines() text += indent("GREEN(BLUE)@{tip = %s}@\n" % lines[0], " " * 4) for line in lines[1:]: text += indent("GREEN(BLUE)@{ %s}@\n" % lines[0], " " * 4) self._params['tip'] = tip # put it back in params, even though it might've been on the class if timestamp and self.timestamp: ts = datetime.fromtimestamp(self.timestamp).isoformat() text += indent("MAGENTA<<timestamp = %s>>\n" % ts, " " * 4) if context and self.context: text += "Context:\n" + indent("".join(make_block(self.context, skip={"indentation"})), " " * 4) if traceback and self.traceback: fmt = "DARK_GRAY@{{{}}}@" text += "\n".join(map(fmt.format, self.traceback.splitlines())) if not color: from easypy.colors import uncolored text = uncolored(text) return text
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)
def progress(self, record): if not self._thread: return if record.levelno >= logging.DEBUG: record.drawing = "__PB__" + record.drawing[2:] self._last_line = compact( uncolored(self._format(record).split("\n")[0]).strip()[8:]) self._event.set()
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
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"
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)
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()
def get(): return uncolored(stream.getvalue())