ast-explore 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ast_explore/__init__.py +48 -0
- ast_explore/cli.py +130 -0
- ast_explore/display.py +129 -0
- ast_explore/explorer.py +251 -0
- ast_explore-0.1.0.dist-info/METADATA +482 -0
- ast_explore-0.1.0.dist-info/RECORD +9 -0
- ast_explore-0.1.0.dist-info/WHEEL +4 -0
- ast_explore-0.1.0.dist-info/entry_points.txt +2 -0
- ast_explore-0.1.0.dist-info/licenses/LICENSE +201 -0
ast_explore/__init__.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""Tool for exploring the AST of given Python source code."""
|
|
2
|
+
|
|
3
|
+
import ast
|
|
4
|
+
import importlib.metadata
|
|
5
|
+
import sys
|
|
6
|
+
from collections.abc import Iterator
|
|
7
|
+
|
|
8
|
+
__version__ = importlib.metadata.version(__name__)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
PYTHON_VERSION = f'{sys.version_info.major}.{sys.version_info.minor}'
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def ast_node_types_generator() -> Iterator[str]:
|
|
15
|
+
"""
|
|
16
|
+
Generator for getting the names of all the AST node types.
|
|
17
|
+
|
|
18
|
+
Yields
|
|
19
|
+
------
|
|
20
|
+
Iterator[str]
|
|
21
|
+
Names of each of the AST node types.
|
|
22
|
+
"""
|
|
23
|
+
seen = set()
|
|
24
|
+
|
|
25
|
+
def subclass_generator(base_class: type) -> Iterator[str]:
|
|
26
|
+
"""
|
|
27
|
+
Generator for getting the names of all of a class's subclasses recursively.
|
|
28
|
+
|
|
29
|
+
Parameters
|
|
30
|
+
----------
|
|
31
|
+
base_class : type
|
|
32
|
+
The class for which to grab all subclasses.
|
|
33
|
+
|
|
34
|
+
Yields
|
|
35
|
+
------
|
|
36
|
+
Iterator[str]
|
|
37
|
+
Names of each subclass, regardless of how many levels deep.
|
|
38
|
+
"""
|
|
39
|
+
for subclass in base_class.__subclasses__():
|
|
40
|
+
if subclass not in seen:
|
|
41
|
+
seen.add(subclass)
|
|
42
|
+
yield subclass.__name__
|
|
43
|
+
yield from subclass_generator(subclass)
|
|
44
|
+
|
|
45
|
+
yield from subclass_generator(ast.AST)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
__all__ = ['PYTHON_VERSION', '__version__', 'ast_node_types_generator']
|
ast_explore/cli.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""CLI for exploring an AST of Python source code."""
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import difflib
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
from typing import cast
|
|
7
|
+
|
|
8
|
+
from . import __version__, ast_node_types_generator
|
|
9
|
+
from .explorer import NodeExplorer
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def validate_node_type(node_type: str) -> str:
|
|
13
|
+
"""
|
|
14
|
+
Validate node type is an AST node.
|
|
15
|
+
|
|
16
|
+
Parameters
|
|
17
|
+
----------
|
|
18
|
+
node_type : str
|
|
19
|
+
The node type to validate.
|
|
20
|
+
|
|
21
|
+
Returns
|
|
22
|
+
-------
|
|
23
|
+
str
|
|
24
|
+
The node type, if it is valid.
|
|
25
|
+
|
|
26
|
+
Raises
|
|
27
|
+
------
|
|
28
|
+
argparse.ArgumentTypeError
|
|
29
|
+
This is raised when the node type is not valid, and an attempt will be made to
|
|
30
|
+
suggest a close match for a valid node type (if one exists).
|
|
31
|
+
"""
|
|
32
|
+
if (
|
|
33
|
+
not node_type
|
|
34
|
+
or (node_name := node_type.removeprefix('ast.')) in ast_node_types_generator()
|
|
35
|
+
):
|
|
36
|
+
return node_type
|
|
37
|
+
|
|
38
|
+
msg = f'Unknown AST node type "{node_name}".'
|
|
39
|
+
if did_you_mean := difflib.get_close_matches(
|
|
40
|
+
node_name, ast_node_types_generator(), n=1
|
|
41
|
+
):
|
|
42
|
+
msg += f' Did you mean "{did_you_mean[0]}"?'
|
|
43
|
+
raise argparse.ArgumentTypeError(msg)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def get_parser() -> argparse.ArgumentParser:
|
|
47
|
+
"""
|
|
48
|
+
Get an argument parser for the AST explorer CLI.
|
|
49
|
+
|
|
50
|
+
Returns
|
|
51
|
+
-------
|
|
52
|
+
argparse.ArgumentParser
|
|
53
|
+
The configured argument parser.
|
|
54
|
+
"""
|
|
55
|
+
parser = argparse.ArgumentParser(
|
|
56
|
+
description='Traverse the AST of the input source code and explore the nodes encountered'
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
parser.add_argument(
|
|
60
|
+
'-v', '--version', action='version', version=f'%(prog)s {__version__}'
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
parser.add_argument(
|
|
64
|
+
metavar='source_code',
|
|
65
|
+
dest='source_code_file_path',
|
|
66
|
+
help='Source code file to analyze',
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
parser.add_argument(
|
|
70
|
+
'--types',
|
|
71
|
+
nargs='*',
|
|
72
|
+
help=(
|
|
73
|
+
'node types to explore (e.g., --types Try ExceptionHandler). '
|
|
74
|
+
"If you don't provide any specific types, all will be explored."
|
|
75
|
+
),
|
|
76
|
+
type=validate_node_type,
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
parser.add_argument(
|
|
80
|
+
'--interactive',
|
|
81
|
+
action='store_true',
|
|
82
|
+
help=(
|
|
83
|
+
'whether to run in interactive mode, which allows the user to decide '
|
|
84
|
+
'whether they want to visit a given node.'
|
|
85
|
+
),
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
return parser
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
92
|
+
"""
|
|
93
|
+
Explore the AST of a given Python source code module.
|
|
94
|
+
|
|
95
|
+
Parameters
|
|
96
|
+
----------
|
|
97
|
+
argv : Sequence[str] | None, optional
|
|
98
|
+
The arguments passed on the command line.
|
|
99
|
+
|
|
100
|
+
Returns
|
|
101
|
+
-------
|
|
102
|
+
int
|
|
103
|
+
Exit code for the process. Non-zero exit codes mean something went wrong.
|
|
104
|
+
"""
|
|
105
|
+
args = get_parser().parse_args(argv)
|
|
106
|
+
|
|
107
|
+
try:
|
|
108
|
+
visitor = NodeExplorer(args.source_code_file_path, args.types, args.interactive)
|
|
109
|
+
except (FileNotFoundError, SyntaxError):
|
|
110
|
+
return 1
|
|
111
|
+
except Exception as exc:
|
|
112
|
+
print(exc)
|
|
113
|
+
return 1
|
|
114
|
+
|
|
115
|
+
try:
|
|
116
|
+
visitor.run()
|
|
117
|
+
except KeyboardInterrupt:
|
|
118
|
+
print('\n🛑 Exiting...')
|
|
119
|
+
return 0
|
|
120
|
+
except SystemExit as exit:
|
|
121
|
+
return cast('int', exit.code)
|
|
122
|
+
except Exception as exc:
|
|
123
|
+
print(exc)
|
|
124
|
+
return 1
|
|
125
|
+
|
|
126
|
+
return 0
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
if __name__ == '__main__':
|
|
130
|
+
raise SystemExit(main())
|
ast_explore/display.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"""Display utilities."""
|
|
2
|
+
|
|
3
|
+
# mypy: disable-error-code="attr-defined"
|
|
4
|
+
import ast
|
|
5
|
+
import itertools
|
|
6
|
+
import math
|
|
7
|
+
import operator
|
|
8
|
+
import shutil
|
|
9
|
+
|
|
10
|
+
TERMINAL_WIDTH, TERMINAL_HEIGHT = shutil.get_terminal_size()
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def print_header(title: str, symbol: str) -> None:
|
|
14
|
+
"""
|
|
15
|
+
Print a header section with the title centered.
|
|
16
|
+
|
|
17
|
+
Parameters
|
|
18
|
+
----------
|
|
19
|
+
title : str
|
|
20
|
+
The title of the section.
|
|
21
|
+
symbol : str
|
|
22
|
+
The symbol to use to build the border.
|
|
23
|
+
"""
|
|
24
|
+
line = symbol * TERMINAL_WIDTH
|
|
25
|
+
title_line = f'{title:^{TERMINAL_WIDTH}}'
|
|
26
|
+
|
|
27
|
+
if len(title) < TERMINAL_WIDTH - 6:
|
|
28
|
+
border = symbol * 2
|
|
29
|
+
title_line = border + title_line[2:-2] + border
|
|
30
|
+
|
|
31
|
+
print(line, title_line, line, sep='\n', end='\n\n')
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def print_section_divider(symbol: str) -> None:
|
|
35
|
+
"""
|
|
36
|
+
Print section divider.
|
|
37
|
+
|
|
38
|
+
Parameters
|
|
39
|
+
----------
|
|
40
|
+
symbol : str
|
|
41
|
+
The symbol to use for the divider.
|
|
42
|
+
"""
|
|
43
|
+
print(f'{symbol * TERMINAL_WIDTH}\n')
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def print_source_code(
|
|
47
|
+
source_code: str, node: ast.AST, max_lines: int | None = None
|
|
48
|
+
) -> None:
|
|
49
|
+
"""
|
|
50
|
+
Print a source code segment with line numbers.
|
|
51
|
+
|
|
52
|
+
Parameters
|
|
53
|
+
----------
|
|
54
|
+
source_code : str
|
|
55
|
+
The source code.
|
|
56
|
+
node : ast.AST
|
|
57
|
+
The AST node.
|
|
58
|
+
max_lines : int | None, optional
|
|
59
|
+
The maximum number of lines to show. By default, show as many lines as can fit
|
|
60
|
+
in the terminal window.
|
|
61
|
+
"""
|
|
62
|
+
code_lines = source_code.splitlines()
|
|
63
|
+
|
|
64
|
+
start_line_number = node.lineno
|
|
65
|
+
highlight_line_number = None
|
|
66
|
+
|
|
67
|
+
arrow = ' '
|
|
68
|
+
underline = None
|
|
69
|
+
if node.lineno == node.end_lineno:
|
|
70
|
+
highlight_line_number = node.lineno
|
|
71
|
+
start_line_number = max(1, start_line_number - 2)
|
|
72
|
+
|
|
73
|
+
code_segment = list(
|
|
74
|
+
itertools.dropwhile(
|
|
75
|
+
operator.not_, code_lines[start_line_number - 1 : highlight_line_number]
|
|
76
|
+
)
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
if (width := node.end_col_offset - node.col_offset) < len(code_segment[-1]):
|
|
80
|
+
underline = '^' * width
|
|
81
|
+
else:
|
|
82
|
+
code_segment = (
|
|
83
|
+
ast.get_source_segment(source_code, node, padded=True) or ''
|
|
84
|
+
).splitlines()
|
|
85
|
+
|
|
86
|
+
if (num_lines := len(code_segment)) > 1 and (
|
|
87
|
+
num_lines > (node.end_lineno - node.lineno + 1)
|
|
88
|
+
):
|
|
89
|
+
arrow = '-> '
|
|
90
|
+
|
|
91
|
+
end_line_number = num_lines + start_line_number
|
|
92
|
+
|
|
93
|
+
digits = math.ceil(math.log10(end_line_number))
|
|
94
|
+
if arrow:
|
|
95
|
+
digits += len(arrow)
|
|
96
|
+
|
|
97
|
+
padding = digits
|
|
98
|
+
separator = ' | '
|
|
99
|
+
|
|
100
|
+
def _get_line_prefix(
|
|
101
|
+
line_number: int, node: ast.AST
|
|
102
|
+
) -> str: # numpydoc ignore=PR01,RT01
|
|
103
|
+
"""Helper function to get the prefix for the line of source code."""
|
|
104
|
+
text = (
|
|
105
|
+
f'{arrow}{line_number}'
|
|
106
|
+
if node.lineno <= line_number <= node.end_lineno
|
|
107
|
+
else line_number
|
|
108
|
+
)
|
|
109
|
+
return f'{f"{text}":>{padding}}'
|
|
110
|
+
|
|
111
|
+
print('\n🐍 Source code represented by the node:')
|
|
112
|
+
print(
|
|
113
|
+
*(
|
|
114
|
+
[
|
|
115
|
+
f'{f"{_get_line_prefix(line_number, node)}":>{padding}}{separator}{code}'
|
|
116
|
+
for line_number, code in zip(
|
|
117
|
+
range(start_line_number, end_line_number),
|
|
118
|
+
code_segment[: max_lines or TERMINAL_HEIGHT],
|
|
119
|
+
strict=False,
|
|
120
|
+
)
|
|
121
|
+
]
|
|
122
|
+
),
|
|
123
|
+
sep='\n',
|
|
124
|
+
end='\n',
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
if underline:
|
|
128
|
+
print(f'{" ":>{padding}}{separator}{" ":>{node.col_offset}}{underline}')
|
|
129
|
+
print()
|
ast_explore/explorer.py
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
"""Traverse the AST to provide information on desired nodes."""
|
|
2
|
+
|
|
3
|
+
import ast
|
|
4
|
+
import contextlib
|
|
5
|
+
import itertools
|
|
6
|
+
import os
|
|
7
|
+
import reprlib
|
|
8
|
+
import time
|
|
9
|
+
from collections.abc import Sequence
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import cast
|
|
12
|
+
|
|
13
|
+
from . import PYTHON_VERSION, ast_node_types_generator
|
|
14
|
+
from .display import print_header, print_section_divider, print_source_code
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class NodeExplorer(ast.NodeVisitor):
|
|
18
|
+
"""
|
|
19
|
+
Node visitor capable of interactively exploring nodes of the AST during traversal.
|
|
20
|
+
|
|
21
|
+
Parameters
|
|
22
|
+
----------
|
|
23
|
+
source_code_file_path : str | os.PathLike[str]
|
|
24
|
+
Path to the Python source code to explore. The code must be valid Python syntax.
|
|
25
|
+
nodes_to_explore : Sequence[str] | Sequence[type[ast.AST]] | None
|
|
26
|
+
The types of nodes to explore. When set to ``None``, the traversal will explore
|
|
27
|
+
each node encountered. Provide a sequence of strings (*e.g.*, ``['Try', 'Assert']``)
|
|
28
|
+
or :mod:`ast` types (*e.g.*, ``[ast.Try, ast.Assert]``) to only explore specific
|
|
29
|
+
node types.
|
|
30
|
+
interactive : bool, default=False
|
|
31
|
+
Whether to explore the AST interactively, in which case the traversal will stop
|
|
32
|
+
to show you information about the node and wait for you to decide the next step.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def __init__(
|
|
36
|
+
self,
|
|
37
|
+
source_code_file_path: str | os.PathLike[str],
|
|
38
|
+
nodes_to_explore: Sequence[str] | Sequence[type[ast.AST]] | None,
|
|
39
|
+
interactive: bool = False,
|
|
40
|
+
) -> None:
|
|
41
|
+
self._nodes_visited = itertools.count(1)
|
|
42
|
+
|
|
43
|
+
file_path = Path(source_code_file_path).resolve()
|
|
44
|
+
print(f'📖 Reading Python source code from {file_path}...')
|
|
45
|
+
|
|
46
|
+
try:
|
|
47
|
+
self._source_code = file_path.read_text()
|
|
48
|
+
except FileNotFoundError as exc:
|
|
49
|
+
print(f'⚠️ {exc.strerror}')
|
|
50
|
+
raise
|
|
51
|
+
|
|
52
|
+
print(f'🔍 Parsing into a Python {PYTHON_VERSION} AST...')
|
|
53
|
+
try:
|
|
54
|
+
self.tree = ast.parse(self._source_code)
|
|
55
|
+
except SyntaxError:
|
|
56
|
+
print('⚠️ Input source code is not syntactically-correct')
|
|
57
|
+
raise
|
|
58
|
+
|
|
59
|
+
match nodes_to_explore:
|
|
60
|
+
case None:
|
|
61
|
+
self._nodes_to_explore = set(ast_node_types_generator())
|
|
62
|
+
case [str(), *other_node_types] if all(
|
|
63
|
+
isinstance(node_type, str) for node_type in other_node_types
|
|
64
|
+
):
|
|
65
|
+
self._nodes_to_explore = {
|
|
66
|
+
node_type.removeprefix('ast.') for node_type in nodes_to_explore
|
|
67
|
+
}
|
|
68
|
+
case [type(), *other_node_types] if all(
|
|
69
|
+
issubclass(node_type, ast.AST) for node_type in other_node_types
|
|
70
|
+
):
|
|
71
|
+
self._nodes_to_explore = {
|
|
72
|
+
cast('str', node_type.__name__) for node_type in nodes_to_explore
|
|
73
|
+
}
|
|
74
|
+
case _:
|
|
75
|
+
raise TypeError(
|
|
76
|
+
'nodes_to_explore must be a sequence of strings or AST classes, if not None'
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
self.stack: list[str] = []
|
|
80
|
+
self._interactive = interactive
|
|
81
|
+
|
|
82
|
+
def _prompt_user(self, prompt: str) -> bool:
|
|
83
|
+
"""
|
|
84
|
+
Prompt user (when in interactive mode) or return ``True``.
|
|
85
|
+
|
|
86
|
+
Parameters
|
|
87
|
+
----------
|
|
88
|
+
prompt : str
|
|
89
|
+
The prompt. Supported options will be appended automatically.
|
|
90
|
+
|
|
91
|
+
Returns
|
|
92
|
+
-------
|
|
93
|
+
bool
|
|
94
|
+
Whether the user wants to explore further.
|
|
95
|
+
|
|
96
|
+
Raises
|
|
97
|
+
------
|
|
98
|
+
SystemExit
|
|
99
|
+
This is raised when the user requests to quit.
|
|
100
|
+
"""
|
|
101
|
+
if self._interactive:
|
|
102
|
+
user_input = (
|
|
103
|
+
input(f'❓ {prompt} [y]es [n]o [q]uit: ').casefold().strip()[:1]
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
match user_input:
|
|
107
|
+
case 'q':
|
|
108
|
+
print('🛑 Quitting...')
|
|
109
|
+
raise SystemExit(0)
|
|
110
|
+
case 'n':
|
|
111
|
+
return False
|
|
112
|
+
case 'y' | '':
|
|
113
|
+
print()
|
|
114
|
+
return True
|
|
115
|
+
case _:
|
|
116
|
+
print(f'🫤 Invalid response "{user_input}"')
|
|
117
|
+
time.sleep(1)
|
|
118
|
+
return self._prompt_user(prompt)
|
|
119
|
+
|
|
120
|
+
return True
|
|
121
|
+
|
|
122
|
+
def _show_source_code(self, node: ast.AST) -> None:
|
|
123
|
+
"""
|
|
124
|
+
Show the source code for a given node (if applicable).
|
|
125
|
+
|
|
126
|
+
Parameters
|
|
127
|
+
----------
|
|
128
|
+
node : ast.AST
|
|
129
|
+
The node to explore.
|
|
130
|
+
"""
|
|
131
|
+
try:
|
|
132
|
+
print_source_code(self._source_code, node)
|
|
133
|
+
|
|
134
|
+
if self._prompt_user('Show location fields?'):
|
|
135
|
+
location_fields = [
|
|
136
|
+
'lineno',
|
|
137
|
+
'end_lineno',
|
|
138
|
+
'col_offset',
|
|
139
|
+
'end_col_offset',
|
|
140
|
+
]
|
|
141
|
+
gap = max(len(field) for field in location_fields)
|
|
142
|
+
print(
|
|
143
|
+
'📍 Location in the source code:',
|
|
144
|
+
*[
|
|
145
|
+
f' - {key:<{gap + 1}}: {getattr(node, key)}'
|
|
146
|
+
for key in location_fields
|
|
147
|
+
],
|
|
148
|
+
sep='\n',
|
|
149
|
+
end='\n\n',
|
|
150
|
+
)
|
|
151
|
+
except (AttributeError, TypeError):
|
|
152
|
+
print('\n📍 This node type does not have any line number information.\n')
|
|
153
|
+
|
|
154
|
+
def _show_node_specific_fields(self, node: ast.AST) -> None:
|
|
155
|
+
"""
|
|
156
|
+
Show node-specific fields and their values.
|
|
157
|
+
|
|
158
|
+
Parameters
|
|
159
|
+
----------
|
|
160
|
+
node : ast.AST
|
|
161
|
+
The node to explore.
|
|
162
|
+
"""
|
|
163
|
+
if self._prompt_user('Do you want more information on this node?'):
|
|
164
|
+
match node:
|
|
165
|
+
case ast.Load() | ast.Store() | ast.Del():
|
|
166
|
+
print(
|
|
167
|
+
'💡 This node specifies the context in which a variable',
|
|
168
|
+
'(ast.Name) is used.',
|
|
169
|
+
)
|
|
170
|
+
case ast.Module():
|
|
171
|
+
print('📝', end=' ')
|
|
172
|
+
if docstring := ast.get_docstring(node):
|
|
173
|
+
print(f'Module docstring: {reprlib.repr(docstring)}')
|
|
174
|
+
else:
|
|
175
|
+
print('Docstring is missing.')
|
|
176
|
+
print()
|
|
177
|
+
case _:
|
|
178
|
+
with contextlib.suppress(TypeError):
|
|
179
|
+
if not ast.get_docstring(node): # type: ignore[arg-type]
|
|
180
|
+
print('📝 Docstring is missing.\n')
|
|
181
|
+
|
|
182
|
+
if node_specific_fields := [
|
|
183
|
+
(key, reprlib.repr(value)) for key, value in ast.iter_fields(node)
|
|
184
|
+
]:
|
|
185
|
+
gap = max(len(key) for key, _ in node_specific_fields)
|
|
186
|
+
print(
|
|
187
|
+
'✨ AST node-specific fields and their values:',
|
|
188
|
+
*[
|
|
189
|
+
f' - {key:<{gap + 1}}: {value}'
|
|
190
|
+
for key, value in node_specific_fields
|
|
191
|
+
],
|
|
192
|
+
sep='\n',
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
def _explore(self, node: ast.AST) -> None:
|
|
196
|
+
"""
|
|
197
|
+
Explore an AST node.
|
|
198
|
+
|
|
199
|
+
Parameters
|
|
200
|
+
----------
|
|
201
|
+
node : ast.AST
|
|
202
|
+
The node to explore.
|
|
203
|
+
"""
|
|
204
|
+
node_name = node.__class__.__name__
|
|
205
|
+
node_class = f'{node.__module__}.{node_name}'
|
|
206
|
+
|
|
207
|
+
if node_name in self._nodes_to_explore:
|
|
208
|
+
list_item = (
|
|
209
|
+
f'{next(self._nodes_visited)}. {node_name} '
|
|
210
|
+
f'(https://docs.python.org/{PYTHON_VERSION}/library/ast.html#{node_class})'
|
|
211
|
+
)
|
|
212
|
+
print_header(list_item, '*')
|
|
213
|
+
|
|
214
|
+
print(
|
|
215
|
+
'🌲 Path to this node from the root node of the AST:',
|
|
216
|
+
' -> '.join(self.stack),
|
|
217
|
+
sep='\n ',
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
self._show_source_code(node)
|
|
221
|
+
self._show_node_specific_fields(node)
|
|
222
|
+
print()
|
|
223
|
+
|
|
224
|
+
if not self._prompt_user('Continue traversal?'):
|
|
225
|
+
print('🛑 Stopping traversal...')
|
|
226
|
+
raise SystemExit(0)
|
|
227
|
+
|
|
228
|
+
if self._interactive:
|
|
229
|
+
print(f'🚀 Leaving {node_name} node...')
|
|
230
|
+
print()
|
|
231
|
+
|
|
232
|
+
def generic_visit(self, node: ast.AST) -> None:
|
|
233
|
+
"""
|
|
234
|
+
Visit each node, with the option to explore before continuing the traversal.
|
|
235
|
+
|
|
236
|
+
Parameters
|
|
237
|
+
----------
|
|
238
|
+
node : ast.AST
|
|
239
|
+
The AST node to visit.
|
|
240
|
+
"""
|
|
241
|
+
self.stack.append(node.__class__.__name__)
|
|
242
|
+
self._explore(node)
|
|
243
|
+
super().generic_visit(node)
|
|
244
|
+
self.stack.pop()
|
|
245
|
+
|
|
246
|
+
def run(self) -> None:
|
|
247
|
+
"""Traverse the AST from the root to the leaves."""
|
|
248
|
+
print('✅ Ready to explore the AST! Starting depth-first traversal...\n')
|
|
249
|
+
self.visit(self.tree)
|
|
250
|
+
print_section_divider('*')
|
|
251
|
+
print('🏆 Traversal completed!')
|
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ast-explore
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Tool for exploring the AST of given Python source code.
|
|
5
|
+
Project-URL: Documentation, https://github.com/stefmolin/ast-explore
|
|
6
|
+
Project-URL: Homepage, https://github.com/stefmolin/ast-explore
|
|
7
|
+
Project-URL: Source, https://github.com/stefmolin/ast-explore
|
|
8
|
+
Author-email: Stefanie Molin <ast-explore@stefaniemolin.com>
|
|
9
|
+
License: Apache License
|
|
10
|
+
Version 2.0, January 2004
|
|
11
|
+
http://www.apache.org/licenses/
|
|
12
|
+
|
|
13
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
14
|
+
|
|
15
|
+
1. Definitions.
|
|
16
|
+
|
|
17
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
18
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
19
|
+
|
|
20
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
21
|
+
the copyright owner that is granting the License.
|
|
22
|
+
|
|
23
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
24
|
+
other entities that control, are controlled by, or are under common
|
|
25
|
+
control with that entity. For the purposes of this definition,
|
|
26
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
27
|
+
direction or management of such entity, whether by contract or
|
|
28
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
29
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
30
|
+
|
|
31
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
32
|
+
exercising permissions granted by this License.
|
|
33
|
+
|
|
34
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
35
|
+
including but not limited to software source code, documentation
|
|
36
|
+
source, and configuration files.
|
|
37
|
+
|
|
38
|
+
"Object" form shall mean any form resulting from mechanical
|
|
39
|
+
transformation or translation of a Source form, including but
|
|
40
|
+
not limited to compiled object code, generated documentation,
|
|
41
|
+
and conversions to other media types.
|
|
42
|
+
|
|
43
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
44
|
+
Object form, made available under the License, as indicated by a
|
|
45
|
+
copyright notice that is included in or attached to the work
|
|
46
|
+
(an example is provided in the Appendix below).
|
|
47
|
+
|
|
48
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
49
|
+
form, that is based on (or derived from) the Work and for which the
|
|
50
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
51
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
52
|
+
of this License, Derivative Works shall not include works that remain
|
|
53
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
54
|
+
the Work and Derivative Works thereof.
|
|
55
|
+
|
|
56
|
+
"Contribution" shall mean any work of authorship, including
|
|
57
|
+
the original version of the Work and any modifications or additions
|
|
58
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
59
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
60
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
61
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
62
|
+
means any form of electronic, verbal, or written communication sent
|
|
63
|
+
to the Licensor or its representatives, including but not limited to
|
|
64
|
+
communication on electronic mailing lists, source code control systems,
|
|
65
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
66
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
67
|
+
excluding communication that is conspicuously marked or otherwise
|
|
68
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
69
|
+
|
|
70
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
71
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
72
|
+
subsequently incorporated within the Work.
|
|
73
|
+
|
|
74
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
78
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
79
|
+
Work and such Derivative Works in Source or Object form.
|
|
80
|
+
|
|
81
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
82
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
83
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
84
|
+
(except as stated in this section) patent license to make, have made,
|
|
85
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
86
|
+
where such license applies only to those patent claims licensable
|
|
87
|
+
by such Contributor that are necessarily infringed by their
|
|
88
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
89
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
90
|
+
institute patent litigation against any entity (including a
|
|
91
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
92
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
93
|
+
or contributory patent infringement, then any patent licenses
|
|
94
|
+
granted to You under this License for that Work shall terminate
|
|
95
|
+
as of the date such litigation is filed.
|
|
96
|
+
|
|
97
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
98
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
99
|
+
modifications, and in Source or Object form, provided that You
|
|
100
|
+
meet the following conditions:
|
|
101
|
+
|
|
102
|
+
(a) You must give any other recipients of the Work or
|
|
103
|
+
Derivative Works a copy of this License; and
|
|
104
|
+
|
|
105
|
+
(b) You must cause any modified files to carry prominent notices
|
|
106
|
+
stating that You changed the files; and
|
|
107
|
+
|
|
108
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
109
|
+
that You distribute, all copyright, patent, trademark, and
|
|
110
|
+
attribution notices from the Source form of the Work,
|
|
111
|
+
excluding those notices that do not pertain to any part of
|
|
112
|
+
the Derivative Works; and
|
|
113
|
+
|
|
114
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
115
|
+
distribution, then any Derivative Works that You distribute must
|
|
116
|
+
include a readable copy of the attribution notices contained
|
|
117
|
+
within such NOTICE file, excluding those notices that do not
|
|
118
|
+
pertain to any part of the Derivative Works, in at least one
|
|
119
|
+
of the following places: within a NOTICE text file distributed
|
|
120
|
+
as part of the Derivative Works; within the Source form or
|
|
121
|
+
documentation, if provided along with the Derivative Works; or,
|
|
122
|
+
within a display generated by the Derivative Works, if and
|
|
123
|
+
wherever such third-party notices normally appear. The contents
|
|
124
|
+
of the NOTICE file are for informational purposes only and
|
|
125
|
+
do not modify the License. You may add Your own attribution
|
|
126
|
+
notices within Derivative Works that You distribute, alongside
|
|
127
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
128
|
+
that such additional attribution notices cannot be construed
|
|
129
|
+
as modifying the License.
|
|
130
|
+
|
|
131
|
+
You may add Your own copyright statement to Your modifications and
|
|
132
|
+
may provide additional or different license terms and conditions
|
|
133
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
134
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
135
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
136
|
+
the conditions stated in this License.
|
|
137
|
+
|
|
138
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
139
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
140
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
141
|
+
this License, without any additional terms or conditions.
|
|
142
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
143
|
+
the terms of any separate license agreement you may have executed
|
|
144
|
+
with Licensor regarding such Contributions.
|
|
145
|
+
|
|
146
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
147
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
148
|
+
except as required for reasonable and customary use in describing the
|
|
149
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
150
|
+
|
|
151
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
152
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
153
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
154
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
155
|
+
implied, including, without limitation, any warranties or conditions
|
|
156
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
157
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
158
|
+
appropriateness of using or redistributing the Work and assume any
|
|
159
|
+
risks associated with Your exercise of permissions under this License.
|
|
160
|
+
|
|
161
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
162
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
163
|
+
unless required by applicable law (such as deliberate and grossly
|
|
164
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
165
|
+
liable to You for damages, including any direct, indirect, special,
|
|
166
|
+
incidental, or consequential damages of any character arising as a
|
|
167
|
+
result of this License or out of the use or inability to use the
|
|
168
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
169
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
170
|
+
other commercial damages or losses), even if such Contributor
|
|
171
|
+
has been advised of the possibility of such damages.
|
|
172
|
+
|
|
173
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
174
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
175
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
176
|
+
or other liability obligations and/or rights consistent with this
|
|
177
|
+
License. However, in accepting such obligations, You may act only
|
|
178
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
179
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
180
|
+
defend, and hold each Contributor harmless for any liability
|
|
181
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
182
|
+
of your accepting any such warranty or additional liability.
|
|
183
|
+
|
|
184
|
+
END OF TERMS AND CONDITIONS
|
|
185
|
+
|
|
186
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
187
|
+
|
|
188
|
+
To apply the Apache License to your work, attach the following
|
|
189
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
190
|
+
replaced with your own identifying information. (Don't include
|
|
191
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
192
|
+
comment syntax for the file format. We also recommend that a
|
|
193
|
+
file or class name and description of purpose be included on the
|
|
194
|
+
same "printed page" as the copyright notice for easier
|
|
195
|
+
identification within third-party archives.
|
|
196
|
+
|
|
197
|
+
Copyright [yyyy] [name of copyright owner]
|
|
198
|
+
|
|
199
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
200
|
+
you may not use this file except in compliance with the License.
|
|
201
|
+
You may obtain a copy of the License at
|
|
202
|
+
|
|
203
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
204
|
+
|
|
205
|
+
Unless required by applicable law or agreed to in writing, software
|
|
206
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
207
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
208
|
+
See the License for the specific language governing permissions and
|
|
209
|
+
limitations under the License.
|
|
210
|
+
License-File: LICENSE
|
|
211
|
+
Keywords: ast,ast explainer,ast explorer,ast viewer
|
|
212
|
+
Classifier: Development Status :: 3 - Alpha
|
|
213
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
214
|
+
Classifier: Programming Language :: Python
|
|
215
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
216
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
217
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
218
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
219
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
220
|
+
Requires-Python: >=3.11
|
|
221
|
+
Description-Content-Type: text/markdown
|
|
222
|
+
|
|
223
|
+
# ast-explore
|
|
224
|
+
|
|
225
|
+
Tool for exploring the AST of given Python source code.
|
|
226
|
+
|
|
227
|
+
## Installation
|
|
228
|
+
|
|
229
|
+
This package is available on PyPI. To use it, you can either install with `pip`:
|
|
230
|
+
|
|
231
|
+
```shell
|
|
232
|
+
pip install ast-explore
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
or use it with `uv` directly:
|
|
236
|
+
|
|
237
|
+
```shell
|
|
238
|
+
uvx ast-explore --help
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
## Usage
|
|
242
|
+
|
|
243
|
+
At a minimum, you must provide a Python file to analyze:
|
|
244
|
+
|
|
245
|
+
```shell
|
|
246
|
+
# previously-installed with pip:
|
|
247
|
+
ast-explore source_code.py
|
|
248
|
+
|
|
249
|
+
# with uv:
|
|
250
|
+
uvx ast-explore source_code.py
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
This will extract all possible data points from every node in the AST generated from that source code and print it to the screen. You may wish to pipe the results to a file or `less` to process.
|
|
254
|
+
|
|
255
|
+
Here's a partial example of the output:
|
|
256
|
+
|
|
257
|
+
```
|
|
258
|
+
📖 Reading Python source code from /.../source_code.py...
|
|
259
|
+
🔍 Parsing into a Python 3.14 AST...
|
|
260
|
+
✅ Ready to explore the AST! Starting depth-first traversal...
|
|
261
|
+
|
|
262
|
+
********************************************************************************
|
|
263
|
+
** 1. Module (https://docs.python.org/3.14/library/ast.html#ast.Module) **
|
|
264
|
+
********************************************************************************
|
|
265
|
+
|
|
266
|
+
🌲 Path to this node from the root node of the AST:
|
|
267
|
+
Module
|
|
268
|
+
|
|
269
|
+
📍 This node type does not have any line number information.
|
|
270
|
+
|
|
271
|
+
📝 Docstring is missing.
|
|
272
|
+
|
|
273
|
+
✨ AST node-specific fields and their values:
|
|
274
|
+
- body : [FunctionDef(n...ype_params=[])]
|
|
275
|
+
- type_ignores : []
|
|
276
|
+
|
|
277
|
+
********************************************************************************
|
|
278
|
+
2. FunctionDef (https://docs.python.org/3.14/library/ast.html#ast.FunctionDef)
|
|
279
|
+
********************************************************************************
|
|
280
|
+
|
|
281
|
+
🌲 Path to this node from the root node of the AST:
|
|
282
|
+
Module -> FunctionDef
|
|
283
|
+
|
|
284
|
+
🐍 Source code represented by the node:
|
|
285
|
+
1 | def strip_password(x: dict[str, str]) -> None:
|
|
286
|
+
2 | try:
|
|
287
|
+
3 | del x['password']
|
|
288
|
+
4 | except KeyError:
|
|
289
|
+
5 | pass
|
|
290
|
+
|
|
291
|
+
📍 Location in the source code:
|
|
292
|
+
- lineno : 1
|
|
293
|
+
- end_lineno : 5
|
|
294
|
+
- col_offset : 0
|
|
295
|
+
- end_col_offset : 12
|
|
296
|
+
|
|
297
|
+
📝 Docstring is missing.
|
|
298
|
+
|
|
299
|
+
✨ AST node-specific fields and their values:
|
|
300
|
+
- name : 'strip_password'
|
|
301
|
+
- args : arguments(pos..., defaults=[])
|
|
302
|
+
- body : [Try(body=[Del... finalbody=[])]
|
|
303
|
+
- decorator_list : []
|
|
304
|
+
- returns : Constant(value...ne, kind=None)
|
|
305
|
+
- type_comment : None
|
|
306
|
+
- type_params : []
|
|
307
|
+
|
|
308
|
+
********************************************************************************
|
|
309
|
+
3. arguments (https://docs.python.org/3.14/library/ast.html#ast.arguments)
|
|
310
|
+
********************************************************************************
|
|
311
|
+
|
|
312
|
+
🌲 Path to this node from the root node of the AST:
|
|
313
|
+
Module -> FunctionDef -> arguments
|
|
314
|
+
|
|
315
|
+
📍 This node type does not have any line number information.
|
|
316
|
+
|
|
317
|
+
✨ AST node-specific fields and their values:
|
|
318
|
+
- posonlyargs : []
|
|
319
|
+
- args : [arg(arg='x', ..._comment=None)]
|
|
320
|
+
- vararg : None
|
|
321
|
+
- kwonlyargs : []
|
|
322
|
+
- kw_defaults : []
|
|
323
|
+
- kwarg : None
|
|
324
|
+
- defaults : []
|
|
325
|
+
|
|
326
|
+
********************************************************************************
|
|
327
|
+
** 4. arg (https://docs.python.org/3.14/library/ast.html#ast.arg) **
|
|
328
|
+
********************************************************************************
|
|
329
|
+
|
|
330
|
+
🌲 Path to this node from the root node of the AST:
|
|
331
|
+
Module -> FunctionDef -> arguments -> arg
|
|
332
|
+
|
|
333
|
+
🐍 Source code represented by the node:
|
|
334
|
+
1 | def strip_password(x: dict[str, str]) -> None:
|
|
335
|
+
| ^^^^^^^^^^^^^^^^^
|
|
336
|
+
|
|
337
|
+
📍 Location in the source code:
|
|
338
|
+
- lineno : 1
|
|
339
|
+
- end_lineno : 1
|
|
340
|
+
- col_offset : 19
|
|
341
|
+
- end_col_offset : 36
|
|
342
|
+
|
|
343
|
+
✨ AST node-specific fields and their values:
|
|
344
|
+
- arg : 'x'
|
|
345
|
+
- annotation : Subscript(val...), ctx=Load())
|
|
346
|
+
- type_comment : None
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
### Specifying nodes of interest
|
|
350
|
+
|
|
351
|
+
Use the `--types` argument to list the nodes you want to explore and only information about those nodes will be shown. Here, we only care about function definition nodes:
|
|
352
|
+
|
|
353
|
+
```shell
|
|
354
|
+
# previously-installed with pip:
|
|
355
|
+
ast-explore source_code.py --types FunctionDef AsyncFunctionDef
|
|
356
|
+
|
|
357
|
+
# with uv:
|
|
358
|
+
uvx ast-explore source_code.py --types FunctionDef AsyncFunctionDef
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
Here's an example of the result (there are no async functions in the file analyzed):
|
|
362
|
+
|
|
363
|
+
```
|
|
364
|
+
📖 Reading Python source code from /.../source-code.py...
|
|
365
|
+
🔍 Parsing into a Python 3.14 AST...
|
|
366
|
+
✅ Ready to explore the AST! Starting depth-first traversal...
|
|
367
|
+
|
|
368
|
+
********************************************************************************
|
|
369
|
+
1. FunctionDef (https://docs.python.org/3.14/library/ast.html#ast.FunctionDef)
|
|
370
|
+
********************************************************************************
|
|
371
|
+
|
|
372
|
+
🌲 Path to this node from the root node of the AST:
|
|
373
|
+
Module -> FunctionDef
|
|
374
|
+
|
|
375
|
+
🐍 Source code represented by the node:
|
|
376
|
+
1 | def strip_password(x: dict[str, str]) -> None:
|
|
377
|
+
2 | try:
|
|
378
|
+
3 | del x['password']
|
|
379
|
+
4 | except KeyError:
|
|
380
|
+
5 | pass
|
|
381
|
+
|
|
382
|
+
📍 Location in the source code:
|
|
383
|
+
- lineno : 1
|
|
384
|
+
- end_lineno : 5
|
|
385
|
+
- col_offset : 0
|
|
386
|
+
- end_col_offset : 12
|
|
387
|
+
|
|
388
|
+
📝 Docstring is missing.
|
|
389
|
+
|
|
390
|
+
✨ AST node-specific fields and their values:
|
|
391
|
+
- name : 'strip_password'
|
|
392
|
+
- args : arguments(pos..., defaults=[])
|
|
393
|
+
- body : [Try(body=[Del... finalbody=[])]
|
|
394
|
+
- decorator_list : []
|
|
395
|
+
- returns : Constant(value...ne, kind=None)
|
|
396
|
+
- type_comment : None
|
|
397
|
+
- type_params : []
|
|
398
|
+
|
|
399
|
+
********************************************************************************
|
|
400
|
+
|
|
401
|
+
🏆 Traversal completed!
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
### Interactive mode
|
|
405
|
+
|
|
406
|
+
Use interactive mode to step through the AST one node at a time:
|
|
407
|
+
|
|
408
|
+
```shell
|
|
409
|
+
# previously-installed with pip:
|
|
410
|
+
ast-explore source_code.py --interactive
|
|
411
|
+
|
|
412
|
+
# with uv:
|
|
413
|
+
uvx ast-explore source_code.py --interactive
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
Here's what that looks like:
|
|
417
|
+
|
|
418
|
+
```
|
|
419
|
+
📖 Reading Python source code from /.../source_code.py...
|
|
420
|
+
🔍 Parsing into a Python 3.14 AST...
|
|
421
|
+
✅ Ready to explore the AST! Starting depth-first traversal...
|
|
422
|
+
|
|
423
|
+
********************************************************************************
|
|
424
|
+
** 1. Module (https://docs.python.org/3.14/library/ast.html#ast.Module) **
|
|
425
|
+
********************************************************************************
|
|
426
|
+
|
|
427
|
+
🌲 Path to this node from the root node of the AST:
|
|
428
|
+
Module
|
|
429
|
+
|
|
430
|
+
📍 This node type does not have any line number information.
|
|
431
|
+
|
|
432
|
+
❓ Do you want more information on this node? [y]es [n]o [q]uit:
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
Of course, you can combine `--interactive` with `--types` to customize your exploration. Here we interactively visit all `try` blocks:
|
|
436
|
+
|
|
437
|
+
```shell
|
|
438
|
+
# previously-installed with pip:
|
|
439
|
+
ast-explore source_code.py --interactive --types Try
|
|
440
|
+
|
|
441
|
+
# with uv:
|
|
442
|
+
uvx ast-explore source_code.py --interactive --types Try
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
Note that the first node we encounter is now the `ast.Try` node and not the `ast.Module` node we saw without specifying `--types`:
|
|
446
|
+
|
|
447
|
+
```
|
|
448
|
+
📖 Reading Python source code from /.../source_code.py...
|
|
449
|
+
🔍 Parsing into a Python 3.14 AST...
|
|
450
|
+
✅ Ready to explore the AST! Starting depth-first traversal...
|
|
451
|
+
|
|
452
|
+
********************************************************************************
|
|
453
|
+
** 1. Try (https://docs.python.org/3.14/library/ast.html#ast.Try) **
|
|
454
|
+
********************************************************************************
|
|
455
|
+
|
|
456
|
+
🌲 Path to this node from the root node of the AST:
|
|
457
|
+
Module -> FunctionDef -> Try
|
|
458
|
+
|
|
459
|
+
🐍 Source code represented by the node:
|
|
460
|
+
2 | try:
|
|
461
|
+
3 | del x['password']
|
|
462
|
+
4 | except KeyError:
|
|
463
|
+
5 | pass
|
|
464
|
+
|
|
465
|
+
❓ Show location fields? [y]es [n]o [q]uit: y
|
|
466
|
+
|
|
467
|
+
📍 Location in the source code:
|
|
468
|
+
- lineno : 2
|
|
469
|
+
- end_lineno : 5
|
|
470
|
+
- col_offset : 4
|
|
471
|
+
- end_col_offset : 12
|
|
472
|
+
|
|
473
|
+
❓ Do you want more information on this node? [y]es [n]o [q]uit: y
|
|
474
|
+
|
|
475
|
+
✨ AST node-specific fields and their values:
|
|
476
|
+
- body : [Delete(target..., ctx=Del())])]
|
|
477
|
+
- handlers : [ExceptHandler...body=[Pass()])]
|
|
478
|
+
- orelse : []
|
|
479
|
+
- finalbody : []
|
|
480
|
+
|
|
481
|
+
❓ Continue traversal? [y]es [n]o [q]uit:
|
|
482
|
+
```
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
ast_explore/__init__.py,sha256=Y7ZwoeiSsIeRE5DONj16amYHASjgp2sxFutyD7CaW4s,1262
|
|
2
|
+
ast_explore/cli.py,sha256=5d5I98tx_mthgEVMp7k9ohHGt9azL9oCJF9YiwWdM8I,3221
|
|
3
|
+
ast_explore/display.py,sha256=S7xi2rbCc9ynLfEmpfJx0kw6AGFrFnvPBRzu1OlAc9M,3421
|
|
4
|
+
ast_explore/explorer.py,sha256=i5sh28hZKbvFImN8ZaEcGg9p8UKXbEmpiLC5jWywxSc,8710
|
|
5
|
+
ast_explore-0.1.0.dist-info/METADATA,sha256=oQyODg7pkcWs17DasdN533EGZDYi4hcmH78xsYQyR-c,21892
|
|
6
|
+
ast_explore-0.1.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
|
|
7
|
+
ast_explore-0.1.0.dist-info/entry_points.txt,sha256=ALv810QHY9nNXMNkVxcDeSXkLp15rDWoU57lXJ3iUz4,53
|
|
8
|
+
ast_explore-0.1.0.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
9
|
+
ast_explore-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|