def get_success_text(success): success = str(success) styles = { "True": ("#73D216", str(Emoji("heavy_check_mark"))), "False": ("#CC0000", str(Emoji("cross_mark"))), } color, emoji = styles[success] return Text(emoji, style=color)
def get_action_status_text(success, action): success = str(success) actions = {"kill": "killed", "delete": "deleted"} styles = { "True": ("#73D216", f"{actions[action]} " + str(Emoji("heavy_check_mark"))), "False": ("#CC0000", f"failed to {action} " + str(Emoji("cross_mark"))), } color, emoji = styles[success] return Text(emoji, style=color)
def get_status_text(status: str, as_text=True): styles = { "pending": ("#CE5C00", str(Emoji("gear"))), "running": ("#CE5C00", str(Emoji("gear"))), "reported_without_fails": ("#73D216", str(Emoji("heavy_check_mark"))), "reported_with_fails": ("#CC0000", str(Emoji("warning"))), "failed": ("#CC0000", str(Emoji("cross_mark"))), } color, emoji = styles[status] s = f"[{color}]{status} {emoji}[/]" return Text(status + " " + emoji, style=color) if as_text else s
def emoji_text(*lines: str, emoji: str = None, font: Fonts = Fonts.courier_new, size: int = 13, flip: bool = False): print('before', emoji) emoji = emoji or random_emoji() print('after', emoji) if emoji.isascii(): print('i am ascii') emoji = str(Emoji(emoji.strip(':'))) font = load_font(font, size) res = [] for line in lines: bits = font.render_text(line).bits bits = _with_border(bits) res.append('\n'.join(_convert_bits(bits, emoji, flip))) print(_emoji_name_map[emoji]) return res
def _make_rich_rext(*, text: str, style: str = "", markup_mode: MarkupMode) -> Union[Markdown, Text]: """Take a string, remove indentations, and return styled text. By default, return the text as a Rich Text with the request style. If `rich_markdown_enable` is `True`, also parse the text for Rich markup strings. If `rich_markup_enable` is `True`, parse as Markdown. Only one of `rich_markdown_enable` or `rich_markup_enable` can be True. If both are True, `rich_markdown_enable` takes precedence. """ # Remove indentations from input text text = inspect.cleandoc(text) if markup_mode == MARKUP_MODE_MARKDOWN: text = Emoji.replace(text) return Markdown(text, style=style) if markup_mode == MARKUP_MODE_RICH: return highlighter(Text.from_markup(text, style=style)) else: return highlighter(Text(text, style=style))
def test_emoji(): from rich.emoji import Emoji log.debug(":thumbs_up:") assert Emoji.replace(":thumbs_up:") in handler.console.file.getvalue()
def test_no_emoji(): with pytest.raises(NoEmoji): Emoji("ambivalent_bunny")
def test_replace(): assert Emoji.replace("my code is :pile_of_poo:") == "my code is 💩"
def test_str_repr(): assert str(Emoji("pile_of_poo")) == "💩" assert repr(Emoji("pile_of_poo")) == "<emoji 'pile_of_poo'>"
def main(): p = argparse.ArgumentParser() p.add_argument("--report", type=Path, required=True) p.add_argument("--config", type=Path, required=True) p.add_argument("--strip-prefix-path", type=Path) args = p.parse_args() console = Console() with args.config.open() as fh: config = yaml.safe_load(fh) data = [] with args.report.open() as fh: data = ItemCollection(__root__=json.load(fh)).__root__ for item in data: if args.strip_prefix_path and not item.path.is_absolute: item.path = item.path.relative_to(args.strip_prefix_path) counts = config["limits"].copy() kf = lambda i: i.path for file, items in itertools.groupby(sorted(data, key=kf), key=kf): output = [] for item in items: if item.code in config["ignore"]: continue emoji = Emoji({ "warning": "yellow_circle", "error": "red_circle" }[item.severity]) style = "bold " if item.severity == "warning": style += "yellow" elif item.severity == "error": style += "red" s = Text() s.append(f"{emoji}") s.append(f" {item.path}:{item.line}:{item.col}", style="bold") s.append(f" {item.severity.upper()} ", style=style) s.append("[") s.append(item.code, style="bold") s.append(f"]") output.append(s) def subpath(m): return f"[bold]{m.group(1)}[/bold]:" message = re.sub(r"([\w/.\-+]+:\d+:\d+):", subpath, item.message) output.append(Panel(message)) output.append(Rule()) for pattern in counts.keys(): if not fnmatch.fnmatch(item.code, pattern): continue counts[pattern] += 1 output = output[:-1] console.print(Panel(Group(*output), title=str(file))) table = Table() table.add_column("", width=2) table.add_column("code / pattern") table.add_column("count", justify="right") table.add_column("limit", justify="right") exit = 0 for pattern, count in counts.items(): limit = config["limits"][pattern] emoji = Emoji("green_circle") style = "green" if count > limit: exit = 1 emoji = Emoji("red_circle") style = "red bold" table.add_row(emoji, pattern, str(count), str(limit), style=style) console.rule() console.print(Panel.fit(table, title="Results"), justify="center") if exit != 0: console.print( Panel( Text(f"{Emoji('red_circle')} FAILURE", justify="center"), style="red bold", )) else: console.print( Panel( Text(f"{Emoji('green_circle')} SUCCESS", justify="center"), style="green bold", )) sys.exit(exit)
def test_render(): render_result = render(Emoji("pile_of_poo")) assert render_result == "💩"
def test_variant_non_default(): render_result = render(Emoji("warning", variant="emoji")) assert render_result == "âš " + "\uFE0F"
def test_variant(): print(repr(Emoji.replace(":warning:"))) assert Emoji.replace(":warning:") == "âš " assert Emoji.replace(":warning-text:") == "âš " + "\uFE0E" assert Emoji.replace(":warning-emoji:") == "âš " + "\uFE0F" assert Emoji.replace(":warning-foo:") == ":warning-foo:"