|
| 1 | +""" |
| 2 | +Script to check if open_api_framework has properly documented (or excluded) all environment |
| 3 | +variables that are loaded by the library itself |
| 4 | +""" |
| 5 | + |
| 6 | +import ast |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | + |
| 11 | +class ConfigChecker(ast.NodeVisitor): |
| 12 | + def __init__(self): |
| 13 | + self.issues = [] |
| 14 | + |
| 15 | + def visit_Call(self, node): |
| 16 | + if isinstance(node.func, ast.Name) and node.func.id == "config": |
| 17 | + has_help_text = False |
| 18 | + has_add_to_docs = False |
| 19 | + |
| 20 | + # Check for 'help_text' or 'add_to_docs' in the arguments |
| 21 | + for keyword in node.keywords: |
| 22 | + if keyword.arg == "help_text" and isinstance( |
| 23 | + keyword.value, (ast.Constant, ast.JoinedStr) |
| 24 | + ): |
| 25 | + has_help_text = True |
| 26 | + elif ( |
| 27 | + keyword.arg == "add_to_docs" |
| 28 | + and isinstance(keyword.value, ast.Constant) |
| 29 | + and keyword.value.value is False |
| 30 | + ): |
| 31 | + has_add_to_docs = True |
| 32 | + |
| 33 | + # Record issue if neither is found |
| 34 | + if not (has_help_text or has_add_to_docs): |
| 35 | + self.issues.append((node.lineno, node.col_offset)) |
| 36 | + |
| 37 | + self.generic_visit(node) |
| 38 | + |
| 39 | + |
| 40 | +def check_config_usage(file_path): |
| 41 | + with file_path.open("r") as source: |
| 42 | + tree = ast.parse(source.read(), filename=str(file_path)) |
| 43 | + |
| 44 | + checker = ConfigChecker() |
| 45 | + checker.visit(tree) |
| 46 | + |
| 47 | + return checker.issues |
| 48 | + |
| 49 | + |
| 50 | +def check_library(directory): |
| 51 | + issues = {} |
| 52 | + for file_path in directory.rglob("*.py"): |
| 53 | + issues_in_file = check_config_usage(file_path) |
| 54 | + if issues_in_file: |
| 55 | + issues[file_path] = issues_in_file |
| 56 | + |
| 57 | + return issues |
| 58 | + |
| 59 | + |
| 60 | +# Example usage |
| 61 | +library_directory = Path("open_api_framework/") |
| 62 | +issues = check_library(library_directory) |
| 63 | + |
| 64 | +if issues: |
| 65 | + for file_path, positions in issues.items(): |
| 66 | + for lineno, col_offset in positions: |
| 67 | + print( |
| 68 | + f"Issue in {file_path} at line {lineno}, column {col_offset}: " |
| 69 | + "'config' call lacks 'help_text' or 'add_to_docs=False'" |
| 70 | + ) |
| 71 | + sys.exit(1) |
| 72 | +else: |
| 73 | + print("All 'config' calls are correctly documented.") |
0 commit comments