codetree
Class info¶
Classes¶
| Name | Children | Inherits |
|---|---|---|
| Node jinjarope.codetree Represents a node in the Python code structure tree. |
||
| NodeType jinjarope.codetree Types of nodes found in Python code structure analysis. |
||
| TreeOptions jinjarope.codetree Configuration options for tree visualization. |
🛈 DocStrings¶
Analyzes and visualizes Python code structure as a tree representation.
This module provides functionality to parse Python source files and generate a tree-like visualization of their structure, including classes, methods, and functions with their decorators.
Node
dataclass
¶
Represents a node in the Python code structure tree.
A node contains information about a specific code element (like a class or function) and maintains relationships with its child nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The identifier of the code element |
required |
type
|
NodeType
|
The classification of the code element |
required |
children
|
list[Node]
|
List of child nodes |
required |
line_number
|
int
|
Source code line where this element appears |
required |
decorators
|
list[str]
|
List of decorator names applied to this element |
required |
Source code in src/jinjarope/codetree.py
NodeType
¶
Bases: Enum
Types of nodes found in Python code structure analysis.
Each enum value represents a different structural element that can be found when parsing Python source code.
Source code in src/jinjarope/codetree.py
TreeOptions
dataclass
¶
Configuration options for tree visualization.
Controls the appearance and content of the generated tree structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
show_types
|
bool
|
Include node type annotations |
True
|
show_line_numbers
|
bool
|
Display source line numbers |
False
|
show_decorators
|
bool
|
Include decorator information |
True
|
sort_alphabetically
|
bool
|
Sort nodes by name |
False
|
include_private
|
bool
|
Include private members (_name) |
True
|
include_dunder
|
bool
|
Include double underscore methods (name) |
False
|
max_depth
|
int | None
|
Maximum tree depth to display |
None
|
branch_style
|
str
|
Style of tree branches ("ascii" or "unicode") |
'ascii'
|
Source code in src/jinjarope/codetree.py
generate_tree
¶
generate_tree(
node: Node,
options: TreeOptions,
prefix: str = "",
is_last: bool = True,
depth: int = 0,
) -> str
Recursively generate a textual tree representation from a Node.
Traverses the provided Node structure and creates a string representing the tree in ASCII or Unicode format, applying the specified options to control its appearance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
The Node to start from |
required |
options
|
TreeOptions
|
Configuration options for tree visualization |
required |
prefix
|
str
|
Prefix string to use for indentation |
''
|
is_last
|
bool
|
Flag indicating if this node is the last child |
True
|
depth
|
int
|
Current depth in the tree |
0
|
Note
This function is recursive and uses the provided options to determine if a node should be included and how it's formatted.
Source code in src/jinjarope/codetree.py
get_structure_map
¶
get_structure_map(
obj: PathLike[str] | str,
*,
show_types: bool = True,
show_line_numbers: bool = False,
show_decorators: bool = True,
sort_alphabetically: bool = False,
include_private: bool = True,
include_dunder: bool = False,
max_depth: int | None = None,
use_unicode: bool = True
) -> str
Generate a textual tree representation of Python code structure.
Creates a visual tree showing the hierarchical structure of classes, methods, and functions in a Python file, with customizable display options.
Tip
Use use_unicode=True for better-looking trees in terminals
that support Unicode characters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
PathLike[str] | str
|
Path to the Python source file or a Python object |
required |
show_types
|
bool
|
Include node type annotations |
True
|
show_line_numbers
|
bool
|
Display source line numbers |
False
|
show_decorators
|
bool
|
Include decorator information |
True
|
sort_alphabetically
|
bool
|
Sort nodes by name |
False
|
include_private
|
bool
|
Include private members |
True
|
include_dunder
|
bool
|
Include double underscore methods |
False
|
max_depth
|
int | None
|
Maximum tree depth to display |
None
|
use_unicode
|
bool
|
Use Unicode characters for tree branches |
True
|
Example
Source code in src/jinjarope/codetree.py
parse_object
¶
Parse Python source code into a tree structure.
Analyzes the AST of a Python file and creates a hierarchical representation of its structure, including classes, methods, and functions.
Note
The parser recognizes special method types through decorators and handles both synchronous and asynchronous definitions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
AnyPath | type
|
Path to the Python source file |
required |
Example
Source code in src/jinjarope/codetree.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |