pystepmap-core 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.
pystepmap/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ """pystepmap 核心包。"""
2
+
3
+ __version__ = "0.1.0" # 引擎版本:平台把它和每次分析一起保存
pystepmap/analyze.py ADDED
@@ -0,0 +1,68 @@
1
+ """把索引、规则、解析器、函数图、功能线和自检串成一次完整的项目分析。"""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass, field
6
+ from pathlib import Path
7
+
8
+ from pystepmap.build import build_function_graph
9
+ from pystepmap.config import ProjectConfig
10
+ from pystepmap.entries import auto_entries, unlisted_roots
11
+ from pystepmap.index import build_index
12
+ from pystepmap.lines import annotate_dispatches, assemble_lines, extra_entry_keys
13
+ from pystepmap.model import SCHEMA_VERSION, ProjectGraph, function_sids
14
+ from pystepmap.resolve import Resolver
15
+ from pystepmap.rules import ActiveRules
16
+ from pystepmap.selfcheck import run_selfcheck
17
+
18
+
19
+ @dataclass
20
+ class AnalysisNotes:
21
+ """命令行摘要用到的、图里没有的信息。"""
22
+
23
+ rules: list[str] # 启用的识别规则名,排序
24
+ source_roots: list[str] # 源码根(相对路径,"" 是分析的目录本身)
25
+ excluded: dict[str, int] # 实际排除掉的目录名 -> 个数
26
+ package: str = "" # 分析的目录本身是包时它的点分名(见 ProjectIndex.package)
27
+ marks: list[str] = field(default_factory=list) # 标记里读不出来的参数,每处一句(见 ActiveRules.mark_notes)
28
+ # 不在功能线上、没有人直接调用、但项目里别处提到过的函数(交给别处调用的回调、依赖注入、注册表、模块顶层调用的
29
+ # 启动代码):列"不在任何功能线上的函数"时不算它们(见 entries.unlisted_roots)
30
+ mentioned: set[str] = field(default_factory=set)
31
+
32
+
33
+ def analyze_project(root: Path, config: ProjectConfig | None = None) -> ProjectGraph:
34
+ """分析 root 下的项目(只读),为每个项目内函数生成函数图。"""
35
+ return analyze_with_notes(root, config)[0]
36
+
37
+
38
+ def analyze_with_notes(root: Path, config: ProjectConfig | None = None) -> tuple[ProjectGraph, AnalysisNotes]:
39
+ """同 analyze_project,另外返回命令行摘要用到的信息。"""
40
+ config = config or ProjectConfig()
41
+ index = build_index(Path(root).resolve(), config)
42
+ rules, resolver = ActiveRules(index, config), Resolver(index)
43
+ skipped = list(index.skipped)
44
+ functions = {}
45
+ for key in sorted(index.functions):
46
+ fn = index.functions[key]
47
+ try:
48
+ functions[key] = build_function_graph(fn, index, rules, resolver, config)
49
+ except Exception as e: # noqa: BLE001 单个函数出错不拖垮整体分析
50
+ skipped.append((fn.file, f"函数 {fn.qualname} 构建失败:{type(e).__name__}: {e}"))
51
+ sids = function_sids(index.functions) # 按全部定义编号:某个函数构建失败不影响别的函数的 sid
52
+ for key, fg in functions.items():
53
+ fg.sid = sids[key]
54
+ annotate_dispatches(functions)
55
+ referenced = resolver.value_references()
56
+ auto = auto_entries(functions, index, resolver, rules.external, referenced, config.fallback_entries,
57
+ extra_entry_keys(functions, config))
58
+ lines, common = assemble_lines(functions, config, referenced, auto)
59
+ graph = ProjectGraph(
60
+ schema_version=SCHEMA_VERSION, root=str(index.root), functions=functions, lines=lines,
61
+ common_tasks=common, selfcheck=run_selfcheck(functions, skipped), file_hashes=dict(sorted(index.hashes.items())),
62
+ sids={fg.sid: key for key, fg in sorted(functions.items(), key=lambda kv: kv[1].sid)})
63
+ notes = AnalysisNotes(sorted(rules.names), list(index.source_roots), dict(sorted(index.excluded.items())),
64
+ index.package, rules.mark_notes())
65
+ loose = unlisted_roots(graph, limit=None)[0]
66
+ if loose:
67
+ notes.mentioned = resolver.mentions({fg.qualname for fg in loose}) & {fg.key for fg in loose}
68
+ return graph, notes