| 5 | |
| 6 | |
| 7 | def parse_python_file(file_path): |
| 8 | parsed_contents = { |
| 9 | "imports": [], |
| 10 | "globals": [], |
| 11 | "classes": [], |
| 12 | "functions": [], |
| 13 | } |
| 14 | |
| 15 | with open(file_path, "r") as file: |
| 16 | file_contents = file.read() |
| 17 | parsed_tree = ast.parse(file_contents) |
| 18 | |
| 19 | for node in ast.iter_child_nodes(parsed_tree): |
| 20 | if isinstance(node, ast.Import) or isinstance(node, ast.ImportFrom): |
| 21 | parsed_contents["imports"].append(astor.to_source(node).strip()) |
| 22 | elif isinstance(node, ast.Assign) and len(node.targets) == 1 and isinstance(node.targets[0], ast.Name): |
| 23 | parsed_contents["globals"].append(astor.to_source(node).strip()) |
| 24 | elif isinstance(node, ast.FunctionDef): |
| 25 | if node.name == "main": |
| 26 | parsed_contents["functions"].append(ast.get_source_segment(file_contents, node)) |
| 27 | else: |
| 28 | parsed_contents["functions"].append(ast.get_source_segment(file_contents, node)) |
| 29 | elif isinstance(node, ast.ClassDef): |
| 30 | parsed_contents["classes"].append(ast.get_source_segment(file_contents, node)) |
| 31 | |
| 32 | return parsed_contents |
| 33 | |
| 34 | |
| 35 | def get_methods(class_or_str): |