import re import ast from .preProcessing import parse_docstring,generate_html def extract_python_objects(file_path): """Extract Python functions and classes (with methods) and their doc strings from a source file. Args: file_path (str): Path to the Python source file (.py). Returns: list of dict: Each object is either a function or class represented as: { 'type': 'function' or 'class', 'name': str, 'doc': dict, # Parsed docstring 'methods': list (only for class): [ { 'name': str, 'doc': dict # Parsed method docstring } ] } """ with open(file_path, 'r', encoding='utf-8') as f: tree = ast.parse(f.read(), filename=file_path) objects = [] for node in ast.iter_child_nodes(tree): if isinstance(node, ast.FunctionDef): docstring = ast.get_docstring(node) if docstring: doc_sections = parse_docstring(docstring) objects.append({ 'address': file_path, 'type': 'function', 'name': node.name, 'doc': doc_sections }) elif isinstance(node, ast.ClassDef): class_doc = ast.get_docstring(node) class_info = { 'address': file_path, 'type': 'class', 'name': node.name, 'doc': parse_docstring(class_doc) if class_doc else {}, 'methods': [] } for body_item in node.body: if isinstance(body_item, ast.FunctionDef): method_doc = ast.get_docstring(body_item) if method_doc: class_info['methods'].append({ 'address': file_path, 'name': body_item.name, 'doc': parse_docstring(method_doc) }) objects.append(class_info) return objects if __name__ == "__main__": items = extract_python_objects("src/PyThon/Test/test.py") for obj in items: print(generate_html(obj))