def parse_item_long_list(provided_list: list, level: int) -> List[Text]: final_lines = [] prepend_hyphen = "- " prepend_spaces = " " for n in range(level): prepend_hyphen = " " + prepend_hyphen prepend_spaces = " " + prepend_spaces prepend_hyphen = Text(prepend_hyphen, Style(bold=True)) provided_list.sort(key=list_sort) for item in provided_list: if type(item) == dict: dict_lines = parse_item_long_dict(item, level) # fix the dictionary so it has a hyphen on the first line for i in range(len(dict_lines)): if i == 0: dict_lines[0] = Text.assemble( prepend_hyphen, dict_lines[0]) else: dict_lines[i] = Text.assemble( prepend_spaces, dict_lines[i]) final_lines.extend(dict_lines) else: final_content = Text.assemble( prepend_spaces, (str(item), Style(bold=False)), style='bold') final_lines.append(final_content) return final_lines
def parse_item_long_dict(provided_dict: dict, level: int) -> List[Text]: final_lines = [] prepend_spaces = "" for n in range(level): prepend_spaces = prepend_spaces + " " for item in provided_dict.items(): if type(item[1]) == dict: final_content = Text.assemble( prepend_spaces, item[0], ": ", style="bold") final_lines.append(final_content) sub_dict_lines = parse_item_long_dict(item[1], level + 1) final_lines.extend(sub_dict_lines) elif type(item[1]) == list: sub_list_lines = parse_item_long_list(item[1], level + 1) final_lines.extend(sub_list_lines) else: # print(type(item[1])) value = Text(str(item[1]), Style(bold=False)) if item[0] == "state": value = color_state(value) final_content = Text.assemble( prepend_spaces, item[0], ": ", value, style="bold") final_lines.append(final_content) return final_lines
def parse_item_short_dict(provided_dict: dict) -> List[Text]: final_lines = [] state = provided_dict.get("state") item_id = provided_dict.get("id") item_type = provided_dict.get("@type") first_key = list(provided_dict.keys())[0] content_key = "" content = "" final_content = "" if item_id is not None: content_key = "id" content = item_id elif item_type is not None: content_key = "@type" content = item_type else: content_key = first_key content = item[first_key] final_content = Text.assemble( "- ", content_key, ": ", (content, Style(bold=False)), style="bold") final_lines.append(final_content) if state is not None: state_content = Text.assemble( " ", "state", ": ", (color_state(state)), style="bold") final_lines.append(state_content) return final_lines
def output_test_failed_location(self, test_result: TestResult): assert_msg = Text("") if isinstance(test_result.error, TestAssertionFailure): if test_result.error.assert_msg: assert_msg = Text.assemble((" - ", "dim"), test_result.error.assert_msg) else: assert_msg = Text("") output_str = ( f"Failed at {os.path.relpath(test_result.test.path, Path.cwd())}:" f"{test_result.error.error_line}" ) else: # This is what we'd expect to see if the test failed for something like # an error during fixture resolution. output_str = ( f"Failed at {os.path.relpath(test_result.test.path, Path.cwd())}" ) output_text = Text.assemble(output_str, assert_msg) self.console.print( Padding( output_text, pad=(1, 0, 0, 2), ) )
def get_pretty_failure_for_in(self, err: TestFailure) -> RenderableType: lhs_msg = Text.assemble( ("The ", "default"), ("item ", "pass.textonly"), *self.of_type(err.lhs), ) lhs = Panel( Pretty(err.lhs), title=lhs_msg, title_align="left", border_style="pass.textonly", padding=1, ) rhs_msg = Text.assemble( ("was not " if err.operator is Comparison.In else "was ", "bold default"), ("found in the ", "default"), ("container ", "fail.textonly"), *self.of_type(err.rhs), ) rhs = Panel( Pretty(err.rhs), title=rhs_msg, title_align="left", border_style="fail.textonly", padding=1, ) return Padding(RenderGroup(lhs, rhs), pad=(0, 0, 1, 2))
def describe_results(validated: gpd.GeoDataFrame, error_column: str, console: Console = Console()): """ Describe validation results to stdout. E.g. >>> gdf = gpd.GeoDataFrame({"VALIDATION_ERRORS": [("V NODE",)]}) >>> describe_results(gdf, "VALIDATION_ERRORS") Out of 1 traces, 1 were invalid. There were 1 error types. These were: V NODE """ error_count = sum( [len(val) != 0 for val in validated[error_column].values]) error_types = { c for c in chain(*validated[error_column].to_list()) if isinstance(c, str) } trace_count = validated.shape[0] count_string = f"Out of {trace_count} traces, {error_count} were invalid." type_string = f"There were {len(error_types)} error types. These were:\n" type_color = "yellow" for error_type in error_types: type_string += error_type + "\n" if SharpCornerValidator.ERROR not in error_type: type_color = "bold red" count_color = "bold red" if error_count / trace_count > 0.05 else "yellow" console.print(Text.assemble((count_string, count_color))) console.print(Text.assemble((type_string, type_color)))
def human_date(dt): if dt == "1990-01-01T00:00:00Z": return None elif dt: diff = datetime.now() - datetime.fromisoformat(dt[:-1]) if diff <= timedelta(days=7): return Text.assemble((humanize.naturaltime(diff), "bold green")) elif diff <= timedelta(days=30): return Text.assemble((humanize.naturaltime(diff), "bold orange3")) else: return Text.assemble((humanize.naturaltime(diff), "bold red"))
def _format_dictionary_to_tree(dict_: dict[str, list[str]], title: str) -> str: """Format missing root nodes.""" tree = Tree(title) for node, tasks in dict_.items(): branch = tree.add(Text.assemble(FILE_ICON, node)) for task in tasks: branch.add(Text.assemble(TASK_ICON, task)) text = render_to_string(tree, console=console, strip_styles=True) return text
def main(): # Printing heading banner f = Figlet(font="banner4") grid = Table.grid(expand=True, padding=1, pad_edge=True) grid.add_column(justify="right", ratio=38) grid.add_column(justify="left", ratio=62) grid.add_row( Text.assemble((f.renderText("PE"), "bold red")), Text(f.renderText("Sidious"), "bold white"), ) print(grid) print( Panel( Text.assemble( ("Creating Chaos with Mutated Evasive Malware with ", "grey"), ("Reinforcement Learning ", "bold red"), ("and "), ("Generative Adversarial Networks", "bold red"), justify="center", ))) # Read arguments and set logging configurations. args = parse_args() logging_setup(str(args.logfile), args.log) info("[bold red][*] Starting Feature Extraction Program ...\n", extra={"markup": True}) info("[*] Setting parameters ...") debug(f"\t[*] Malware Directory - [bold green] {str(args.malware_path)}", extra={"markup": True}) debug(f"\t[*] Benign Directory - [bold green]{str(args.benign_path)}", extra={"markup": True}) debug(f"\t[*] Output Directory - [bold green]{str(args.output_dir)}", extra={"markup": True}) debug(f"\t[*] Logfile - [bold green]{str(args.logfile)}", extra={"markup": True}) debug(f"\t[*] Log Level - [bold green]{str(args.log)}", extra={"markup": True}) info("[+] Parameteres set successfully ... \n") malware_path = str(args.malware_path) benign_path = str(args.benign_path) output_dir = str(args.output_dir) features_mapping_index(malware_path, benign_path, output_dir) pass
def show_toolbox_apps(toolbox: ToolBox) -> Dict[str, list[str]]: apps = toolbox.apps() columns = {"available": [], "unavailable": []} for app, path in apps.items(): if any(map(lambda x: app.lower().startswith(x.lower()), working)): columns["available"].append(app) else: columns["unavailable"].append(app) panel = Text() for i, available in enumerate(columns["available"], start=1): left = (f"{i}. ", "bold gray") right = (f"{available}\n", "green") panel.append(Text.assemble(left, right)) for unavailable in columns["unavailable"]: panel.append(Text(f" {unavailable}\n", style="red")) console.print(Align.center(Panel.fit(panel, title="Available IDEs"))) return columns
def sla_create(name, description, loss, latency, jitter): headers = authentication(vmanage) base_url = "https://" + f'{vmanage["host"]}:{vmanage["port"]}/dataservice' api = "/template/policy/list/sla" url = base_url + api payload = { "name": name, "description": description, "entries": [{ "latency": latency, "loss": loss, "jitter": jitter }] } response = requests.post(url=url, data=json.dumps(payload), headers=headers, verify=False) if response.status_code == 200: items = response.json() text = Text.assemble(("Successful", "bold green"), " create SLA Class with listID = ", (items["listId"], "magenta")) console = Console() console.print(text) else: print(str(response.text)) exit()
def inning(game: Game) -> Text: if not game.game_start: style = "#9a9531" inning = "UPCOMING" else: if game.shame: style = "#800878" inning = "SHAME" elif game.state.holiday_inning: style = "#ff66f9" inning = "PARTY" elif game.game_complete: style = "red" inning = "FINAL" else: style = "green" inning = "LIVE" inning += f" - {game.inning + 1:X}" if not game.game_complete: inning += "▲" if game.top_of_inning else "▼" return Text.assemble( (inning, style), "", )
def sla_edit(name, sla_id, loss, latency, jitter): headers = authentication(vmanage) base_url = "https://" + f'{vmanage["host"]}:{vmanage["port"]}/dataservice' api = f"/template/policy/list/sla/{sla_id}" url = base_url + api payload = { "name": name, "entries": [{ "latency": latency, "loss": loss, "jitter": jitter }] } response = requests.put(url=url, data=json.dumps(payload), headers=headers, verify=False) if response.status_code == 200: text = Text.assemble(("Successful", "bold green"), " edit the SLA class") console = Console() console.print(text) else: print(str(response.text)) exit()
def update_standings(title: str, data: Prediction) -> None: layout["header"].update(Panel(Text(title, justify="center"))) for subleague, rows in data.items(): teams = Table.grid(expand=True) teams.add_column("Flag", width=2) teams.add_column("Name") teams.add_column("Championships", width=3, style="#FFEB57") teams.add_column("Wins", width=4, justify="right") teams.add_column("Record", width=6, justify="right") teams.add_column("Estimate", width=3, justify="right") for row in rows: if not row: teams.add_row() else: teams.add_row( row.badge, Text.assemble((row.name, row.color), f"[{row.tiebreaker}]"), "●" * row.championships if row.championships < 4 else f"●x{row.championships}", f"{'*' if row.in_progress else ''}{row.wins}", row.record, row.estimate, ) layout[subleague].update(Panel( teams, title=subleague, padding=0, ))
def _print_errored_task_report(session: Session, report: ExecutionReport) -> None: """Print the traceback and the exception of an errored report.""" task_name = format_task_id( task=report.task, editor_url_scheme=session.config["editor_url_scheme"], short_name=True, ) text = Text.assemble("Task ", task_name, " failed", style="failed") console.rule(text, style=report.outcome.style) console.print() if report.exc_info and isinstance(report.exc_info[1], Exit): console.print(format_exception_without_traceback(report.exc_info)) else: console.print( render_exc_info(*report.exc_info, session.config["show_locals"])) console.print() show_capture = session.config["show_capture"] for when, key, content in report.sections: if key in ("stdout", "stderr") and show_capture in ( ShowCapture[key.upper()], ShowCapture.ALL, ): console.rule(f"Captured {key} during {when}", style=None) console.print(content)
def report_check_result(result): table = Table(title="Check Result", show_lines=True, width=100, box=box.ROUNDED) table.add_column("Pass/Fail", justify="center") table.add_column("Requirement") table.add_column("Tips") for requirement, fail_reason in result: if not fail_reason: table.add_row("✅", Markdown(requirement, style="green"), "") continue elif type(fail_reason) is AssertionError: tb = traceback.extract_tb(fail_reason.__traceback__, limit=-1) if fail_reason.args: reason_txt = fail_reason.args[0] else: reason_txt = Text.assemble( ("AssertionError", "bold red"), (": We expected\n", "blue"), (tb[0].line.replace("assert ", "") + "\n", "white"), ) elif type(fail_reason) is AttributeError: fail_reason = str(fail_reason).split(" ") reason_txt = Text.assemble( ("AttributeError", "bold red"), (": The ", "blue"), (fail_reason[0], "white"), (" ", ""), (" ".join(fail_reason[1:-1]), "blue"), (" ", ""), (fail_reason[-1], "white"), ) elif issubclass(type(fail_reason), Exception): reason_txt = Text.assemble( (type(fail_reason).__name__, "bold red"), (": " + str(fail_reason), "blue"), ) else: reason_txt = Text(str(fail_reason)) if type(reason_txt) is not Text: reason_txt = Markdown(reason_txt) table.add_row("❌", Markdown(requirement, style="red"), reason_txt) console.print(table)
def update_standings(data: Prediction, sim: SimData) -> None: layout["header"].update( Text(f"Season {sim.season + 1} Day {sim.day + 1}", justify="center")) for subleague, rows in data.items(): teams = [] for row in rows: wang = f"{row.wins - row.nonlosses:+}" closest = min(row.over, row.under) if closest < 0: postseason = "" elif closest > 99: postseason = "🃏" elif closest <= sim.day: postseason = "👑" else: postseason = str(closest) if row.party < 0: party = "" elif row.party > 99: party = "😐" elif row.party <= sim.day: party = "🥳" else: party = str(row.party) teams.append(( row.division.split()[1][0], Text.assemble((row.name, row.color), f"[{row.tiebreaker}]"), clip_championships(row), f"{'*' if row.in_progress else ''}{row.wins}", wang, f"{row.nonlosses}-{row.losses}", party, postseason, )) for i, bracket in enumerate(BRACKETS): table = Table.grid(expand=True) table.add_column("Division", width=1) table.add_column("Name") table.add_column("Championships", width=2) table.add_column("Wins", width=3, justify="right") table.add_column("WANG", width=3, justify="right") table.add_column("Record", width=5, justify="right") table.add_column("Party", width=2, justify="right") table.add_column("Postseason", width=2, justify="right") slice_size = len(teams) // len(BRACKETS) for team in teams[i * slice_size:(i + 1) * slice_size]: table.add_row(*team) layout[bracket][subleague].update( Panel( table, title=subleague, padding=0, ))
def format_invoice(invoice: payment.Invoice): status_style = status_to_color(invoice.status) or "" table = Table( "[yellow]Attribute[/yellow]", "[yellow]Value[/yellow]", header_style="bold yellow", title=invoice.invoice_id, style=status_style, title_style=status_style, ) table.add_row(Text.assemble("issuer", style="bold"), invoice.issuer_id) date_txt = str(invoice.timestamp.date()) time_txt = str(invoice.timestamp.time()) table.add_row("ts", f"{date_txt} [dim]{time_txt}[/dim]") table.add_row("amount", invoice.amount) table.add_row("status", Text.assemble(str(invoice.status), style=status_style)) return table
def test_assemble_meta(): text = Text.assemble("foo", ("bar", "bold"), meta={"foo": "bar"}) assert str(text) == "foobar" assert text._spans == [ Span(3, 6, "bold"), Span(0, 6, Style(meta={"foo": "bar"})) ] console = Console() assert text.get_style_at_offset(console, 0).meta == {"foo": "bar"}
def render(self, task): if task.finished: extra = task.fields.get('status_extra', '') if len(extra) > 0: extra = " (%s)" % extra if task.fields.get('status', 'OK') == 'ERROR': return Text.assemble(Text("[ "), Text("ERROR", style="red"), Text(" ]%s" % extra)) elif task.fields.get('status', 'OK') == 'WARNING': return Text.assemble(Text("[ "), Text("WARNING", style="yellow"), Text(" ]%s" % extra)) else: return Text.assemble(Text("[ "), Text("OK", style="green"), Text(" ]%s" % extra)) else: return Text.assemble(Text("[ "), Text("RUNNING", style="yellow"), Text(" ]"))
def display_corpus(pos, token, corpus, selected=[], highlight_token=None): """ Returns a panel with token as the title and corpus as the body Parameters ---------- pos: Position of token token: Token to display corpus: List[str] of the section of corpus to display trim: Whether to trim the result to show only the section that contains the first occurence of the token selected: List[int] of indices representing the currently selected corpus highlight_token: Position of token to highlight """ _tok = get_first_item(token) content = [] for i, c in enumerate(corpus): c = c.replace(_tok, f'[yellow]{_tok}[/yellow]') if i in selected: c = '[bold magenta]>>[/bold magenta] ' + c elif len(selected) > 0: c = ' ' + c content.append(c) title = [Text(f'{pos}.')] for i, t in enumerate(token): st = Text.styled( t, Style(color='black', bgcolor='white') ) if highlight_token is not None and i == highlight_token else Text(t) title.append(Text.assemble(' | ', st)) title = Text.assemble(*title) return Panel('\n'.join(content), title=title)
def put_banner(): # Printing heading banner f = Figlet(font="banner4") grid = Table.grid(expand=True, padding=1, pad_edge=True) grid.add_column(justify="right", ratio=38) grid.add_column(justify="left", ratio=62) grid.add_row( Text.assemble((f.renderText("PE"), "bold red")), Text(f.renderText("Sidious"), "bold white"), ) print(grid) print( Panel( Text.assemble( ("Creating Chaos with Mutated Evasive Malware with ", "grey"), ("Reinforcement Learning ", "bold red"), ("and "), ("Generative Adversarial Networks", "bold red"), justify="center", )))
def render_video(self, video: Video): formatted = video.get_formatted() title = formatted['title'] title.stylize('bold') stats = formatted['stats'] stats.stylize('blue') up = formatted['up'] up.stylize('magenta') desc = formatted['desc'] desc.truncate(90, overflow='ellipsis') summary = Text.assemble(title, '\n', stats, '\n', up, '\n', desc) self.panels.append(Panel(summary, expand=True))
def instruction_auipc(): text = Text.assemble(("auipc", "bold magenta underline"), (" rd,", "bold red underline"), (" immediate,", "bold blue underline"), justify="left") text.append("\nHELP:", style="bold white") text.append(" x[rd] = pc + sext(immediate[31:12] << 12)", style="bold green") text.append( "\n♦ Add Upper Immediate to PC. U-type, RV32I and RV64I. \n♦ Adds the sign-extended 20-bit immediate, left-shifted by 12 bits, to the pc, and writes the result to x[rd]." ) console.print(text) display_R(one="0010111", two="rd", three="imm[31:12]")
def from_html(text: Union[Text, str], tag: str, **kwargs) -> Text: mark = ProtoStyle() mark.tag = tag mark.xml_attr = kwargs if isinstance(text, Text): t = [ Segment(text.plain[s.start:s.end - s.start], s.style) for s in text.spans ] return Text.assemble(*t, style=mark.convert()) elif isinstance(text, str): spans = [Span(0, len(text), mark.convert())] return Text(text, spans=spans)
def _item_durability(metadata: JSON) -> Text: durability = metadata["itemDurability"] health_before = metadata["itemHealthBefore"] before = "●" * health_before + "○" * (durability - health_before) health_after = metadata["itemHealthAfter"] after = "●" * health_after + "○" * (durability - health_after) after_color = "green" if health_after > health_before else "red" return Text.assemble( str(health_before), (before, ITEM), " -> ", (str(health_after), after_color), (after, ITEM), )
def summary_device(sla_id): headers = authentication(vmanage) base_url = "https://" + f'{vmanage["host"]}:{vmanage["port"]}/dataservice' api = f"/template/policy/list/sla/{sla_id}" url = base_url + api response = requests.delete(url=url, headers=headers, verify=False) if response.status_code == 200: text = Text.assemble(("Successful", "bold green"), " delete SLA Class with listID = ", (sla_id, "magenta")) console = Console() console.print(text) exit() else: print(str(response.text)) exit()
def ansi_fun(code: str, text: Union[Text, str]) -> Text: """ This constructor is used to create a Text from a PennMUSH style ansi() call, such as: ansi(hr,texthere!) """ code = code.strip() mark = ProtoStyle() apply_rules(mark, code) if isinstance(text, Text): t = [ Segment(text.plain[s.start:s.end - s.start], s.style) for s in text.spans ] return Text.assemble(*t) elif isinstance(text, str): spans = [Span(0, len(text), mark.convert())] return Text(text, spans=spans)
def decode(src, errors: str = "strict") -> Text: current = ProtoStyle() state = 0 remaining = src segments: List[Tuple[str, Style]] = list() tag = None while len(remaining): if state == 0: idx_start = remaining.find("\002") if idx_start != -1: segments.append((remaining[:idx_start], current.convert())) remaining = remaining[idx_start + 1:] state = 1 else: segments.append((remaining, current.convert())) remaining = "" elif state == 1: # encountered a TAG START... tag = remaining[0] remaining = remaining[1:] state = 2 elif state == 2: # we are inside a tag. hoover up all data up to TAG_END... idx_end = remaining.find("\003") opening = True if idx_end != -1: tag_data = remaining[:idx_end] remaining = remaining[idx_end + 1:] if tag_data and tag_data[0] == "/": opening = False tag_data = tag_data[1:] if opening: current = ProtoStyle(parent=current) if tag == "p": apply_mxp(current, tag_data) elif tag == "c": current.inherit_ansi() apply_rules(current, tag_data) else: current = current.parent state = 0 else: # malformed data. break return Text.assemble(*segments)
def grid_layout(repos): """ Displays repositories in a grid format using rich """ max_desc_len = 90 panels = [] for repo in repos: stats = get_stats(repo) # '\n' added here as it would group both text and new line together # hence if date_range isn't present the new line will also not be displayed date_range_str = ( repo["date_range"].replace("stars", "тнР") + "\n" if "date_range" in repo.keys() and repo["date_range"] else "" ) if not repo["language"]: # if language is not provided repo["language"] = "None" # make it a string if not repo["description"]: repo["description"] = "None" name = Text(repo["name"], style="bold yellow") language = Text(repo["language"], style="magenta") description = Text(repo["description"], style="green") stats = Text(stats, style="blue") # truncate rest of the description if # it's more than 90 (max_desc_len) chars # using truncate() is better than textwrap # because it also takes care of asian characters description.truncate(max_desc_len, overflow="ellipsis") repo_summary = Text.assemble( name, "\n", stats, "\n", date_range_str, language, "\n", description, ) panels.append(Panel(repo_summary, expand=True)) console = Console() console.print((Columns(panels, width=30, expand=True)))