prism-code-workspace 1.0.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.
- prism_code_workspace-1.0.0.dist-info/METADATA +272 -0
- prism_code_workspace-1.0.0.dist-info/RECORD +30 -0
- prism_code_workspace-1.0.0.dist-info/WHEEL +4 -0
- prism_code_workspace-1.0.0.dist-info/entry_points.txt +3 -0
- prism_code_workspace-1.0.0.dist-info/licenses/LICENSE +21 -0
- python_app/__init__.py +3 -0
- python_app/ai.py +225 -0
- python_app/analysis.py +953 -0
- python_app/compare.py +685 -0
- python_app/compare_engine.py +284 -0
- python_app/compiled_analysis.py +727 -0
- python_app/desktop.py +181 -0
- python_app/function_pov/__init__.py +16 -0
- python_app/function_pov/analyzer.py +464 -0
- python_app/function_pov/graph.py +50 -0
- python_app/function_pov/models.py +53 -0
- python_app/function_pov/pov/__init__.py +17 -0
- python_app/function_pov/pov/engine.py +1630 -0
- python_app/function_pov/pov/exercises.py +1263 -0
- python_app/function_pov_ui.py +1749 -0
- python_app/i18n.py +570 -0
- python_app/installer.nsi +70 -0
- python_app/installer_runtime.nsi +70 -0
- python_app/js_analysis.py +709 -0
- python_app/launch.py +6 -0
- python_app/launch_native.py +14 -0
- python_app/metrics_advisor.py +79 -0
- python_app/native_app.py +1709 -0
- python_app/requirements.txt +7 -0
- python_app/server.py +453 -0
python_app/desktop.py
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"""PySide6 desktop shell for Prism.
|
|
2
|
+
|
|
3
|
+
Python owns the desktop window, local HTTP server, AI proxy, lifecycle and
|
|
4
|
+
fullscreen menu. The rendered workspace is the same local web UI, loaded in
|
|
5
|
+
Qt WebEngine so the visualizations remain pixel-identical.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import ctypes
|
|
11
|
+
import os
|
|
12
|
+
import sys
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
from PySide6.QtCore import QUrl, Qt
|
|
16
|
+
from PySide6.QtGui import QAction, QIcon, QKeySequence
|
|
17
|
+
from PySide6.QtWebEngineCore import QWebEngineProfile, QWebEngineSettings
|
|
18
|
+
from PySide6.QtWebEngineWidgets import QWebEngineView
|
|
19
|
+
from PySide6.QtWidgets import QApplication, QMainWindow, QMessageBox
|
|
20
|
+
|
|
21
|
+
if __package__ in {None, ""}:
|
|
22
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
23
|
+
|
|
24
|
+
from python_app.server import start_server, stop_server
|
|
25
|
+
|
|
26
|
+
APP_TITLE = "Prism — code analysis workspace"
|
|
27
|
+
_MUTEX_HANDLE = None
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def resource_root() -> Path:
|
|
31
|
+
if getattr(sys, "frozen", False):
|
|
32
|
+
return Path(getattr(sys, "_MEIPASS", Path(sys.executable).parent))
|
|
33
|
+
return Path(__file__).resolve().parent.parent
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def acquire_single_instance() -> bool:
|
|
37
|
+
global _MUTEX_HANDLE
|
|
38
|
+
if os.name != "nt":
|
|
39
|
+
return True
|
|
40
|
+
kernel32 = ctypes.windll.kernel32
|
|
41
|
+
_MUTEX_HANDLE = kernel32.CreateMutexW(None, False, "Local\\PrismPythonDesktop")
|
|
42
|
+
return kernel32.GetLastError() != 183
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def storage_root() -> Path:
|
|
46
|
+
executable_dir = Path(sys.executable).resolve().parent
|
|
47
|
+
if (executable_dir / "portable.flag").exists():
|
|
48
|
+
return executable_dir / "PrismData"
|
|
49
|
+
local = Path(os.environ.get("LOCALAPPDATA", Path.home())) / "Prism"
|
|
50
|
+
return local / "WebEngine"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class PrismWindow(QMainWindow):
|
|
54
|
+
def __init__(self, url: str, icon_path: Path):
|
|
55
|
+
super().__init__()
|
|
56
|
+
self.setWindowTitle(APP_TITLE)
|
|
57
|
+
self.resize(1440, 920)
|
|
58
|
+
self.setMinimumSize(960, 680)
|
|
59
|
+
if icon_path.exists():
|
|
60
|
+
self.setWindowIcon(QIcon(str(icon_path)))
|
|
61
|
+
|
|
62
|
+
profile = QWebEngineProfile.defaultProfile()
|
|
63
|
+
storage = storage_root()
|
|
64
|
+
storage.mkdir(parents=True, exist_ok=True)
|
|
65
|
+
profile.setPersistentStoragePath(str(storage / "storage"))
|
|
66
|
+
profile.setCachePath(str(storage / "cache"))
|
|
67
|
+
profile.setPersistentCookiesPolicy(QWebEngineProfile.PersistentCookiesPolicy.ForcePersistentCookies)
|
|
68
|
+
|
|
69
|
+
self.view = QWebEngineView(self)
|
|
70
|
+
self.view.setContextMenuPolicy(Qt.ContextMenuPolicy.DefaultContextMenu)
|
|
71
|
+
settings = self.view.settings()
|
|
72
|
+
settings.setAttribute(QWebEngineSettings.WebAttribute.JavascriptEnabled, True)
|
|
73
|
+
settings.setAttribute(QWebEngineSettings.WebAttribute.LocalStorageEnabled, True)
|
|
74
|
+
settings.setAttribute(QWebEngineSettings.WebAttribute.ScrollAnimatorEnabled, True)
|
|
75
|
+
settings.setAttribute(QWebEngineSettings.WebAttribute.FullScreenSupportEnabled, True)
|
|
76
|
+
self.view.setUrl(QUrl(url))
|
|
77
|
+
self.setCentralWidget(self.view)
|
|
78
|
+
|
|
79
|
+
self.fullscreen_action = QAction("Toggle full screen", self)
|
|
80
|
+
self.fullscreen_action.setShortcut(QKeySequence("F11"))
|
|
81
|
+
self.fullscreen_action.setShortcutContext(Qt.ShortcutContext.ApplicationShortcut)
|
|
82
|
+
self.fullscreen_action.triggered.connect(self.toggle_fullscreen)
|
|
83
|
+
self.addAction(self.fullscreen_action)
|
|
84
|
+
|
|
85
|
+
reload_action = QAction("Reload", self)
|
|
86
|
+
reload_action.setShortcut(QKeySequence.StandardKey.Refresh)
|
|
87
|
+
reload_action.triggered.connect(self.view.reload)
|
|
88
|
+
self.addAction(reload_action)
|
|
89
|
+
|
|
90
|
+
menu = self.menuBar()
|
|
91
|
+
view_menu = menu.addMenu("&View")
|
|
92
|
+
view_menu.addAction(self.fullscreen_action)
|
|
93
|
+
view_menu.addAction(reload_action)
|
|
94
|
+
window_menu = menu.addMenu("&Window")
|
|
95
|
+
minimize_action = QAction("Minimize", self)
|
|
96
|
+
minimize_action.triggered.connect(self.showMinimized)
|
|
97
|
+
window_menu.addAction(minimize_action)
|
|
98
|
+
close_action = QAction("Close", self)
|
|
99
|
+
close_action.setShortcut(QKeySequence.StandardKey.Close)
|
|
100
|
+
close_action.triggered.connect(self.close)
|
|
101
|
+
window_menu.addAction(close_action)
|
|
102
|
+
help_menu = menu.addMenu("&Help")
|
|
103
|
+
about_action = QAction("About Prism", self)
|
|
104
|
+
about_action.triggered.connect(self.show_about)
|
|
105
|
+
help_menu.addAction(about_action)
|
|
106
|
+
|
|
107
|
+
self._is_fullscreen = False
|
|
108
|
+
|
|
109
|
+
def toggle_fullscreen(self) -> None:
|
|
110
|
+
if self.isFullScreen():
|
|
111
|
+
self.showNormal()
|
|
112
|
+
else:
|
|
113
|
+
self.showFullScreen()
|
|
114
|
+
|
|
115
|
+
def show_about(self) -> None:
|
|
116
|
+
QMessageBox.about(
|
|
117
|
+
self,
|
|
118
|
+
"About Prism",
|
|
119
|
+
"Prism 1.0.0\n\nA local-first code analysis workspace.\n"
|
|
120
|
+
"Python desktop shell, local server, and AI proxy.\n"
|
|
121
|
+
"The analysis and diagram views run locally in Qt WebEngine.",
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
def keyPressEvent(self, event): # keep F11 reliable with WebEngine focus
|
|
125
|
+
if event.key() == Qt.Key.Key_F11:
|
|
126
|
+
self.toggle_fullscreen()
|
|
127
|
+
event.accept()
|
|
128
|
+
return
|
|
129
|
+
super().keyPressEvent(event)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def hide_console() -> None:
|
|
133
|
+
"""Use the console bootloader for reliability, but hide its window."""
|
|
134
|
+
if getattr(sys, "frozen", False) and os.name == "nt":
|
|
135
|
+
hwnd = ctypes.windll.kernel32.GetConsoleWindow()
|
|
136
|
+
if hwnd:
|
|
137
|
+
ctypes.windll.user32.ShowWindow(hwnd, 0)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def configure_webengine(root: Path) -> None:
|
|
141
|
+
"""Point Qt WebEngine at the bundled runtime and use the portable GL path."""
|
|
142
|
+
frozen = getattr(sys, "frozen", False)
|
|
143
|
+
portable_runtime = root / "runtime" / "Lib" / "site-packages" / "PySide6"
|
|
144
|
+
if frozen or (root / "portable.flag").exists():
|
|
145
|
+
qt = root / "PySide6" if frozen else portable_runtime
|
|
146
|
+
os.environ.setdefault("QTWEBENGINE_RESOURCES_PATH", str(qt / "resources"))
|
|
147
|
+
os.environ.setdefault("QTWEBENGINE_LOCALES_PATH", str(qt / "translations" / "qtwebengine_locales"))
|
|
148
|
+
os.environ.setdefault("QTWEBENGINE_DISABLE_SANDBOX", "1")
|
|
149
|
+
flags = os.environ.get("QTWEBENGINE_CHROMIUM_FLAGS", "")
|
|
150
|
+
for flag in ("--single-process", "--disable-gpu", "--no-sandbox"):
|
|
151
|
+
if flag not in flags:
|
|
152
|
+
flags = f"{flags} {flag}".strip()
|
|
153
|
+
os.environ["QTWEBENGINE_CHROMIUM_FLAGS"] = flags
|
|
154
|
+
else:
|
|
155
|
+
os.environ.setdefault("QTWEBENGINE_CHROMIUM_FLAGS", "--disable-features=Translate")
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def main() -> int:
|
|
159
|
+
hide_console()
|
|
160
|
+
if not acquire_single_instance():
|
|
161
|
+
return 0
|
|
162
|
+
root = resource_root()
|
|
163
|
+
configure_webengine(root)
|
|
164
|
+
server, url = start_server(root)
|
|
165
|
+
icon_path = root / "assets" / "icon.ico"
|
|
166
|
+
app = QApplication(sys.argv)
|
|
167
|
+
app.setApplicationName("Prism")
|
|
168
|
+
app.setApplicationDisplayName("Prism")
|
|
169
|
+
window = PrismWindow(url, icon_path)
|
|
170
|
+
if "--fullscreen" in sys.argv:
|
|
171
|
+
window.showFullScreen()
|
|
172
|
+
else:
|
|
173
|
+
window.show()
|
|
174
|
+
try:
|
|
175
|
+
return app.exec()
|
|
176
|
+
finally:
|
|
177
|
+
stop_server(server)
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
if __name__ == "__main__":
|
|
181
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""Static analysis tools for the Function POV project."""
|
|
2
|
+
|
|
3
|
+
from .analyzer import analyze_project
|
|
4
|
+
from .graph import FunctionGraph, build_function_graph
|
|
5
|
+
from .models import AnalysisResult, CallEdge, Diagnostic, FunctionInfo, ParameterInfo
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"AnalysisResult",
|
|
9
|
+
"CallEdge",
|
|
10
|
+
"Diagnostic",
|
|
11
|
+
"FunctionGraph",
|
|
12
|
+
"FunctionInfo",
|
|
13
|
+
"ParameterInfo",
|
|
14
|
+
"analyze_project",
|
|
15
|
+
"build_function_graph",
|
|
16
|
+
]
|
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
"""Static Python source analysis based on the standard-library AST module."""
|
|
2
|
+
|
|
3
|
+
import ast
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Dict, Iterable, List, Optional, Sequence, Set, Tuple
|
|
7
|
+
|
|
8
|
+
from .models import (
|
|
9
|
+
AnalysisResult,
|
|
10
|
+
CallEdge,
|
|
11
|
+
Diagnostic,
|
|
12
|
+
FunctionInfo,
|
|
13
|
+
ParameterInfo,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
DEFAULT_EXCLUDED_DIRS = frozenset(
|
|
17
|
+
{".git", ".venv", "venv", "env", "__pycache__", "build", "dist"}
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class _ParsedFile:
|
|
23
|
+
path: Path
|
|
24
|
+
module: str
|
|
25
|
+
is_package_init: bool
|
|
26
|
+
tree: ast.Module
|
|
27
|
+
source: str
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def analyze_project(
|
|
31
|
+
path: str,
|
|
32
|
+
exclude_dirs: Optional[Iterable[str]] = None,
|
|
33
|
+
) -> AnalysisResult:
|
|
34
|
+
"""Analyze Python functions and their statically resolvable local calls.
|
|
35
|
+
|
|
36
|
+
Only source parsing is performed. No project module is imported or executed.
|
|
37
|
+
"""
|
|
38
|
+
target = Path(path).expanduser().resolve()
|
|
39
|
+
if not target.exists():
|
|
40
|
+
raise FileNotFoundError("Project path does not exist: {}".format(target))
|
|
41
|
+
if target.is_file() and target.suffix != ".py":
|
|
42
|
+
raise ValueError("Expected a Python file or a project directory: {}".format(target))
|
|
43
|
+
if not target.is_file() and not target.is_dir():
|
|
44
|
+
raise ValueError("Expected a Python file or a project directory: {}".format(target))
|
|
45
|
+
|
|
46
|
+
excluded = set(DEFAULT_EXCLUDED_DIRS)
|
|
47
|
+
if exclude_dirs is not None:
|
|
48
|
+
excluded.update(exclude_dirs)
|
|
49
|
+
|
|
50
|
+
root = target.parent if target.is_file() else target
|
|
51
|
+
files = [target] if target.is_file() else _find_python_files(target, excluded)
|
|
52
|
+
diagnostics: List[Diagnostic] = []
|
|
53
|
+
parsed_files: List[_ParsedFile] = []
|
|
54
|
+
|
|
55
|
+
for file_path in files:
|
|
56
|
+
relative = file_path.relative_to(root)
|
|
57
|
+
module, is_package_init = _module_name(relative, root)
|
|
58
|
+
try:
|
|
59
|
+
source = file_path.read_text(encoding="utf-8")
|
|
60
|
+
except UnicodeDecodeError as error:
|
|
61
|
+
diagnostics.append(
|
|
62
|
+
Diagnostic(
|
|
63
|
+
file=relative.as_posix(),
|
|
64
|
+
message="Source is not valid UTF-8: {}".format(error),
|
|
65
|
+
severity="error",
|
|
66
|
+
)
|
|
67
|
+
)
|
|
68
|
+
continue
|
|
69
|
+
|
|
70
|
+
try:
|
|
71
|
+
tree = ast.parse(source, filename=relative.as_posix())
|
|
72
|
+
except SyntaxError as error:
|
|
73
|
+
diagnostics.append(
|
|
74
|
+
Diagnostic(
|
|
75
|
+
file=relative.as_posix(),
|
|
76
|
+
message=error.msg,
|
|
77
|
+
severity="error",
|
|
78
|
+
line=error.lineno,
|
|
79
|
+
)
|
|
80
|
+
)
|
|
81
|
+
continue
|
|
82
|
+
|
|
83
|
+
parsed_files.append(
|
|
84
|
+
_ParsedFile(
|
|
85
|
+
path=relative,
|
|
86
|
+
module=module,
|
|
87
|
+
is_package_init=is_package_init,
|
|
88
|
+
tree=tree,
|
|
89
|
+
source=source,
|
|
90
|
+
)
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
if not files:
|
|
94
|
+
diagnostics.append(
|
|
95
|
+
Diagnostic(
|
|
96
|
+
file=".",
|
|
97
|
+
message="No Python source files were found.",
|
|
98
|
+
severity="warning",
|
|
99
|
+
)
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
return _analyze_parsed_files(str(root), parsed_files, diagnostics)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def analyze_source(source: str, filename: str = "pasted.py") -> AnalysisResult:
|
|
106
|
+
"""Analyze a virtual Python file without writing it to disk."""
|
|
107
|
+
if not isinstance(source, str):
|
|
108
|
+
raise TypeError("Python source must be text.")
|
|
109
|
+
normalized_filename = filename.replace("\\", "/")
|
|
110
|
+
relative = Path(normalized_filename)
|
|
111
|
+
if (
|
|
112
|
+
not filename
|
|
113
|
+
or relative.is_absolute()
|
|
114
|
+
or any(part in {"", ".", ".."} for part in normalized_filename.split("/"))
|
|
115
|
+
or relative.suffix.lower() != ".py"
|
|
116
|
+
):
|
|
117
|
+
raise ValueError("The virtual filename must be a relative Python file path.")
|
|
118
|
+
|
|
119
|
+
diagnostics: List[Diagnostic] = []
|
|
120
|
+
try:
|
|
121
|
+
tree = ast.parse(source, filename=relative.as_posix())
|
|
122
|
+
except SyntaxError as error:
|
|
123
|
+
diagnostics.append(
|
|
124
|
+
Diagnostic(
|
|
125
|
+
file=relative.as_posix(),
|
|
126
|
+
message=error.msg,
|
|
127
|
+
severity="error",
|
|
128
|
+
line=error.lineno,
|
|
129
|
+
)
|
|
130
|
+
)
|
|
131
|
+
return _analyze_parsed_files("<memory>", [], diagnostics)
|
|
132
|
+
|
|
133
|
+
module = ".".join(relative.with_suffix("").parts)
|
|
134
|
+
is_package_init = relative.name == "__init__.py"
|
|
135
|
+
parsed = _ParsedFile(
|
|
136
|
+
path=relative,
|
|
137
|
+
module=module,
|
|
138
|
+
is_package_init=is_package_init,
|
|
139
|
+
tree=tree,
|
|
140
|
+
source=source,
|
|
141
|
+
)
|
|
142
|
+
return _analyze_parsed_files("<memory>", [parsed], diagnostics)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def _analyze_parsed_files(
|
|
146
|
+
root: str,
|
|
147
|
+
parsed_files: List[_ParsedFile],
|
|
148
|
+
diagnostics: List[Diagnostic],
|
|
149
|
+
) -> AnalysisResult:
|
|
150
|
+
functions: List[FunctionInfo] = []
|
|
151
|
+
function_ids: Set[str] = set()
|
|
152
|
+
for parsed in parsed_files:
|
|
153
|
+
for node in parsed.tree.body:
|
|
154
|
+
if isinstance(node, ast.FunctionDef):
|
|
155
|
+
function = _function_info(parsed, node)
|
|
156
|
+
functions.append(function)
|
|
157
|
+
function_ids.add(function.id)
|
|
158
|
+
diagnostics.extend(_nested_definition_diagnostics(parsed.path, node))
|
|
159
|
+
elif isinstance(node, ast.AsyncFunctionDef):
|
|
160
|
+
diagnostics.append(
|
|
161
|
+
Diagnostic(
|
|
162
|
+
file=parsed.path.as_posix(),
|
|
163
|
+
message="Async functions are not analyzed yet.",
|
|
164
|
+
severity="warning",
|
|
165
|
+
line=node.lineno,
|
|
166
|
+
)
|
|
167
|
+
)
|
|
168
|
+
elif isinstance(node, ast.ClassDef):
|
|
169
|
+
diagnostics.append(
|
|
170
|
+
Diagnostic(
|
|
171
|
+
file=parsed.path.as_posix(),
|
|
172
|
+
message="Class definitions and methods are not analyzed yet.",
|
|
173
|
+
severity="warning",
|
|
174
|
+
line=node.lineno,
|
|
175
|
+
)
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
functions.sort(key=lambda item: (item.module, item.line, item.name))
|
|
179
|
+
calls = _collect_calls(parsed_files, function_ids)
|
|
180
|
+
return AnalysisResult(
|
|
181
|
+
root=root,
|
|
182
|
+
functions=tuple(functions),
|
|
183
|
+
calls=tuple(calls),
|
|
184
|
+
diagnostics=tuple(diagnostics),
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _find_python_files(root: Path, excluded_dirs: Set[str]) -> List[Path]:
|
|
189
|
+
return sorted(
|
|
190
|
+
(
|
|
191
|
+
candidate
|
|
192
|
+
for candidate in root.rglob("*.py")
|
|
193
|
+
if not any(part in excluded_dirs for part in candidate.relative_to(root).parts)
|
|
194
|
+
),
|
|
195
|
+
key=lambda candidate: candidate.relative_to(root).as_posix(),
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def _module_name(relative: Path, root: Path) -> Tuple[str, bool]:
|
|
200
|
+
is_package_init = relative.name == "__init__.py"
|
|
201
|
+
parts = list(relative.parent.parts if is_package_init else relative.with_suffix("").parts)
|
|
202
|
+
if (root / "__init__.py").is_file():
|
|
203
|
+
parts.insert(0, root.name)
|
|
204
|
+
if not parts:
|
|
205
|
+
parts.append(root.name)
|
|
206
|
+
return ".".join(parts), is_package_init
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def _function_info(parsed: _ParsedFile, node: ast.FunctionDef) -> FunctionInfo:
|
|
210
|
+
parameters = _parameters(node.args)
|
|
211
|
+
returns = tuple(
|
|
212
|
+
ast.unparse(return_node.value) if return_node.value is not None else "None"
|
|
213
|
+
for return_node in _return_nodes(node)
|
|
214
|
+
)
|
|
215
|
+
lines = parsed.source.splitlines()
|
|
216
|
+
first_line = min(
|
|
217
|
+
(decorator.lineno for decorator in node.decorator_list),
|
|
218
|
+
default=node.lineno,
|
|
219
|
+
)
|
|
220
|
+
source = "\n".join(lines[first_line - 1 : node.end_lineno])
|
|
221
|
+
return FunctionInfo(
|
|
222
|
+
id="{}.{}".format(parsed.module, node.name),
|
|
223
|
+
name=node.name,
|
|
224
|
+
module=parsed.module,
|
|
225
|
+
file=parsed.path.as_posix(),
|
|
226
|
+
line=first_line,
|
|
227
|
+
end_line=node.end_lineno,
|
|
228
|
+
source=source,
|
|
229
|
+
docstring=ast.get_docstring(node, clean=False),
|
|
230
|
+
parameters=parameters,
|
|
231
|
+
return_expressions=returns,
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def _parameters(arguments: ast.arguments) -> Tuple[ParameterInfo, ...]:
|
|
236
|
+
result: List[ParameterInfo] = []
|
|
237
|
+
positional = list(arguments.posonlyargs) + list(arguments.args)
|
|
238
|
+
positional_defaults: List[Optional[ast.expr]] = (
|
|
239
|
+
[None] * (len(positional) - len(arguments.defaults)) + list(arguments.defaults)
|
|
240
|
+
)
|
|
241
|
+
positional_kinds = ["positional_only"] * len(arguments.posonlyargs)
|
|
242
|
+
positional_kinds.extend(["positional_or_keyword"] * len(arguments.args))
|
|
243
|
+
|
|
244
|
+
for argument, default, kind in zip(positional, positional_defaults, positional_kinds):
|
|
245
|
+
result.append(_parameter(argument, kind, default))
|
|
246
|
+
if arguments.vararg is not None:
|
|
247
|
+
result.append(_parameter(arguments.vararg, "var_positional", None))
|
|
248
|
+
for argument, default in zip(arguments.kwonlyargs, arguments.kw_defaults):
|
|
249
|
+
result.append(_parameter(argument, "keyword_only", default))
|
|
250
|
+
if arguments.kwarg is not None:
|
|
251
|
+
result.append(_parameter(arguments.kwarg, "var_keyword", None))
|
|
252
|
+
return tuple(result)
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def _parameter(
|
|
256
|
+
argument: ast.arg,
|
|
257
|
+
kind: str,
|
|
258
|
+
default: Optional[ast.expr],
|
|
259
|
+
) -> ParameterInfo:
|
|
260
|
+
return ParameterInfo(
|
|
261
|
+
name=argument.arg,
|
|
262
|
+
kind=kind,
|
|
263
|
+
default=ast.unparse(default) if default is not None else None,
|
|
264
|
+
annotation=ast.unparse(argument.annotation) if argument.annotation else None,
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def _return_nodes(function: ast.FunctionDef) -> List[ast.Return]:
|
|
269
|
+
class ReturnFinder(ast.NodeVisitor):
|
|
270
|
+
def __init__(self) -> None:
|
|
271
|
+
self.nodes: List[ast.Return] = []
|
|
272
|
+
|
|
273
|
+
def visit_Return(self, node: ast.Return) -> None:
|
|
274
|
+
self.nodes.append(node)
|
|
275
|
+
|
|
276
|
+
def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
|
|
277
|
+
if node is function:
|
|
278
|
+
self.generic_visit(node)
|
|
279
|
+
|
|
280
|
+
def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None:
|
|
281
|
+
return
|
|
282
|
+
|
|
283
|
+
def visit_ClassDef(self, node: ast.ClassDef) -> None:
|
|
284
|
+
return
|
|
285
|
+
|
|
286
|
+
def visit_Lambda(self, node: ast.Lambda) -> None:
|
|
287
|
+
return
|
|
288
|
+
|
|
289
|
+
finder = ReturnFinder()
|
|
290
|
+
finder.visit(function)
|
|
291
|
+
return finder.nodes
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def _nested_definition_diagnostics(
|
|
295
|
+
path: Path,
|
|
296
|
+
function: ast.FunctionDef,
|
|
297
|
+
) -> List[Diagnostic]:
|
|
298
|
+
diagnostics: List[Diagnostic] = []
|
|
299
|
+
|
|
300
|
+
class DefinitionFinder(ast.NodeVisitor):
|
|
301
|
+
def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
|
|
302
|
+
if node is not function:
|
|
303
|
+
diagnostics.append(
|
|
304
|
+
Diagnostic(
|
|
305
|
+
file=path.as_posix(),
|
|
306
|
+
message="Nested functions are not analyzed yet.",
|
|
307
|
+
severity="warning",
|
|
308
|
+
line=node.lineno,
|
|
309
|
+
)
|
|
310
|
+
)
|
|
311
|
+
return
|
|
312
|
+
self.generic_visit(node)
|
|
313
|
+
|
|
314
|
+
def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None:
|
|
315
|
+
diagnostics.append(
|
|
316
|
+
Diagnostic(
|
|
317
|
+
file=path.as_posix(),
|
|
318
|
+
message="Nested async functions are not analyzed yet.",
|
|
319
|
+
severity="warning",
|
|
320
|
+
line=node.lineno,
|
|
321
|
+
)
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
def visit_ClassDef(self, node: ast.ClassDef) -> None:
|
|
325
|
+
diagnostics.append(
|
|
326
|
+
Diagnostic(
|
|
327
|
+
file=path.as_posix(),
|
|
328
|
+
message="Nested classes are not analyzed yet.",
|
|
329
|
+
severity="warning",
|
|
330
|
+
line=node.lineno,
|
|
331
|
+
)
|
|
332
|
+
)
|
|
333
|
+
|
|
334
|
+
DefinitionFinder().visit(function)
|
|
335
|
+
return diagnostics
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
def _collect_calls(
|
|
339
|
+
parsed_files: Sequence[_ParsedFile],
|
|
340
|
+
function_ids: Set[str],
|
|
341
|
+
) -> List[CallEdge]:
|
|
342
|
+
calls: List[CallEdge] = []
|
|
343
|
+
for parsed in parsed_files:
|
|
344
|
+
for node in parsed.tree.body:
|
|
345
|
+
if not isinstance(node, ast.FunctionDef):
|
|
346
|
+
continue
|
|
347
|
+
caller = "{}.{}".format(parsed.module, node.name)
|
|
348
|
+
imported_functions, imported_modules = _import_bindings(parsed)
|
|
349
|
+
finder = _CallFinder(
|
|
350
|
+
caller=caller,
|
|
351
|
+
module=parsed.module,
|
|
352
|
+
function_ids=function_ids,
|
|
353
|
+
imported_functions=imported_functions,
|
|
354
|
+
imported_modules=imported_modules,
|
|
355
|
+
)
|
|
356
|
+
for statement in node.body:
|
|
357
|
+
finder.visit(statement)
|
|
358
|
+
calls.extend(finder.calls)
|
|
359
|
+
calls.sort(key=lambda item: (item.caller, item.line, item.callee))
|
|
360
|
+
return calls
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
def _import_bindings(parsed: _ParsedFile) -> Tuple[Dict[str, str], Dict[str, str]]:
|
|
364
|
+
imported_functions: Dict[str, str] = {}
|
|
365
|
+
imported_modules: Dict[str, str] = {}
|
|
366
|
+
for node in parsed.tree.body:
|
|
367
|
+
if isinstance(node, ast.ImportFrom):
|
|
368
|
+
module = _resolve_from_module(parsed, node)
|
|
369
|
+
for alias in node.names:
|
|
370
|
+
local_name = alias.asname or alias.name
|
|
371
|
+
imported_modules[local_name] = ".".join(
|
|
372
|
+
part for part in (module, alias.name) if part
|
|
373
|
+
)
|
|
374
|
+
imported_functions[local_name] = ".".join(
|
|
375
|
+
part for part in (module, alias.name) if part
|
|
376
|
+
)
|
|
377
|
+
elif isinstance(node, ast.Import):
|
|
378
|
+
for alias in node.names:
|
|
379
|
+
local_name = alias.asname or alias.name.split(".")[0]
|
|
380
|
+
imported_modules[local_name] = (
|
|
381
|
+
alias.name if alias.asname else alias.name.split(".")[0]
|
|
382
|
+
)
|
|
383
|
+
return imported_functions, imported_modules
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
def _resolve_from_module(parsed: _ParsedFile, node: ast.ImportFrom) -> str:
|
|
387
|
+
if node.level == 0:
|
|
388
|
+
return node.module or ""
|
|
389
|
+
package_parts = parsed.module.split(".")
|
|
390
|
+
if not parsed.is_package_init and package_parts:
|
|
391
|
+
package_parts.pop()
|
|
392
|
+
remove_count = node.level - 1
|
|
393
|
+
if remove_count > len(package_parts):
|
|
394
|
+
return node.module or ""
|
|
395
|
+
base = package_parts[: len(package_parts) - remove_count]
|
|
396
|
+
if node.module:
|
|
397
|
+
base.extend(node.module.split("."))
|
|
398
|
+
return ".".join(base)
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
class _CallFinder(ast.NodeVisitor):
|
|
402
|
+
def __init__(
|
|
403
|
+
self,
|
|
404
|
+
caller: str,
|
|
405
|
+
module: str,
|
|
406
|
+
function_ids: Set[str],
|
|
407
|
+
imported_functions: Dict[str, str],
|
|
408
|
+
imported_modules: Dict[str, str],
|
|
409
|
+
) -> None:
|
|
410
|
+
self.caller = caller
|
|
411
|
+
self.module = module
|
|
412
|
+
self.function_ids = function_ids
|
|
413
|
+
self.imported_functions = imported_functions
|
|
414
|
+
self.imported_modules = imported_modules
|
|
415
|
+
self.calls: List[CallEdge] = []
|
|
416
|
+
|
|
417
|
+
def visit_Call(self, node: ast.Call) -> None:
|
|
418
|
+
callee = self._resolve_callee(node.func)
|
|
419
|
+
if callee is not None:
|
|
420
|
+
self.calls.append(
|
|
421
|
+
CallEdge(
|
|
422
|
+
caller=self.caller,
|
|
423
|
+
callee=callee,
|
|
424
|
+
line=node.lineno,
|
|
425
|
+
expression=ast.unparse(node.func),
|
|
426
|
+
)
|
|
427
|
+
)
|
|
428
|
+
self.generic_visit(node)
|
|
429
|
+
|
|
430
|
+
def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
|
|
431
|
+
return
|
|
432
|
+
|
|
433
|
+
def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None:
|
|
434
|
+
return
|
|
435
|
+
|
|
436
|
+
def visit_ClassDef(self, node: ast.ClassDef) -> None:
|
|
437
|
+
return
|
|
438
|
+
|
|
439
|
+
def visit_Lambda(self, node: ast.Lambda) -> None:
|
|
440
|
+
return
|
|
441
|
+
|
|
442
|
+
def _resolve_callee(self, expression: ast.expr) -> Optional[str]:
|
|
443
|
+
if isinstance(expression, ast.Name):
|
|
444
|
+
imported = self.imported_functions.get(expression.id)
|
|
445
|
+
if imported in self.function_ids:
|
|
446
|
+
return imported
|
|
447
|
+
local = "{}.{}".format(self.module, expression.id)
|
|
448
|
+
return local if local in self.function_ids else None
|
|
449
|
+
|
|
450
|
+
if isinstance(expression, ast.Attribute):
|
|
451
|
+
parts: List[str] = []
|
|
452
|
+
current: ast.expr = expression
|
|
453
|
+
while isinstance(current, ast.Attribute):
|
|
454
|
+
parts.append(current.attr)
|
|
455
|
+
current = current.value
|
|
456
|
+
if isinstance(current, ast.Name):
|
|
457
|
+
parts.append(current.id)
|
|
458
|
+
parts.reverse()
|
|
459
|
+
module = self.imported_modules.get(parts[0])
|
|
460
|
+
if module:
|
|
461
|
+
target = ".".join([module] + parts[1:])
|
|
462
|
+
if target in self.function_ids:
|
|
463
|
+
return target
|
|
464
|
+
return None
|