-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_wire_encode.py
More file actions
351 lines (319 loc) · 11.1 KB
/
_wire_encode.py
File metadata and controls
351 lines (319 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
# Copyright (c) 2026 Den Rozhnovskiy
from __future__ import annotations
from ._canonicalize import _normalized_optional_string_list
from .entries import CacheEntry, ClassMetricsDict
def _encode_source_stats(entry: CacheEntry, wire: dict[str, object]) -> None:
source_stats = entry.get("source_stats")
if source_stats is not None:
wire["ss"] = [
source_stats["lines"],
source_stats["functions"],
source_stats["methods"],
source_stats["classes"],
]
def _encode_units(entry: CacheEntry, wire: dict[str, object]) -> None:
units = sorted(
entry["units"],
key=lambda unit: (
unit["qualname"],
unit["start_line"],
unit["end_line"],
unit["fingerprint"],
),
)
if units:
wire["u"] = [
[
unit["qualname"],
unit["start_line"],
unit["end_line"],
unit["loc"],
unit["stmt_count"],
unit["fingerprint"],
unit["loc_bucket"],
unit.get("cyclomatic_complexity", 1),
unit.get("nesting_depth", 0),
unit.get("risk", "low"),
unit.get("raw_hash", ""),
unit.get("entry_guard_count", 0),
unit.get("entry_guard_terminal_profile", "none"),
1 if unit.get("entry_guard_has_side_effect_before", False) else 0,
unit.get("terminal_kind", "fallthrough"),
unit.get("try_finally_profile", "none"),
unit.get("side_effect_order_profile", "none"),
]
for unit in units
]
def _encode_blocks(entry: CacheEntry, wire: dict[str, object]) -> None:
blocks = sorted(
entry["blocks"],
key=lambda block: (
block["qualname"],
block["start_line"],
block["end_line"],
block["block_hash"],
),
)
if blocks:
wire["b"] = [
[
block["qualname"],
block["start_line"],
block["end_line"],
block["size"],
block["block_hash"],
]
for block in blocks
]
def _encode_segments(entry: CacheEntry, wire: dict[str, object]) -> None:
segments = sorted(
entry["segments"],
key=lambda segment: (
segment["qualname"],
segment["start_line"],
segment["end_line"],
segment["segment_hash"],
),
)
if segments:
wire["s"] = [
[
segment["qualname"],
segment["start_line"],
segment["end_line"],
segment["size"],
segment["segment_hash"],
segment["segment_sig"],
]
for segment in segments
]
def _append_coupled_classes_row(
metric: ClassMetricsDict,
*,
rows: list[list[object]],
) -> None:
coupled_classes = _normalized_optional_string_list(
metric.get("coupled_classes", [])
)
if coupled_classes:
rows.append([metric["qualname"], coupled_classes])
def _encode_class_metrics(entry: CacheEntry, wire: dict[str, object]) -> None:
class_metrics = sorted(
entry["class_metrics"],
key=lambda metric: (
metric["start_line"],
metric["end_line"],
metric["qualname"],
),
)
if class_metrics:
coupled_classes_rows: list[list[object]] = []
wire["cm"] = [
[
metric["qualname"],
metric["start_line"],
metric["end_line"],
metric["cbo"],
metric["lcom4"],
metric["method_count"],
metric["instance_var_count"],
metric["risk_coupling"],
metric["risk_cohesion"],
]
for metric in class_metrics
]
for metric in class_metrics:
_append_coupled_classes_row(metric, rows=coupled_classes_rows)
if coupled_classes_rows:
wire["cc"] = coupled_classes_rows
def _encode_module_deps(entry: CacheEntry, wire: dict[str, object]) -> None:
module_deps = sorted(
entry["module_deps"],
key=lambda dep: (dep["source"], dep["target"], dep["import_type"], dep["line"]),
)
if module_deps:
wire["md"] = [
[
dep["source"],
dep["target"],
dep["import_type"],
dep["line"],
]
for dep in module_deps
]
def _encode_dead_candidates(entry: CacheEntry, wire: dict[str, object]) -> None:
dead_candidates = sorted(
entry["dead_candidates"],
key=lambda candidate: (
candidate["start_line"],
candidate["end_line"],
candidate["qualname"],
candidate["local_name"],
candidate["kind"],
),
)
if dead_candidates:
encoded_dead_candidates: list[list[object]] = []
for candidate in dead_candidates:
encoded = [
candidate["qualname"],
candidate["local_name"],
candidate["start_line"],
candidate["end_line"],
candidate["kind"],
]
suppressed_rules = candidate.get("suppressed_rules", [])
normalized_rules = _normalized_optional_string_list(suppressed_rules)
if normalized_rules:
encoded.append(normalized_rules)
encoded_dead_candidates.append(encoded)
wire["dc"] = encoded_dead_candidates
def _encode_name_lists(entry: CacheEntry, wire: dict[str, object]) -> None:
if entry["referenced_names"]:
wire["rn"] = sorted(set(entry["referenced_names"]))
if entry.get("referenced_qualnames"):
wire["rq"] = sorted(set(entry["referenced_qualnames"]))
if entry["import_names"]:
wire["in"] = sorted(set(entry["import_names"]))
if entry["class_names"]:
wire["cn"] = sorted(set(entry["class_names"]))
def _encode_security_surfaces(entry: CacheEntry, wire: dict[str, object]) -> None:
security_surfaces = sorted(
entry.get("security_surfaces", []),
key=lambda item: (
item["start_line"],
item["end_line"],
item["qualname"],
item["category"],
item["capability"],
item["evidence_symbol"],
),
)
if security_surfaces:
wire["sc"] = [
[
item["category"],
item["capability"],
item["module"],
item["qualname"],
item["start_line"],
item["end_line"],
item["location_scope"],
item["classification_mode"],
item["evidence_kind"],
item["evidence_symbol"],
]
for item in security_surfaces
]
def _encode_runtime_reachability(entry: CacheEntry, wire: dict[str, object]) -> None:
runtime_reachability = sorted(
entry.get("runtime_reachability", []),
key=lambda item: (
item["start_line"],
item["end_line"],
item["target_qualname"],
item["framework"],
item["edge_kind"],
item["evidence_symbol"],
),
)
if runtime_reachability:
wire["rr"] = [
[
item["target_qualname"],
item["start_line"],
item["end_line"],
item["target_kind"],
item["framework"],
item["edge_kind"],
item["confidence"],
item["evidence"],
item["evidence_symbol"],
item["source_qualname"],
]
for item in runtime_reachability
]
def _encode_optional_metrics_sections(
entry: CacheEntry, wire: dict[str, object]
) -> None:
typing_coverage = entry.get("typing_coverage")
if typing_coverage is not None:
wire["tc"] = [
typing_coverage["module"],
typing_coverage["callable_count"],
typing_coverage["params_total"],
typing_coverage["params_annotated"],
typing_coverage["returns_total"],
typing_coverage["returns_annotated"],
typing_coverage["any_annotation_count"],
]
docstring_coverage = entry.get("docstring_coverage")
if docstring_coverage is not None:
wire["dg"] = [
docstring_coverage["module"],
docstring_coverage["public_symbol_total"],
docstring_coverage["public_symbol_documented"],
]
api_surface = entry.get("api_surface")
if api_surface is not None:
wire["as"] = [
api_surface["module"],
sorted(set(api_surface.get("all_declared", []))),
[
[
symbol["qualname"],
symbol["kind"],
symbol["start_line"],
symbol["end_line"],
symbol.get("exported_via", "name"),
symbol.get("returns_hash", ""),
[
[
param["name"],
param["kind"],
1 if param["has_default"] else 0,
param.get("annotation_hash", ""),
]
for param in symbol.get("params", [])
],
]
for symbol in api_surface["symbols"]
],
]
def _encode_structural_findings(entry: CacheEntry, wire: dict[str, object]) -> None:
if "structural_findings" in entry:
structural_findings = entry.get("structural_findings", [])
wire["sf"] = [
[
group["finding_kind"],
group["finding_key"],
sorted(group["signature"].items()),
[
[item["qualname"], item["start"], item["end"]]
for item in group["items"]
],
]
for group in structural_findings
]
def _encode_wire_file_entry(entry: CacheEntry) -> dict[str, object]:
wire: dict[str, object] = {
"st": [entry["stat"]["mtime_ns"], entry["stat"]["size"]],
}
_encode_source_stats(entry, wire)
_encode_units(entry, wire)
_encode_blocks(entry, wire)
_encode_segments(entry, wire)
_encode_class_metrics(entry, wire)
_encode_module_deps(entry, wire)
_encode_dead_candidates(entry, wire)
_encode_name_lists(entry, wire)
_encode_runtime_reachability(entry, wire)
_encode_security_surfaces(entry, wire)
_encode_optional_metrics_sections(entry, wire)
_encode_structural_findings(entry, wire)
return wire
__all__ = ["_encode_wire_file_entry"]