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
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
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
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
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
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
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.
Example
root = parse_object("my_module.py")
tree_str = generate_tree(root, options)
print(tree_str)
Source code in src/jinjarope/codetree.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
|
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
tree = get_structure_map(
"myfile.py",
show_types=False,
show_line_numbers=True,
sort_alphabetically=True
)
print(tree)
Source code in src/jinjarope/codetree.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
|
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
|
PathLike[str] | str | type
|
Path to the Python source file |
required |
Example
root = parse_object("example.py")
print(f"Found {len(root.children)} top-level definitions")
Source code in src/jinjarope/codetree.py
118 119 120 121 122 123 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 |
|