pydocs.py

clew/pydocs.py

PEP 257 docstrings, where doxygen left a Python definition undocumented.

docstring_harvester

Harvester clew.pydocs.docstring_harvester()

The docstring stage's harvester.

clew/pydocs.py:71

Source · 5 lines
def docstring_harvester() -> Harvester:
    """@brief Build this stage's harvester."""
    return _DocstringHarvester()

Activity · 1 steps
lines 73–73return _DocstringHarvester()

Requirements

REQ-DDB-PIPE-013

enrich_python_docstrings

int clew.pydocs.enrich_python_docstrings(Path db_path, Path repo_root, Any cache=None)

Fill empty doxygen briefs (and missing versions) from PEP 257 docstrings.

clew/pydocs.py:133

Show on the map →

Called by _build_stages

Calls _enrich_one, _has_provenance, _locate_rows, _table_for, docstring_harvester, run_harvest, summarize

Source · 27 lines
def enrich_python_docstrings(db_path: Path, repo_root: Path, cache: Any = None) -> int:
    """@brief Write docstring summaries into the rows doxygen left bare."""
    conn = sqlite3.connect(str(db_path))
    try:
        harvested = run_harvest(conn, repo_root, docstring_harvester(), cache)
        provenance = _has_provenance(conn)
        located = _locate_rows(conn)
        enriched = 0
        for path_rowid, payload in harvested:
            for kind, line, name, body in payload or ():
                candidates = located.get((_table_for(kind), path_rowid, line))
                if not candidates:
                    continue
                summary, version = summarize(body)
                if not summary and not version:
                    continue
                enriched += _enrich_one(
                    conn, kind, path_rowid, line, name, summary, version, provenance, candidates
                )
        conn.commit()
    finally:
        conn.close()
    if enriched:
        logger.info("pep 257: %d Python definition(s) documented from their docstrings", enriched)
    return enriched

Activity · 19 steps
lines 135–135conn = sqlite3.connect(str(db_path))lines 136–136trylines 137–137harvested = run_harvest(conn, repo_root, docstring_harvester(), cache)lines 138–138provenance = _has_provenance(conn)lines 139–139located = _locate_rows(conn)lines 140–140enriched = 0lines 141–141◇ for path_rowid, payload in harvestedlines 142–142◇ for kind, line, name, body in payload or ()lines 143–143candidates = located.get((_table_for(kind), path_rowid, line))lines 144–144◇ if not candidateslines 145–145continuelines 146–146summary, version = summarize(body)lines 147–147◇ if not summary and not versionlines 148–148continuelines 149–151enriched += _enrich_one( conn, kind, path_rowid, line, name, summary, version, provenance, candidates )lines 152–152conn.commit()lines 155–155◇ if enrichedlines 156–156logger.info("pep 257: %d Python definition(s) documented from their docstrings", enriched)lines 157–157return enriched

Requirements

REQ-DDB-PIPE-013

harvest

Any clew.pydocs._DocstringHarvester.harvest(self, Any tree, bytes src_bytes)

Harvest one file's definition docstrings.

clew/pydocs.py:61

Source · 6 lines
    def harvest(self, tree: Any, src_bytes: bytes) -> Any:
        from .pyast import is_python_tree

        return harvest_docstrings(tree) if is_python_tree(tree) else []

Activity · 2 steps
lines 62–62from .pyast import is_python_treelines 64–64return harvest_docstrings(tree) if is_python_tree(tree) else []

Requirements

REQ-DDB-PIPE-013

harvest_docstrings

list[list[Any]] clew.pydocs.harvest_docstrings(Any tree)

Every function and class docstring in a Python tree.

clew/pydocs.py:81

Show on the map →

Called by harvest

Tested by

1 teststest_harvest_docstrings_lines_are_the_def_lines

Calls _find_python_docstring_node, _strip_docstring_quotes

Source · 27 lines
def harvest_docstrings(tree: Any) -> list[list[Any]]:
    """The line is the `def` / `class` line, not a decorator's, which is the line doxygen
    records, so the two join on (file, line, name).

    @brief Collect definition docstrings.
    @return The docstrings found.
    """
    from .guard.ts_parser import _find_python_docstring_node, _strip_docstring_quotes

    out: list[list[Any]] = []
    stack = [tree.root_node]
    while stack:
        node = stack.pop()
        stack.extend(node.children)
        if node.type not in _DEFINITIONS:
            continue
        name = node.child_by_field_name("name")
        string = _find_python_docstring_node(node)
        if name is None or string is None or not string.text:
            continue
        body = _strip_docstring_quotes(string.text.decode("utf-8", errors="replace"))
        if body.strip():
            kind = "class" if node.type == "class_definition" else "function"
            out.append([kind, node.start_point[0] + 1, name.text.decode("utf-8"), body])
    return out

Activity · 17 steps
lines 88–88from .guard.ts_parser import _find_python_docstring_node, _strip_docstring_quoteslines 90–90out: list[list[Any]] = []lines 91–91stack = [tree.root_node]lines 92–92◇ while stacklines 93–93node = stack.pop()lines 94–94stack.extend(node.children)lines 95–95◇ if node.type not in _DEFINITIONSlines 96–96continuelines 97–97name = node.child_by_field_name("name")lines 98–98string = _find_python_docstring_node(node)lines 99–99◇ if name is None or string is None or not string.textlines 100–100continuelines 101–101body = _strip_docstring_quotes(string.text.decode("utf-8", errors="replace"))lines 102–102◇ if body.strip()lines 103–103kind = "class" if node.type == "class_definition" else "function"lines 104–104out.append([kind, node.start_point[0] + 1, name.text.decode("utf-8"), body])lines 105–105return out

Requirements

REQ-DDB-PIPE-013

summarize

tuple[str, str] clew.pydocs.summarize(str body)

The PEP 257 summary and the.

clew/pydocs.py:113

Source · 13 lines
def summarize(body: str) -> tuple[str, str]:
    """An explicit `@brief` wins over the summary line, as in the gate.

    @brief Read a docstring's brief and version.
    @return The pair.
    """
    from .guard.parser import apply_autobrief, parse_doxygen_tags

    tags = parse_doxygen_tags(body)
    apply_autobrief(body, tags)
    return tags.get("brief", [""])[0], tags.get("version", [""])[0]

Activity · 4 steps
lines 119–119from .guard.parser import apply_autobrief, parse_doxygen_tagslines 121–121tags = parse_doxygen_tags(body)lines 122–122apply_autobrief(body, tags)lines 123–123return tags.get("brief", [""])[0], tags.get("version", [""])[0]

Requirements

REQ-DDB-PIPE-013