Exemple #1
0
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)
Exemple #2
0
def test_styled():
    text = Text.styled("foo", "bold red")
    assert text.style == ""
    assert str(text) == "foo"
    assert text._spans == [Span(0, 3, "bold red")]
Exemple #3
0
def output_console(
    all_cve_data: Dict[ProductInfo, CVEData], console=Console(theme=cve_theme)
):
    """ Output list of CVEs in a tabular format with color support """

    now = datetime.now().strftime("%Y-%m-%d  %H:%M:%S")

    console.print(
        Markdown(
            textwrap.dedent(
                f"""
                # CVE BINARY TOOL
                - cve-bin-tool Report Generated: {now}
                """
            )
        )
    )

    remarks_colors = {
        Remarks.Mitigated: "green",
        Remarks.Confirmed: "red",
        Remarks.NewFound: "blue",
        Remarks.Unexplored: "yellow",
        Remarks.Ignored: "white",
    }

    cve_by_remarks: DefaultDict[Remarks, List[Dict[str, str]]] = defaultdict(list)
    # group cve_data by its remarks
    for product_info, cve_data in all_cve_data.items():
        for cve in cve_data["cves"]:
            cve_by_remarks[cve.remarks].append(
                {
                    "vendor": product_info.vendor,
                    "product": product_info.product,
                    "version": product_info.version,
                    "cve_number": cve.cve_number,
                    "severity": cve.severity,
                }
            )

    for remarks in sorted(cve_by_remarks):
        color = remarks_colors[remarks]
        console.print(Panel(f"[{color}] {remarks.name} CVEs [/{color}]", expand=False))
        # table instance
        table = Table()

        # Add Head Columns to the Table
        table.add_column("Vendor")
        table.add_column("Product")
        table.add_column("Version")
        table.add_column("CVE Number")
        table.add_column("Severity")

        for cve_data in cve_by_remarks[remarks]:
            color = cve_data["severity"].lower()
            table.add_row(
                Text.styled(cve_data["vendor"], color),
                Text.styled(cve_data["product"], color),
                Text.styled(cve_data["version"], color),
                linkify_cve(Text.styled(cve_data["cve_number"], color)),
                Text.styled(cve_data["severity"], color),
            )
        # Print the table to the console
        console.print(table)
Exemple #4
0
 def get_level_text(self, record):
     return RichText.styled(
         self.level_names[record.levelname],
         f"logging.level.{record.levelname.lower()}",
     )