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.
Source · 5 lines
def docstring_harvester() -> Harvester:
"""@brief Build this stage's harvester."""
return _DocstringHarvester()
Activity · 1 steps
Requirements
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.
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
Requirements
harvest
Any clew.pydocs._DocstringHarvester.harvest(self, Any tree, bytes src_bytes)
Harvest one file's definition docstrings.
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
Requirements
harvest_docstrings
list[list[Any]] clew.pydocs.harvest_docstrings(Any tree)
Every function and class docstring in a Python tree.
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
Requirements
summarize
tuple[str, str] clew.pydocs.summarize(str body)
The PEP 257 summary and the.
Called by enrich_python_docstrings
Tested by
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]