inspectlab 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.
- inspectlab/__init__.py +0 -0
- inspectlab/ast_sample.py +75 -0
- inspectlab/cmds/base.py +28 -0
- inspectlab/cmds/classes.py +149 -0
- inspectlab/cmds/functions.py +227 -0
- inspectlab/cmds/imports.py +97 -0
- inspectlab/cmds/test.py +11 -0
- inspectlab/cmds/tree.py +5 -0
- inspectlab/main.py +50 -0
- inspectlab-0.1.0.dist-info/METADATA +287 -0
- inspectlab-0.1.0.dist-info/RECORD +14 -0
- inspectlab-0.1.0.dist-info/WHEEL +5 -0
- inspectlab-0.1.0.dist-info/entry_points.txt +2 -0
- inspectlab-0.1.0.dist-info/top_level.txt +1 -0
inspectlab/__init__.py
ADDED
|
File without changes
|
inspectlab/ast_sample.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import sys as system
|
|
3
|
+
import json
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from collections import defaultdict as dd
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def add(a: int, b: int) -> int:
|
|
10
|
+
"""Add two integers."""
|
|
11
|
+
return a + b
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def greet(name, /, greeting: str):
|
|
15
|
+
return f"{greeting}, {name}"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def generator(limit: int):
|
|
19
|
+
"""Yield numbers up to limit."""
|
|
20
|
+
for number in range(limit):
|
|
21
|
+
if number == 5:
|
|
22
|
+
return
|
|
23
|
+
|
|
24
|
+
yield number
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
async def fetch_user(user_id: int) -> dict:
|
|
28
|
+
return {
|
|
29
|
+
"id": user_id,
|
|
30
|
+
"name": "Test User"
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@staticmethod
|
|
35
|
+
def multiply(a: float, b: float) -> float:
|
|
36
|
+
return a * b
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class User:
|
|
40
|
+
"""Represents a normal user."""
|
|
41
|
+
|
|
42
|
+
role = "member"
|
|
43
|
+
|
|
44
|
+
def __init__(self, name: str):
|
|
45
|
+
self.name = name
|
|
46
|
+
|
|
47
|
+
def greet(self) -> str:
|
|
48
|
+
return f"Hello {self.name}"
|
|
49
|
+
|
|
50
|
+
async def save(self) -> None:
|
|
51
|
+
return None
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class Admin(User):
|
|
55
|
+
"""Represents an administrator."""
|
|
56
|
+
|
|
57
|
+
level = 10
|
|
58
|
+
|
|
59
|
+
@classmethod
|
|
60
|
+
def create(cls, name: str):
|
|
61
|
+
return cls(name)
|
|
62
|
+
|
|
63
|
+
@staticmethod
|
|
64
|
+
def validate(value: int) -> bool:
|
|
65
|
+
return value > 0
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class Config(metaclass=type):
|
|
69
|
+
"""Configuration class."""
|
|
70
|
+
|
|
71
|
+
enabled = True
|
|
72
|
+
|
|
73
|
+
@property
|
|
74
|
+
def status(self) -> str:
|
|
75
|
+
return "enabled"
|
inspectlab/cmds/base.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
import ast
|
|
3
|
+
|
|
4
|
+
def find_file(file_name: str):
|
|
5
|
+
try:
|
|
6
|
+
path = Path.cwd()
|
|
7
|
+
file = next(path.rglob(file_name))
|
|
8
|
+
return file.read_text()
|
|
9
|
+
except StopIteration:
|
|
10
|
+
raise FileNotFoundError(f'File not found: {file_name}')
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def create_ast(code: str):
|
|
14
|
+
code = ast.parse(code)
|
|
15
|
+
return code
|
|
16
|
+
|
|
17
|
+
def view_tree(file: str):
|
|
18
|
+
code = find_file(file)
|
|
19
|
+
tree = create_ast(code)
|
|
20
|
+
return ast.dump(tree, indent=4)
|
|
21
|
+
|
|
22
|
+
def create_main(file: str):
|
|
23
|
+
main = find_file(file)
|
|
24
|
+
tree = create_ast(main)
|
|
25
|
+
return tree
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import ast
|
|
2
|
+
from .functions import Function
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class ClassInspect(ast.NodeVisitor):
|
|
6
|
+
|
|
7
|
+
def __init__(self):
|
|
8
|
+
self.classes = []
|
|
9
|
+
|
|
10
|
+
def visit_ClassDef(self, node):
|
|
11
|
+
info = self.inspect(node)
|
|
12
|
+
self.classes.append(info)
|
|
13
|
+
self.generic_visit(node)
|
|
14
|
+
|
|
15
|
+
def inspect(self, node):
|
|
16
|
+
|
|
17
|
+
info = {
|
|
18
|
+
"Name": self.get_name(node),
|
|
19
|
+
"Inherited Classes": self.get_inher(node),
|
|
20
|
+
"Metaclass": self.get_metaclass(node),
|
|
21
|
+
"Decorators": self.get_decorators(node),
|
|
22
|
+
"Docstring": self.get_docs(node),
|
|
23
|
+
"Lines": self.get_lines(node),
|
|
24
|
+
"All Method and Info": self.get_methods(node),
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return info
|
|
28
|
+
|
|
29
|
+
def get_name(self, node):
|
|
30
|
+
return node.name
|
|
31
|
+
|
|
32
|
+
def get_inher(self, node):
|
|
33
|
+
class_inherited = None
|
|
34
|
+
if node.bases:
|
|
35
|
+
class_inherited = [ast.unparse(base) for base in node.bases]
|
|
36
|
+
return class_inherited
|
|
37
|
+
|
|
38
|
+
return class_inherited
|
|
39
|
+
|
|
40
|
+
def get_metaclass(self, node):
|
|
41
|
+
for keyword in node.keywords:
|
|
42
|
+
if keyword.arg == "metaclass":
|
|
43
|
+
return ast.unparse(keyword.value)
|
|
44
|
+
|
|
45
|
+
return None
|
|
46
|
+
|
|
47
|
+
def get_decorators(self, node):
|
|
48
|
+
decorators = None
|
|
49
|
+
if node.decorator_list:
|
|
50
|
+
decorators = [ast.unparse(decorator) for decorator in node.decorator_list]
|
|
51
|
+
return decorators
|
|
52
|
+
return decorators
|
|
53
|
+
|
|
54
|
+
def get_lines(self, node):
|
|
55
|
+
lines = {'Start of Class': node.lineno, 'End of Class': node.end_lineno}
|
|
56
|
+
return lines
|
|
57
|
+
|
|
58
|
+
def get_methods(self, node):
|
|
59
|
+
methods = []
|
|
60
|
+
function_inspector = Function()
|
|
61
|
+
|
|
62
|
+
for child in node.body:
|
|
63
|
+
if isinstance(child, ast.FunctionDef):
|
|
64
|
+
methods.append(
|
|
65
|
+
function_inspector.inspect_function(child, is_async=False)
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
elif isinstance(child, ast.AsyncFunctionDef):
|
|
69
|
+
methods.append(
|
|
70
|
+
function_inspector.inspect_function(child, is_async=True)
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
return methods
|
|
74
|
+
def get_docs(self, node):
|
|
75
|
+
return ast.get_docstring(node)
|
|
76
|
+
|
|
77
|
+
def __str__(self):
|
|
78
|
+
output = []
|
|
79
|
+
|
|
80
|
+
for class_info in self.classes:
|
|
81
|
+
output.append(f"Class: {class_info['Name']}")
|
|
82
|
+
|
|
83
|
+
inherited = class_info["Inherited Classes"]
|
|
84
|
+
if inherited:
|
|
85
|
+
output.append(f"Inherits: {', '.join(inherited)}")
|
|
86
|
+
else:
|
|
87
|
+
output.append("Inherits: None")
|
|
88
|
+
|
|
89
|
+
output.append(f"Metaclass: {class_info['Metaclass']}")
|
|
90
|
+
|
|
91
|
+
decorators = class_info["Decorators"]
|
|
92
|
+
if decorators:
|
|
93
|
+
output.append(f"Decorators: {', '.join(decorators)}")
|
|
94
|
+
else:
|
|
95
|
+
output.append("Decorators: None")
|
|
96
|
+
|
|
97
|
+
output.append(f"Docstring: {class_info['Docstring']}")
|
|
98
|
+
|
|
99
|
+
lines = class_info["Lines"]
|
|
100
|
+
output.append(
|
|
101
|
+
f"Lines: {lines['Start of Class']} - {lines['End of Class']}"
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
output.append("Methods:")
|
|
105
|
+
|
|
106
|
+
methods = class_info["All Method and Info"]
|
|
107
|
+
|
|
108
|
+
if methods:
|
|
109
|
+
for method in methods:
|
|
110
|
+
output.append(f" Name: {method['Name']}")
|
|
111
|
+
output.append(f" Type: {method['Type']}")
|
|
112
|
+
|
|
113
|
+
output.append(" Arguments:")
|
|
114
|
+
|
|
115
|
+
args = method["Arguments"]
|
|
116
|
+
|
|
117
|
+
if args:
|
|
118
|
+
for arg in args:
|
|
119
|
+
name = arg["Name"]
|
|
120
|
+
arg_type = arg["Argument Type"]
|
|
121
|
+
data_type = arg.get("Data Type")
|
|
122
|
+
|
|
123
|
+
if data_type:
|
|
124
|
+
output.append(
|
|
125
|
+
f" - {name}: {data_type} [{arg_type}]"
|
|
126
|
+
)
|
|
127
|
+
else:
|
|
128
|
+
output.append(
|
|
129
|
+
f" - {name} [{arg_type}]"
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
else:
|
|
133
|
+
output.append(" None")
|
|
134
|
+
|
|
135
|
+
output.append(f" Returns: {method['Returns']}")
|
|
136
|
+
output.append(
|
|
137
|
+
f" Return Data Type: {method['Return Data Type']}"
|
|
138
|
+
)
|
|
139
|
+
output.append(f" Decorators: {method['Decorators']}")
|
|
140
|
+
output.append(f" Docstring: {method['Docstring']}")
|
|
141
|
+
output.append(f" Lines: {method['Lines']}")
|
|
142
|
+
output.append("")
|
|
143
|
+
|
|
144
|
+
else:
|
|
145
|
+
output.append(" None")
|
|
146
|
+
|
|
147
|
+
output.append("-" * 40)
|
|
148
|
+
|
|
149
|
+
return "\n".join(output)
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import ast
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Function(ast.NodeVisitor):
|
|
5
|
+
|
|
6
|
+
def __init__(self):
|
|
7
|
+
self.functions = []
|
|
8
|
+
|
|
9
|
+
def visit_FunctionDef(self, node):
|
|
10
|
+
info = self.inspect_function(node, is_async=False)
|
|
11
|
+
self.functions.append(info)
|
|
12
|
+
|
|
13
|
+
self.generic_visit(node)
|
|
14
|
+
|
|
15
|
+
def visit_AsyncFunctionDef(self, node):
|
|
16
|
+
info = self.inspect_function(node, is_async=True)
|
|
17
|
+
self.functions.append(info)
|
|
18
|
+
|
|
19
|
+
self.generic_visit(node)
|
|
20
|
+
|
|
21
|
+
def inspect_function(self, node, is_async):
|
|
22
|
+
function_type = self.get_function_type(node, is_async)
|
|
23
|
+
|
|
24
|
+
info = {
|
|
25
|
+
"Name": node.name,
|
|
26
|
+
"Type": function_type,
|
|
27
|
+
"Arguments": self.get_args(node),
|
|
28
|
+
"Returns": self.get_return(node),
|
|
29
|
+
"Return Data Type": self.get_return_annotation(node),
|
|
30
|
+
"Decorators": self.get_decors(node),
|
|
31
|
+
"Docstring": self.get_docs(node),
|
|
32
|
+
"Lines": self.get_lines(node),
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return info
|
|
36
|
+
|
|
37
|
+
def get_args(self, node):
|
|
38
|
+
args = []
|
|
39
|
+
|
|
40
|
+
for arg in node.args.posonlyargs:
|
|
41
|
+
info = {
|
|
42
|
+
"Argument Type": "Positional Only",
|
|
43
|
+
"Name": arg.arg
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if arg.annotation is not None:
|
|
47
|
+
info["Data Type"] = ast.unparse(arg.annotation)
|
|
48
|
+
|
|
49
|
+
args.append(info)
|
|
50
|
+
|
|
51
|
+
for arg in node.args.args:
|
|
52
|
+
info = {
|
|
53
|
+
"Argument Type": "Normal",
|
|
54
|
+
"Name": arg.arg
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if arg.annotation is not None:
|
|
58
|
+
info["Data Type"] = ast.unparse(arg.annotation)
|
|
59
|
+
|
|
60
|
+
args.append(info)
|
|
61
|
+
|
|
62
|
+
if node.args.vararg is not None:
|
|
63
|
+
info = {
|
|
64
|
+
"Argument Type": "Args Argument",
|
|
65
|
+
"Name": node.args.vararg.arg
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if node.args.vararg.annotation is not None:
|
|
69
|
+
info["Data Type"] = ast.unparse(
|
|
70
|
+
node.args.vararg.annotation
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
args.append(info)
|
|
74
|
+
|
|
75
|
+
for arg in node.args.kwonlyargs:
|
|
76
|
+
info = {
|
|
77
|
+
"Argument Type": "Keyword Only",
|
|
78
|
+
"Name": arg.arg
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if arg.annotation is not None:
|
|
82
|
+
info["Data Type"] = ast.unparse(arg.annotation)
|
|
83
|
+
|
|
84
|
+
args.append(info)
|
|
85
|
+
|
|
86
|
+
if node.args.kwarg is not None:
|
|
87
|
+
info = {
|
|
88
|
+
"Argument Type": "Kwargs Argument",
|
|
89
|
+
"Name": node.args.kwarg.arg
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if node.args.kwarg.annotation is not None:
|
|
93
|
+
info["Data Type"] = ast.unparse(
|
|
94
|
+
node.args.kwarg.annotation
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
args.append(info)
|
|
98
|
+
|
|
99
|
+
return args
|
|
100
|
+
|
|
101
|
+
def get_function_type(self, node, is_async):
|
|
102
|
+
has_return = False
|
|
103
|
+
has_yield = False
|
|
104
|
+
|
|
105
|
+
for child in ast.walk(node):
|
|
106
|
+
|
|
107
|
+
if isinstance(child, ast.Return):
|
|
108
|
+
has_return = True
|
|
109
|
+
|
|
110
|
+
elif isinstance(child, (ast.Yield, ast.YieldFrom)):
|
|
111
|
+
has_yield = True
|
|
112
|
+
|
|
113
|
+
if is_async:
|
|
114
|
+
if has_yield and has_return:
|
|
115
|
+
return "Async Generator with Return"
|
|
116
|
+
|
|
117
|
+
if has_yield:
|
|
118
|
+
return "Async Generator"
|
|
119
|
+
|
|
120
|
+
if has_return:
|
|
121
|
+
return "Async Function with Return"
|
|
122
|
+
|
|
123
|
+
return "Async Function"
|
|
124
|
+
|
|
125
|
+
else:
|
|
126
|
+
if has_yield and has_return:
|
|
127
|
+
return "Generator with Return"
|
|
128
|
+
if has_yield:
|
|
129
|
+
return "Generator"
|
|
130
|
+
if has_return:
|
|
131
|
+
return "Function with Return"
|
|
132
|
+
return "Function"
|
|
133
|
+
|
|
134
|
+
def get_return(self, node):
|
|
135
|
+
return_info = []
|
|
136
|
+
|
|
137
|
+
for child in ast.walk(node):
|
|
138
|
+
if isinstance(child, ast.Return):
|
|
139
|
+
if child.value is not None:
|
|
140
|
+
return_info.append({
|
|
141
|
+
"Return": ast.unparse(child.value)
|
|
142
|
+
})
|
|
143
|
+
else:
|
|
144
|
+
return_info.append({
|
|
145
|
+
"Return": None
|
|
146
|
+
})
|
|
147
|
+
elif isinstance(child, (ast.Yield, ast.YieldFrom)):
|
|
148
|
+
if child.value is not None:
|
|
149
|
+
return_info.append({
|
|
150
|
+
"Yield": ast.unparse(child.value)
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
else:
|
|
154
|
+
return_info.append({
|
|
155
|
+
"Yield": None
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
return return_info
|
|
159
|
+
|
|
160
|
+
def get_return_annotation(self, node):
|
|
161
|
+
if node.returns is not None:
|
|
162
|
+
return ast.unparse(node.returns)
|
|
163
|
+
|
|
164
|
+
return None
|
|
165
|
+
|
|
166
|
+
def get_decors(self, node):
|
|
167
|
+
decorators = []
|
|
168
|
+
|
|
169
|
+
for decorator in node.decorator_list:
|
|
170
|
+
decorators.append(ast.unparse(decorator))
|
|
171
|
+
|
|
172
|
+
return decorators
|
|
173
|
+
|
|
174
|
+
def get_docs(self, node):
|
|
175
|
+
return ast.get_docstring(node)
|
|
176
|
+
|
|
177
|
+
def get_lines(self, node):
|
|
178
|
+
return {
|
|
179
|
+
"Start": node.lineno,
|
|
180
|
+
"End": node.end_lineno
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
def format_argument(self, arg):
|
|
184
|
+
name = arg["Name"]
|
|
185
|
+
arg_type = arg["Argument Type"]
|
|
186
|
+
data_type = arg.get("Data Type")
|
|
187
|
+
|
|
188
|
+
if data_type:
|
|
189
|
+
return f"{name}: {data_type} [{arg_type}]"
|
|
190
|
+
|
|
191
|
+
return f"{name} [{arg_type}]"
|
|
192
|
+
|
|
193
|
+
def __str__(self):
|
|
194
|
+
output = []
|
|
195
|
+
|
|
196
|
+
for function in self.functions:
|
|
197
|
+
output.append(f"Name: {function['Name']}")
|
|
198
|
+
output.append(f"Type: {function['Type']}")
|
|
199
|
+
|
|
200
|
+
output.append("Arguments:")
|
|
201
|
+
|
|
202
|
+
args = function["Arguments"]
|
|
203
|
+
|
|
204
|
+
if args:
|
|
205
|
+
for arg in args:
|
|
206
|
+
output.append(
|
|
207
|
+
f" - {self.format_argument(arg)}"
|
|
208
|
+
)
|
|
209
|
+
else:
|
|
210
|
+
output.append(" None")
|
|
211
|
+
|
|
212
|
+
output.append(f"Returns: {function['Returns']}")
|
|
213
|
+
output.append(
|
|
214
|
+
f"Return Data Type: {function['Return Data Type']}"
|
|
215
|
+
)
|
|
216
|
+
output.append(f"Decorators: {function['Decorators']}")
|
|
217
|
+
output.append(f"Docstring: {function['Docstring']}")
|
|
218
|
+
output.append(f"Lines: {function['Lines']}")
|
|
219
|
+
output.append("")
|
|
220
|
+
|
|
221
|
+
return "\n".join(output)
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import ast
|
|
2
|
+
|
|
3
|
+
class Imports(ast.NodeVisitor):
|
|
4
|
+
|
|
5
|
+
def __init__(self):
|
|
6
|
+
|
|
7
|
+
self.imports = []
|
|
8
|
+
|
|
9
|
+
def visit_Import(self, node):
|
|
10
|
+
info = self.import_inspect(node)
|
|
11
|
+
self.imports.append(info)
|
|
12
|
+
|
|
13
|
+
self.generic_visit(node)
|
|
14
|
+
|
|
15
|
+
def visit_ImportFrom(self, node):
|
|
16
|
+
info = self.import_inspect(node)
|
|
17
|
+
self.imports.append(info)
|
|
18
|
+
|
|
19
|
+
self.generic_visit(node)
|
|
20
|
+
|
|
21
|
+
def import_inspect(self, node):
|
|
22
|
+
if isinstance(node, ast.Import):
|
|
23
|
+
info = {
|
|
24
|
+
"Type": "Import",
|
|
25
|
+
"Imports": self.get_imports(node),
|
|
26
|
+
"Line": node.lineno
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
elif isinstance(node, ast.ImportFrom):
|
|
30
|
+
info = {
|
|
31
|
+
"Type": "ImportFrom",
|
|
32
|
+
"Module": node.module,
|
|
33
|
+
"Level": node.level,
|
|
34
|
+
"Imports": [
|
|
35
|
+
{
|
|
36
|
+
"Name": alias.name,
|
|
37
|
+
"Alias": alias.asname
|
|
38
|
+
}
|
|
39
|
+
for alias in node.names
|
|
40
|
+
],
|
|
41
|
+
"Line": node.lineno
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return info
|
|
45
|
+
|
|
46
|
+
def get_imports(self, node):
|
|
47
|
+
imports = [{aliases.name: aliases.asname} for aliases in node.names]
|
|
48
|
+
return imports
|
|
49
|
+
|
|
50
|
+
def get_importfrom(self, node):
|
|
51
|
+
try:
|
|
52
|
+
imports = []
|
|
53
|
+
imports.append({node.module: None})
|
|
54
|
+
moduled_imp = []
|
|
55
|
+
|
|
56
|
+
for alias in node.names:
|
|
57
|
+
moduled_imp.append({alias.name: alias.asname})
|
|
58
|
+
|
|
59
|
+
imports[0][node.module] = moduled_imp
|
|
60
|
+
return imports
|
|
61
|
+
except:
|
|
62
|
+
return "Sorry Edge Cases are not added"
|
|
63
|
+
|
|
64
|
+
def __str__(self):
|
|
65
|
+
output = []
|
|
66
|
+
|
|
67
|
+
for import_info in self.imports:
|
|
68
|
+
output.append(f"Type: {import_info['Type']}")
|
|
69
|
+
output.append(f"Line: {import_info['Line']}")
|
|
70
|
+
|
|
71
|
+
if import_info["Type"] == "Import":
|
|
72
|
+
output.append("Imports:")
|
|
73
|
+
|
|
74
|
+
for item in import_info["Imports"]:
|
|
75
|
+
for name, alias in item.items():
|
|
76
|
+
if alias:
|
|
77
|
+
output.append(f" - {name} as {alias}")
|
|
78
|
+
else:
|
|
79
|
+
output.append(f" - {name}")
|
|
80
|
+
|
|
81
|
+
elif import_info["Type"] == "ImportFrom":
|
|
82
|
+
output.append(f"Module: {import_info['Module']}")
|
|
83
|
+
output.append("Imports:")
|
|
84
|
+
|
|
85
|
+
for item in import_info["Imports"]:
|
|
86
|
+
name = item["Name"]
|
|
87
|
+
alias = item["Alias"]
|
|
88
|
+
|
|
89
|
+
if alias:
|
|
90
|
+
output.append(f" - {name} as {alias}")
|
|
91
|
+
else:
|
|
92
|
+
output.append(f" - {name}")
|
|
93
|
+
|
|
94
|
+
output.append("")
|
|
95
|
+
|
|
96
|
+
return "\n".join(output)
|
|
97
|
+
|
inspectlab/cmds/test.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from base import find_file, create_ast, view_tree, create_main
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
import ast
|
|
4
|
+
from functions import *
|
|
5
|
+
from classes import *
|
|
6
|
+
from imports import *
|
|
7
|
+
|
|
8
|
+
tree = create_main('ast_sample.py')
|
|
9
|
+
finder = Imports()
|
|
10
|
+
finder.visit(tree)
|
|
11
|
+
print(finder)
|
inspectlab/cmds/tree.py
ADDED
inspectlab/main.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import typer
|
|
2
|
+
from Cmds.functions import *
|
|
3
|
+
from Cmds.classes import *
|
|
4
|
+
from Cmds.imports import *
|
|
5
|
+
from Cmds.tree import *
|
|
6
|
+
from Cmds.base import *
|
|
7
|
+
|
|
8
|
+
app = typer.Typer()
|
|
9
|
+
|
|
10
|
+
@app.command()
|
|
11
|
+
def functions(file: str):
|
|
12
|
+
try:
|
|
13
|
+
tree = create_main(file)
|
|
14
|
+
finder = Function()
|
|
15
|
+
finder.visit(tree)
|
|
16
|
+
print(finder)
|
|
17
|
+
except:
|
|
18
|
+
print('Wrong file name or typed out command')
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@app.command()
|
|
22
|
+
def classes(file: str):
|
|
23
|
+
try:
|
|
24
|
+
tree = create_main(file)
|
|
25
|
+
finder = ClassInspect()
|
|
26
|
+
finder.visit(tree)
|
|
27
|
+
print(finder)
|
|
28
|
+
except:
|
|
29
|
+
print('Wrong file name or typed out command')
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@app.command()
|
|
33
|
+
def imports(file: str):
|
|
34
|
+
try:
|
|
35
|
+
tree = create_main(file)
|
|
36
|
+
finder = Imports()
|
|
37
|
+
finder.visit(tree)
|
|
38
|
+
print(finder)
|
|
39
|
+
except:
|
|
40
|
+
print('Wrong file name or typed out command')
|
|
41
|
+
|
|
42
|
+
@app.command()
|
|
43
|
+
def tree(file: str):
|
|
44
|
+
try:
|
|
45
|
+
view(file)
|
|
46
|
+
except:
|
|
47
|
+
print('Wrong file name or typed out command')
|
|
48
|
+
|
|
49
|
+
if __name__ == "__main__":
|
|
50
|
+
app()
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: inspectlab
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A CLI tool for inspecting Python source code using AST
|
|
5
|
+
Requires-Python: >=3.10
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
Requires-Dist: typer
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<img src="logo.png" alt="InspectLab Logo" width="220">
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
# InspectLab
|
|
14
|
+
|
|
15
|
+
InspectLab is a lightweight Python CLI for inspecting Python source code directly from the terminal.
|
|
16
|
+
|
|
17
|
+
It uses Python's built-in `ast` module to analyze source files without executing them. InspectLab can quickly show functions, classes, imports, and the full AST structure of a file.
|
|
18
|
+
|
|
19
|
+
## Features
|
|
20
|
+
|
|
21
|
+
* Inspect functions and async functions
|
|
22
|
+
* Detect generators and return/yield expressions
|
|
23
|
+
* Inspect classes, inheritance, metaclasses, and methods
|
|
24
|
+
* Show imports and aliases
|
|
25
|
+
* Show arguments and type annotations
|
|
26
|
+
* Show decorators and docstrings
|
|
27
|
+
* Show source line ranges
|
|
28
|
+
* Print the complete Python AST
|
|
29
|
+
|
|
30
|
+
## Why InspectLab?
|
|
31
|
+
|
|
32
|
+
InspectLab is useful when you want a fast structural overview of a Python file without opening it manually or running its code.
|
|
33
|
+
|
|
34
|
+
### Advantages
|
|
35
|
+
|
|
36
|
+
* **Safe static inspection** — analyzes source code without executing the target file.
|
|
37
|
+
* **Fast CLI workflow** — inspect a file with one command.
|
|
38
|
+
* **No parser setup** — built on Python's standard `ast` module.
|
|
39
|
+
* **Useful for debugging and review** — quickly understand unfamiliar Python code.
|
|
40
|
+
* **Detailed output** — includes annotations, decorators, docstrings, return values, line numbers, and more.
|
|
41
|
+
|
|
42
|
+
## Installation
|
|
43
|
+
|
|
44
|
+
Install InspectLab from PyPI:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pip install inspectlab
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Then verify that the CLI is available:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
inspectlab --help
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
> InspectLab relies on modern Python AST features such as `ast.unparse`, so Python 3.9 or newer is recommended.
|
|
57
|
+
|
|
58
|
+
## Basic Usage
|
|
59
|
+
|
|
60
|
+
InspectLab provides four commands:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
inspectlab functions FILE
|
|
64
|
+
inspectlab classes FILE
|
|
65
|
+
inspectlab imports FILE
|
|
66
|
+
inspectlab tree FILE
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
`FILE` is the Python filename you want to inspect.
|
|
70
|
+
|
|
71
|
+
InspectLab searches for that filename recursively from your current working directory.
|
|
72
|
+
|
|
73
|
+
Example:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
inspectlab functions app.py
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## 1. `functions`
|
|
82
|
+
|
|
83
|
+
Inspect all functions found in a Python file.
|
|
84
|
+
|
|
85
|
+
### Usage
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
inspectlab functions FILE
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Example:
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
inspectlab functions app.py
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### What it outputs
|
|
98
|
+
|
|
99
|
+
For each function, InspectLab shows:
|
|
100
|
+
|
|
101
|
+
* Function name
|
|
102
|
+
* Function type
|
|
103
|
+
* Arguments and argument kinds
|
|
104
|
+
* Argument type annotations
|
|
105
|
+
* Return and yield expressions
|
|
106
|
+
* Return type annotation
|
|
107
|
+
* Decorators
|
|
108
|
+
* Docstring
|
|
109
|
+
* Start and end line numbers
|
|
110
|
+
|
|
111
|
+
It recognizes normal functions, async functions, generators, and async generators.
|
|
112
|
+
|
|
113
|
+
Example output:
|
|
114
|
+
|
|
115
|
+
```text
|
|
116
|
+
Name: add
|
|
117
|
+
Type: Function with Return
|
|
118
|
+
Arguments:
|
|
119
|
+
- a: int [Normal]
|
|
120
|
+
- b: int [Normal]
|
|
121
|
+
Returns: [{'Return': 'a + b'}]
|
|
122
|
+
Return Data Type: int
|
|
123
|
+
Decorators: []
|
|
124
|
+
Docstring: Add two integers.
|
|
125
|
+
Lines: {'Start': 9, 'End': 11}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## 2. `classes`
|
|
131
|
+
|
|
132
|
+
Inspect classes and their methods.
|
|
133
|
+
|
|
134
|
+
### Usage
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
inspectlab classes FILE
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Example:
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
inspectlab classes models.py
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### What it outputs
|
|
147
|
+
|
|
148
|
+
For each class, InspectLab shows:
|
|
149
|
+
|
|
150
|
+
* Class name
|
|
151
|
+
* Inherited classes
|
|
152
|
+
* Metaclass
|
|
153
|
+
* Class decorators
|
|
154
|
+
* Docstring
|
|
155
|
+
* Start and end line numbers
|
|
156
|
+
* Methods defined directly inside the class
|
|
157
|
+
* Detailed function information for each method
|
|
158
|
+
|
|
159
|
+
Example output:
|
|
160
|
+
|
|
161
|
+
```text
|
|
162
|
+
Class: Admin
|
|
163
|
+
Inherits: User
|
|
164
|
+
Metaclass: None
|
|
165
|
+
Decorators: None
|
|
166
|
+
Docstring: Represents an administrator.
|
|
167
|
+
Lines: 54 - 65
|
|
168
|
+
Methods:
|
|
169
|
+
Name: validate
|
|
170
|
+
Type: Function with Return
|
|
171
|
+
Arguments:
|
|
172
|
+
- value: int [Normal]
|
|
173
|
+
Returns: [{'Return': 'value > 0'}]
|
|
174
|
+
Return Data Type: bool
|
|
175
|
+
Decorators: ['staticmethod']
|
|
176
|
+
Docstring: None
|
|
177
|
+
Lines: {'Start': 64, 'End': 65}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
182
|
+
## 3. `imports`
|
|
183
|
+
|
|
184
|
+
List imports used by a Python file.
|
|
185
|
+
|
|
186
|
+
### Usage
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
inspectlab imports FILE
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Example:
|
|
193
|
+
|
|
194
|
+
```bash
|
|
195
|
+
inspectlab imports app.py
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### What it outputs
|
|
199
|
+
|
|
200
|
+
The command shows:
|
|
201
|
+
|
|
202
|
+
* Import type: `import` or `from ... import ...`
|
|
203
|
+
* Imported module or name
|
|
204
|
+
* Import aliases
|
|
205
|
+
* Source line number
|
|
206
|
+
|
|
207
|
+
Example output:
|
|
208
|
+
|
|
209
|
+
```text
|
|
210
|
+
Type: Import
|
|
211
|
+
Line: 2
|
|
212
|
+
Imports:
|
|
213
|
+
- sys as system
|
|
214
|
+
|
|
215
|
+
Type: ImportFrom
|
|
216
|
+
Line: 6
|
|
217
|
+
Module: collections
|
|
218
|
+
Imports:
|
|
219
|
+
- defaultdict as dd
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
---
|
|
223
|
+
|
|
224
|
+
## 4. `tree`
|
|
225
|
+
|
|
226
|
+
Print the complete Abstract Syntax Tree of a Python file.
|
|
227
|
+
|
|
228
|
+
### Usage
|
|
229
|
+
|
|
230
|
+
```bash
|
|
231
|
+
inspectlab tree FILE
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Example:
|
|
235
|
+
|
|
236
|
+
```bash
|
|
237
|
+
inspectlab tree app.py
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### What it outputs
|
|
241
|
+
|
|
242
|
+
This command prints Python's AST representation using an indented tree. It is useful when you need to see the exact syntax nodes generated from the source code.
|
|
243
|
+
|
|
244
|
+
Example output:
|
|
245
|
+
|
|
246
|
+
```text
|
|
247
|
+
Module(
|
|
248
|
+
body=[
|
|
249
|
+
Import(
|
|
250
|
+
names=[
|
|
251
|
+
alias(name='os')]),
|
|
252
|
+
FunctionDef(
|
|
253
|
+
name='add',
|
|
254
|
+
args=arguments(...),
|
|
255
|
+
body=[...])
|
|
256
|
+
])
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
## Example Workflow
|
|
260
|
+
|
|
261
|
+
```bash
|
|
262
|
+
# See the functions in a file
|
|
263
|
+
inspectlab functions main.py
|
|
264
|
+
|
|
265
|
+
# Inspect its classes
|
|
266
|
+
inspectlab classes main.py
|
|
267
|
+
|
|
268
|
+
# Check its dependencies/imports
|
|
269
|
+
inspectlab imports main.py
|
|
270
|
+
|
|
271
|
+
# View the raw AST
|
|
272
|
+
inspectlab tree main.py
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## Error Handling
|
|
276
|
+
|
|
277
|
+
If InspectLab cannot find or parse the requested file, the CLI currently prints:
|
|
278
|
+
|
|
279
|
+
```text
|
|
280
|
+
Wrong file name or typed out command
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
Make sure you run the command from a directory where InspectLab can find the requested filename.
|
|
284
|
+
|
|
285
|
+
## License
|
|
286
|
+
|
|
287
|
+
Add your project license here.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
inspectlab/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
inspectlab/ast_sample.py,sha256=hEv8ypUmHhbzhqnAQqwCa5FDBoCviVRemnGd112gGkk,1251
|
|
3
|
+
inspectlab/main.py,sha256=r-klpGORp8CS1SNVxP--dtSt8LlUvwwHTmTGqhxLECw,1015
|
|
4
|
+
inspectlab/cmds/base.py,sha256=ZVxIY-OhX9MkHtMyvy2kUz_gv7CMu7IJTYeFBqA-dRQ,561
|
|
5
|
+
inspectlab/cmds/classes.py,sha256=eH9dHzvo1kPQxcUBcTUpFDx4LM52p-peg3iakDi_Dvo,4674
|
|
6
|
+
inspectlab/cmds/functions.py,sha256=RSySvWNWnNpVpqD4aMq30eJ862jl4JpRZlMPRGR5tgs,6099
|
|
7
|
+
inspectlab/cmds/imports.py,sha256=HBBk5qfm2ibRdItB8Vs82CqKyyV0_0E9oqzTkS-jnUU,2765
|
|
8
|
+
inspectlab/cmds/test.py,sha256=NrQ0-4LgNZCaJPlKhpJi-QREYOe6MJrcGTr0DfO73Nc,256
|
|
9
|
+
inspectlab/cmds/tree.py,sha256=i4iQNwmyOd2-Ybe86RG9LXlM4OcMAEaLUX01Jfw_lhA,97
|
|
10
|
+
inspectlab-0.1.0.dist-info/METADATA,sha256=C-jpMiMef9An9negm2b3AIrswHPqzwOsQgxwBa_moYA,5247
|
|
11
|
+
inspectlab-0.1.0.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
|
|
12
|
+
inspectlab-0.1.0.dist-info/entry_points.txt,sha256=HPzg4XjNmNj-R1HZx-8fgzORv7-h7_M2ghPtuHt7-zA,51
|
|
13
|
+
inspectlab-0.1.0.dist-info/top_level.txt,sha256=8axxtC7cVgQJNIzuiXMSN9oZkodArzkgIUZcApAIFZk,11
|
|
14
|
+
inspectlab-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
inspectlab
|