|
| 1 | +# --- |
| 2 | +# jupyter: |
| 3 | +# jupytext: |
| 4 | +# text_representation: |
| 5 | +# extension: .py |
| 6 | +# format_name: light |
| 7 | +# format_version: '1.5' |
| 8 | +# jupytext_version: 1.13.6 |
| 9 | +# kernelspec: |
| 10 | +# display_name: venv-quartodoc |
| 11 | +# language: python |
| 12 | +# name: venv-quartodoc |
| 13 | +# --- |
| 14 | + |
| 15 | +# + |
| 16 | +from griffe import dataclasses as dc |
| 17 | +from griffe.loader import GriffeLoader |
| 18 | +from griffe.docstrings.parsers import Parser |
| 19 | +from pathlib import Path |
| 20 | +from plum import dispatch |
| 21 | +from quartodoc import MdRenderer |
| 22 | + |
| 23 | +griffe = GriffeLoader(docstring_parser=Parser("numpy")) |
| 24 | +mod = griffe.load_module("dascore") |
| 25 | + |
| 26 | +# + |
| 27 | + |
| 28 | +# These functions don't render for one of two reasons: they contain a section |
| 29 | +# type that is currently unsupported in the renderer (these are easy to add!), |
| 30 | +# or due to a small bug with how annotations are rendered. |
| 31 | +IGNORE = { |
| 32 | + "get_format", |
| 33 | + "write", |
| 34 | + "patches_to_df", |
| 35 | + "iter_files", |
| 36 | + "format_dtypes", |
| 37 | + "open_hdf5_file", |
| 38 | + "chunk", |
| 39 | + "get_intervals", |
| 40 | + "filter_df", |
| 41 | + "scan_to_df", |
| 42 | +} |
| 43 | + |
| 44 | +renderer = MdRenderer() |
| 45 | + |
| 46 | + |
| 47 | +class AutoSummary: |
| 48 | + def __init__(self, dir_name: str): |
| 49 | + self.dir_name = dir_name |
| 50 | + |
| 51 | + @staticmethod |
| 52 | + def full_name(el): |
| 53 | + return f"{el.parent.canonical_path}.{el.name}" |
| 54 | + |
| 55 | + @dispatch |
| 56 | + def visit(self, el): |
| 57 | + raise TypeError(f"Unsupported type: {type(el)}") |
| 58 | + |
| 59 | + @dispatch |
| 60 | + def visit(self, el: dc.Module): |
| 61 | + print(f"MOD: {el.canonical_path}") |
| 62 | + for name, class_ in el.classes.items(): |
| 63 | + self.visit(class_) |
| 64 | + |
| 65 | + for name, func in el.functions.items(): |
| 66 | + self.visit(func) |
| 67 | + |
| 68 | + for name, mod in el.modules.items(): |
| 69 | + self.visit(mod) |
| 70 | + |
| 71 | + @dispatch |
| 72 | + def visit(self, el: dc.Class): |
| 73 | + if el.name.startswith("_"): |
| 74 | + return |
| 75 | + |
| 76 | + print(f"CLASS: {self.full_name(el)}") |
| 77 | + for name, method in el.members.items(): |
| 78 | + self.visit(method) |
| 79 | + |
| 80 | + @dispatch |
| 81 | + def visit(self, el: dc.Alias): |
| 82 | + # Should skip Aliases, since dascore API docs match its |
| 83 | + # filestructure. |
| 84 | + return None |
| 85 | + |
| 86 | + @dispatch |
| 87 | + def visit(self, el: dc.Function): |
| 88 | + if el.name.startswith("_"): |
| 89 | + return |
| 90 | + if el.name in IGNORE: |
| 91 | + return |
| 92 | + |
| 93 | + full_name = self.full_name(el) |
| 94 | + print(f"FUNCTION: {full_name}") |
| 95 | + |
| 96 | + p_root = Path(self.dir_name) |
| 97 | + p_root.mkdir(exist_ok=True) |
| 98 | + |
| 99 | + p_func = p_root / f"{full_name}.md" |
| 100 | + p_func.write_text(renderer.to_md(el)) |
| 101 | + |
| 102 | + @dispatch |
| 103 | + def visit(self, el: dc.Attribute): |
| 104 | + if el.name.startswith("_"): |
| 105 | + return |
| 106 | + |
| 107 | + # a class attribute |
| 108 | + print(f"ATTR: {self.full_name(el)}") |
| 109 | + |
| 110 | + |
| 111 | +# - |
| 112 | + |
| 113 | +AutoSummary("api").visit(mod) |
0 commit comments