import inspect import json import shutil from pathlib import Path from textwrap import shorten from typing import Any, Dict, List, Literal, TextIO from docstring_parser import parse from openbb_terminal.core.sdk.trailmap import Trailmap, get_trailmaps from openbb_terminal.core.session.current_system import set_system_variable set_system_variable("TEST_MODE", True) set_system_variable("LOG_COLLECT", False) website_path = Path(__file__).parent.absolute() reference_import = """ import ReferenceCard from "@site/src/components/General/ReferenceCard"; \n") def main() -> bool: """Generate markdown files for OpenBB SDK Documentation.""" print("Loading trailmaps...") trailmaps = get_trailmaps() kwargs = {"encoding": "utf-8", "newline": "\n"} print("Generating markdown files...") content_path = website_path / "content/sdk/reference" reference_cards: Dict[Path, List[Dict[str, str]]] = {} for file in content_path.glob("*"): if file.is_file(): file.unlink() else: shutil.rmtree(file) for trailmap in trailmaps: try: if trailmap.location_path[0] == "root": trailmap.location_path[0] = "" model_meta = ( get_function_meta(trailmap, "model") if trailmap.model else None ) view_meta = get_function_meta(trailmap, "view") if trailmap.view else None markdown = generate_markdown(model_meta, view_meta) if trailmap.class_attr == "index": trailmap.class_attr = "index_cmd" filepath = ( content_path / "/".join(trailmap.location_path) / f"{trailmap.class_attr}.md" ) func = trailmap.func_attrs.get( "model", trailmap.func_attrs.get("view", None) ) reference_cards.setdefault(filepath.parent, []).append( dict( title=trailmap.class_attr, description=func.short_doc, # type: ignore url="/".join(trailmap.location_path), ) ) filepath.parent.mkdir(parents=True, exist_ok=True) with open(filepath, "w", **kwargs) as f: # type: ignore f.write(markdown) except Exception as e: print( f"Error generating {trailmap.location_path} {trailmap.class_attr} - {e}" ) return False # Sort reference_cards reference_cards = dict(sorted(reference_cards.items(), key=lambda item: item[0])) with open(content_path / "index.mdx", "w", **kwargs) as f: # type: ignore fname = "OpenBB SDK Reference" rel_path = content_path.relative_to(content_path) write_reference_index(reference_cards, fname, content_path, rel_path, f) with open(content_path / "_category_.json", "w", **kwargs) as f: # type: ignore """Generate category json""" f.write(json.dumps({"label": "SDK Reference", "position": 4}, indent=2)) def gen_category_json(fname: str, path: Path): """Generate category json""" with open(path / "index.mdx", "w", **kwargs) as f: # type: ignore rel_path = path.relative_to(content_path) write_reference_index(reference_cards, fname, path, rel_path, f) def gen_category_recursive(nested_path: Path): """Generate category json recursively""" for folder in nested_path.iterdir(): if folder.is_dir(): gen_category_json(folder.name, folder) gen_category_recursive(folder) # pylint: disable=cell-var-from-loop gen_category_recursive(content_path) print("Markdown files generated, check the functions folder") return True if __name__ == "__main__": main()