|
| 1 | +""" |
| 2 | +Locates all the examples in the Collection and puts them in a single page. |
| 3 | +""" |
| 4 | + |
| 5 | +import re |
| 6 | +from collections import defaultdict |
| 7 | +from inspect import getmembers, isclass, isfunction |
| 8 | +from pathlib import Path |
| 9 | +from pkgutil import iter_modules |
| 10 | +from textwrap import dedent |
| 11 | +from types import ModuleType |
| 12 | +from typing import Callable, Set, Union |
| 13 | + |
| 14 | +import mkdocs_gen_files |
| 15 | +from griffe.dataclasses import Docstring |
| 16 | +from griffe.docstrings.dataclasses import DocstringSectionKind |
| 17 | +from griffe.docstrings.parsers import Parser, parse |
| 18 | +from prefect.logging.loggers import disable_logger |
| 19 | +from prefect.utilities.importtools import load_module, to_qualified_name |
| 20 | + |
| 21 | +import prefect_aws |
| 22 | + |
| 23 | +COLLECTION_SLUG = "prefect_aws" |
| 24 | + |
| 25 | + |
| 26 | +def skip_parsing(name: str, obj: Union[ModuleType, Callable], module_nesting: str): |
| 27 | + """ |
| 28 | + Skips parsing the object if it's a private object or if it's not in the |
| 29 | + module nesting, preventing imports from other libraries from being added to the |
| 30 | + examples catalog. |
| 31 | + """ |
| 32 | + try: |
| 33 | + wrong_module = not to_qualified_name(obj).startswith(module_nesting) |
| 34 | + except AttributeError: |
| 35 | + wrong_module = False |
| 36 | + return obj.__doc__ is None or name.startswith("_") or wrong_module |
| 37 | + |
| 38 | + |
| 39 | +def skip_block_load_code_example(code_example: str) -> bool: |
| 40 | + """ |
| 41 | + Skips the code example if it's just showing how to load a Block. |
| 42 | + """ |
| 43 | + return re.search(r'\.load\("BLOCK_NAME"\)\s*$', code_example.rstrip("`")) |
| 44 | + |
| 45 | + |
| 46 | +def get_code_examples(obj: Union[ModuleType, Callable]) -> Set[str]: |
| 47 | + """ |
| 48 | + Gathers all the code examples within an object. |
| 49 | + """ |
| 50 | + code_examples = set() |
| 51 | + with disable_logger("griffe.docstrings.google"): |
| 52 | + with disable_logger("griffe.agents.nodes"): |
| 53 | + docstring = Docstring(obj.__doc__) |
| 54 | + parsed_sections = parse(docstring, Parser.google) |
| 55 | + |
| 56 | + for section in parsed_sections: |
| 57 | + if section.kind == DocstringSectionKind.examples: |
| 58 | + code_example = "\n".join( |
| 59 | + (part[1] for part in section.as_dict().get("value", [])) |
| 60 | + ) |
| 61 | + if not skip_block_load_code_example(code_example): |
| 62 | + code_examples.add(code_example) |
| 63 | + if section.kind == DocstringSectionKind.admonition: |
| 64 | + value = section.as_dict().get("value", {}) |
| 65 | + if value.get("annotation") == "example": |
| 66 | + code_example = value.get("description") |
| 67 | + if not skip_block_load_code_example(code_example): |
| 68 | + code_examples.add(code_example) |
| 69 | + |
| 70 | + return code_examples |
| 71 | + |
| 72 | + |
| 73 | +code_examples_grouping = defaultdict(set) |
| 74 | +for _, module_name, ispkg in iter_modules(prefect_aws.__path__): |
| 75 | + |
| 76 | + module_nesting = f"{COLLECTION_SLUG}.{module_name}" |
| 77 | + module_obj = load_module(module_nesting) |
| 78 | + |
| 79 | + # find all module examples |
| 80 | + if skip_parsing(module_name, module_obj, module_nesting): |
| 81 | + continue |
| 82 | + code_examples_grouping[module_name] |= get_code_examples(module_obj) |
| 83 | + |
| 84 | + # find all class and method examples |
| 85 | + for class_name, class_obj in getmembers(module_obj, isclass): |
| 86 | + if skip_parsing(class_name, class_obj, module_nesting): |
| 87 | + continue |
| 88 | + code_examples_grouping[module_name] |= get_code_examples(class_obj) |
| 89 | + for method_name, method_obj in getmembers(class_obj, isfunction): |
| 90 | + if skip_parsing(method_name, method_obj, module_nesting): |
| 91 | + continue |
| 92 | + code_examples_grouping[module_name] |= get_code_examples(method_obj) |
| 93 | + |
| 94 | + # find all function examples |
| 95 | + for function_name, function_obj in getmembers(module_obj, callable): |
| 96 | + if skip_parsing(function_name, function_obj, module_nesting): |
| 97 | + continue |
| 98 | + code_examples_grouping[module_name] |= get_code_examples(function_obj) |
| 99 | + |
| 100 | + |
| 101 | +examples_catalog_path = Path("examples_catalog.md") |
| 102 | +with mkdocs_gen_files.open(examples_catalog_path, "w") as generated_file: |
| 103 | + generated_file.write( |
| 104 | + dedent( |
| 105 | + """ |
| 106 | + # Examples Catalog |
| 107 | +
|
| 108 | + Below is a list of examples for `prefect-aws`. |
| 109 | + """ |
| 110 | + ) |
| 111 | + ) |
| 112 | + for module_name, code_examples in code_examples_grouping.items(): |
| 113 | + if len(code_examples) == 0: |
| 114 | + continue |
| 115 | + module_title = module_name.replace("_", " ").title() |
| 116 | + generated_file.write( |
| 117 | + f"## [{module_title} Module][{COLLECTION_SLUG}.{module_name}]\n" |
| 118 | + ) |
| 119 | + for code_example in code_examples: |
| 120 | + generated_file.write(code_example + "\n") |
0 commit comments