Exemple #1
0
def report_raw_stats(
    sect: Section,
    stats: LinterStats,
    old_stats: LinterStats | None,
) -> None:
    """Calculate percentage of code / doc / comment / empty."""
    total_lines = stats.code_type_count["total"]
    sect.insert(0,
                Paragraph([Text(f"{total_lines} lines have been analyzed\n")]))
    lines = ["type", "number", "%", "previous", "difference"]
    for node_type in ("code", "docstring", "comment", "empty"):
        node_type = cast(Literal["code", "docstring", "comment", "empty"],
                         node_type)
        total = stats.code_type_count[node_type]
        percent = float(total * 100) / total_lines if total_lines else None
        old = old_stats.code_type_count[node_type] if old_stats else None
        diff_str = diff_string(old, total) if old else None
        lines += [
            node_type,
            str(total),
            f"{percent:.2f}" if percent is not None else "NC",
            str(old) if old else "NC",
            diff_str if diff_str else "NC",
        ]
    sect.append(Table(children=lines, cols=5, rheaders=1))
Exemple #2
0
def table_lines_from_stats(stats, old_stats, columns):
    """get values listed in <columns> from <stats> and <old_stats>,
    and return a formated list of values, designed to be given to a
    ureport.Table object
    """
    lines = []
    for m_type in columns:
        new = stats[m_type]
        old = old_stats.get(m_type)
        if old is not None:
            diff_str = diff_string(old, new)
        else:
            old, diff_str = "NC", "NC"
        new = "%.3f" % new if isinstance(new, float) else str(new)
        old = "%.3f" % old if isinstance(old, float) else str(old)
        lines += (m_type.replace("_", " "), new, old, diff_str)
    return lines
Exemple #3
0
def report_raw_stats(sect, stats, old_stats):
    """calculate percentage of code / doc / comment / empty"""
    total_lines = stats["total_lines"]
    if not total_lines:
        raise EmptyReportError()
    sect.description = "%s lines have been analyzed" % total_lines
    lines = ("type", "number", "%", "previous", "difference")
    for node_type in ("code", "docstring", "comment", "empty"):
        key = node_type + "_lines"
        total = stats[key]
        percent = float(total * 100) / total_lines
        old = old_stats.get(key, None)
        if old is not None:
            diff_str = diff_string(old, total)
        else:
            old, diff_str = "NC", "NC"
        lines += (node_type, str(total), "%.2f" % percent, str(old), diff_str)
    sect.append(Table(children=lines, cols=5, rheaders=1))
Exemple #4
0
def report_by_type_stats(
    sect: reporter_nodes.Section,
    stats: LinterStats,
    old_stats: LinterStats | None,
) -> None:
    """Make a report of.

    * percentage of different types documented
    * percentage of different types with a bad name
    """
    # percentage of different types documented and/or with a bad name
    nice_stats: dict[str, dict[str, str]] = {}
    for node_type in ("module", "class", "method", "function"):
        node_type = cast(Literal["function", "class", "method", "module"],
                         node_type)
        total = stats.get_node_count(node_type)
        nice_stats[node_type] = {}
        if total != 0:
            undocumented_node = stats.get_undocumented(node_type)
            documented = total - undocumented_node
            percent = (documented * 100.0) / total
            nice_stats[node_type]["percent_documented"] = f"{percent:.2f}"
            badname_node = stats.get_bad_names(node_type)
            percent = (badname_node * 100.0) / total
            nice_stats[node_type]["percent_badname"] = f"{percent:.2f}"
    lines = [
        "type", "number", "old number", "difference", "%documented", "%badname"
    ]
    for node_type in ("module", "class", "method", "function"):
        node_type = cast(Literal["function", "class", "method", "module"],
                         node_type)
        new = stats.get_node_count(node_type)
        old = old_stats.get_node_count(node_type) if old_stats else None
        diff_str = lint_utils.diff_string(old, new) if old else None
        lines += [
            node_type,
            str(new),
            str(old) if old else "NC",
            diff_str if diff_str else "NC",
            nice_stats[node_type].get("percent_documented", "0"),
            nice_stats[node_type].get("percent_badname", "0"),
        ]
    sect.append(reporter_nodes.Table(children=lines, cols=6, rheaders=1))
Exemple #5
0
def table_lines_from_stats(
    stats: CheckerStats,
    old_stats: CheckerStats,
    columns: Iterable[str],
) -> List[str]:
    """get values listed in <columns> from <stats> and <old_stats>,
    and return a formatted list of values, designed to be given to a
    ureport.Table object
    """
    lines: List[str] = []
    for m_type in columns:
        new: Union[int, str] = stats[m_type]  # type: ignore
        old: Union[int, str, None] = old_stats.get(m_type)  # type: ignore
        if old is not None:
            diff_str = diff_string(old, new)
        else:
            old, diff_str = "NC", "NC"
        new = f"{new:.3f}" if isinstance(new, float) else str(new)
        old = f"{old:.3f}" if isinstance(old, float) else str(old)
        lines.extend((m_type.replace("_", " "), new, old, diff_str))
    return lines
Exemple #6
0
def report_raw_stats(
    sect,
    stats: CheckerStats,
    old_stats: CheckerStats,
):
    """calculate percentage of code / doc / comment / empty"""
    total_lines: int = stats["total_lines"]  # type: ignore
    if not total_lines:
        raise EmptyReportError()
    sect.description = f"{total_lines} lines have been analyzed"
    lines = ["type", "number", "%", "previous", "difference"]
    for node_type in ("code", "docstring", "comment", "empty"):
        key = node_type + "_lines"
        total: int = stats[key]  # type: ignore
        percent = float(total * 100) / total_lines
        old: Optional[Union[int, str]] = old_stats.get(key,
                                                       None)  # type: ignore
        if old is not None:
            diff_str = diff_string(old, total)
        else:
            old, diff_str = "NC", "NC"
        lines += [node_type, str(total), f"{percent:.2f}", str(old), diff_str]
    sect.append(Table(children=lines, cols=5, rheaders=1))
Exemple #7
0
def table_lines_from_stats(
    stats: LinterStats,
    old_stats: Optional[LinterStats],
    stat_type: Literal["duplicated_lines", "message_types"],
) -> List[str]:
    """get values listed in <columns> from <stats> and <old_stats>,
    and return a formatted list of values, designed to be given to a
    ureport.Table object
    """
    lines: List[str] = []
    if stat_type == "duplicated_lines":
        new: List[Tuple[str, Union[str, int, float]]] = [
            ("nb_duplicated_lines", stats.duplicated_lines["nb_duplicated_lines"]),
            (
                "percent_duplicated_lines",
                stats.duplicated_lines["percent_duplicated_lines"],
            ),
        ]
        if old_stats:
            old: List[Tuple[str, Union[str, int, float]]] = [
                (
                    "nb_duplicated_lines",
                    old_stats.duplicated_lines["nb_duplicated_lines"],
                ),
                (
                    "percent_duplicated_lines",
                    old_stats.duplicated_lines["percent_duplicated_lines"],
                ),
            ]
        else:
            old = [("nb_duplicated_lines", "NC"), ("percent_duplicated_lines", "NC")]
    elif stat_type == "message_types":
        new = [
            ("convention", stats.convention),
            ("refactor", stats.refactor),
            ("warning", stats.warning),
            ("error", stats.error),
        ]
        if old_stats:
            old = [
                ("convention", old_stats.convention),
                ("refactor", old_stats.refactor),
                ("warning", old_stats.warning),
                ("error", old_stats.error),
            ]
        else:
            old = [
                ("convention", "NC"),
                ("refactor", "NC"),
                ("warning", "NC"),
                ("error", "NC"),
            ]

    for index, _ in enumerate(new):
        new_value = new[index][1]
        old_value = old[index][1]
        diff_str = (
            diff_string(old_value, new_value)
            if isinstance(old_value, float)
            else old_value
        )
        new_str = f"{new_value:.3f}" if isinstance(new_value, float) else str(new_value)
        old_str = f"{old_value:.3f}" if isinstance(old_value, float) else str(old_value)
        lines.extend((new[index][0].replace("_", " "), new_str, old_str, diff_str))
    return lines