|
| 1 | +import click |
| 2 | + |
1 | 3 | from griffe.loader import GriffeLoader
|
2 | 4 | from griffe.docstrings.parsers import Parser, parse
|
3 | 5 | from griffe.docstrings import dataclasses as ds
|
|
11 | 13 | from typing import Tuple, Union
|
12 | 14 |
|
13 | 15 |
|
| 16 | +@click.command() |
| 17 | +@click.option("--in-name", help="Name of input inventory file") |
| 18 | +@click.option("--out-name", help="Name of result (defaults to <input_name>.json)") |
| 19 | +def convert_inventory(in_name, out_name=None): |
| 20 | + """Convert a sphinx inventory file to json.""" |
| 21 | + import json |
| 22 | + import sphobjinv as soi |
| 23 | + |
| 24 | + from pathlib import Path |
| 25 | + |
| 26 | + if out_name is None: |
| 27 | + out_name = Path(in_name).with_suffix(".json") |
| 28 | + |
| 29 | + inv = soi.Inventory(in_name) |
| 30 | + |
| 31 | + obj = inv.json_dict() |
| 32 | + |
| 33 | + long = list(obj.items()) |
| 34 | + meta, entries = long[:3], [v for k, v in long[3:]] |
| 35 | + |
| 36 | + out = dict(meta) |
| 37 | + out["entries"] = entries |
| 38 | + |
| 39 | + json.dump(out, open(out_name, "w")) |
| 40 | + |
| 41 | + |
14 | 42 | def parse_function(module: str, func_name: str):
|
15 | 43 | griffe = GriffeLoader()
|
16 | 44 | mod = griffe.load_module(module)
|
@@ -183,7 +211,7 @@ def to_md(self, el: ds.DocstringParameter) -> Tuple[str]:
|
183 | 211 | if isinstance(el.annotation, str):
|
184 | 212 | annotation = el.annotation
|
185 | 213 | else:
|
186 |
| - annotation = el.annotation.full if el.annotation else None |
| 214 | + annotation = el.annotation.full if el.annotation else None |
187 | 215 | return (escape(el.name), annotation, el.description, default)
|
188 | 216 |
|
189 | 217 | # examples ----
|
@@ -231,3 +259,7 @@ def to_md(self, el: ExampleText):
|
231 | 259 | )
|
232 | 260 | def to_md(self, el):
|
233 | 261 | raise NotImplementedError(f"{type(el)}")
|
| 262 | + |
| 263 | + |
| 264 | +if __name__ == "__main__": |
| 265 | + convert_inventory() |
0 commit comments