ml_switcheroo.utils.visualizer ============================== .. py:module:: ml_switcheroo.utils.visualizer .. autoapi-nested-parse:: AST Visualization Utility. This module provides the `MermaidGenerator`, a LibCST visitor that traverses an Abstract Syntax Tree and converts it into a Mermaid.js Graph diagram. It is used to generate "Before" and "After" snapshots of the code structure during transpilation for debugging and documentation. The generator applies specific branding colors and handles complex node representation (like dotted imports) robustly. Classes ------- .. autoapisummary:: ml_switcheroo.utils.visualizer.MermaidGenerator Module Contents --------------- .. py:class:: MermaidGenerator Bases: :py:obj:`libcst.CSTVisitor` Generates a Mermaid Graph TD string from a CST Node tree. It traverses the tree and emits nodes and edges formatted with specific branding colors. .. py:attribute:: COLORS .. py:attribute:: STYLES :value: Multiline-String .. raw:: html
Show Value .. code-block:: python """ %% Styles classDef default font-family:'Google Sans Normal',color:#20344b,stroke:#20344b; classDef modNode fill:#20344b,stroke:#20344b,color:#ffffff,rx:5px,font-family:'Google Sans Medium'; classDef classNode fill:#ea4335,stroke:#20344b,color:#ffffff,rx:5px,font-family:'Google Sans Medium'; classDef funcNode fill:#4285f4,stroke:#20344b,color:#ffffff,rx:5px,font-family:'Google Sans Medium'; classDef callNode fill:#34a853,stroke:#20344b,stroke-width:2px,color:#ffffff,rx:5px,font-family:'Roboto Mono Normal'; classDef stmtNode fill:#ffffff,stroke:#20344b,stroke-dasharray: 2 2,color:#20344b,font-family:'Roboto Mono Normal'; classDef argNode fill:#f9ab00,stroke:#20344b,color:#20344b,rx:2px,font-size:10px; classDef impNode fill:#57caff,stroke:#20344b,color:#20344b,rx:5px; classDef valNode fill:#ff7daf,stroke:#20344b,color:#20344b,rx:5px; """ .. raw:: html
.. py:attribute:: nodes :type: List[str] :value: [] .. py:attribute:: edges :type: List[str] :value: [] .. py:attribute:: stack :type: List[str] :value: [] .. py:method:: generate(tree: libcst.CSTNode) -> str Converts a CST Node into a Mermaid graph definition string. :param tree: The root node of the tree to visualize. :type tree: cst.CSTNode :returns: A complete Mermaid.js graph definition including styles. :rtype: str .. py:method:: visit_Module(node: libcst.Module) -> Optional[bool] Visits Module root. .. py:method:: leave_Module(node: libcst.Module) -> None Leaves Module root. .. py:method:: visit_ClassDef(node: libcst.ClassDef) -> Optional[bool] Visits Class Definitions. .. py:method:: leave_ClassDef(node: libcst.ClassDef) -> None Leaves Class Definitions. .. py:method:: visit_FunctionDef(node: libcst.FunctionDef) -> Optional[bool] Visits Function Definitions. .. py:method:: leave_FunctionDef(node: libcst.FunctionDef) -> None Leaves Function Definitions. .. py:method:: visit_Call(node: libcst.Call) -> Optional[bool] Visits Function Calls. .. py:method:: leave_Call(node: libcst.Call) -> None Leaves Function Calls. .. py:method:: visit_Arg(node: libcst.Arg) -> Optional[bool] Visits Arguments inside a Call. .. py:method:: leave_Arg(node: libcst.Arg) -> None Leaves Arguments. .. py:method:: visit_Import(node: libcst.Import) -> Optional[bool] Visits Import statements (collapsing them into single nodes). .. py:method:: visit_ImportFrom(node: libcst.ImportFrom) -> Optional[bool] Visits From-Import statements (collapsed). .. py:method:: visit_Assign(node: libcst.Assign) -> Optional[bool] Visits Assignment statements. .. py:method:: leave_Assign(node: libcst.Assign) -> None Leaves Assignment statements. .. py:method:: visit_SimpleString(node: libcst.SimpleString) -> Optional[bool] Visits Strings to allow visualizing constant values directly in the graph.