Skip to content

Commit 8b86193

Browse files
committed
Add json output
1 parent f5b9f84 commit 8b86193

3 files changed

Lines changed: 39 additions & 10 deletions

File tree

src/freecad/addons/static_analyzer/check_package.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def _check_icon(package, name: str, issues: list[Issue], repo: Path) -> None:
112112
Issue(
113113
name,
114114
icon.sourceline,
115-
f"""Missing icon file '{file!s}'""",
115+
f"""Missing icon file '{icon.text}'""",
116116
)
117117
)
118118
else:
@@ -121,15 +121,15 @@ def _check_icon(package, name: str, issues: list[Issue], repo: Path) -> None:
121121
Issue(
122122
name,
123123
icon.sourceline,
124-
f"""Icon file '{file!s}' is too big (>{max_icon_size_kb}kB)""",
124+
f"""Icon file '{icon.text}' is too big (>{max_icon_size_kb}kB)""",
125125
)
126126
)
127127
if file.suffix.lower() not in (".svg", ".svgz"):
128128
issues.append(
129129
Issue(
130130
name,
131131
icon.sourceline,
132-
f"""Icon file '{file!s}' is not scalable (svg)""",
132+
f"""Icon file '{icon.text}' is not scalable (svg)""",
133133
)
134134
)
135135

src/freecad/addons/static_analyzer/format.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,32 @@
88

99
from __future__ import annotations
1010

11+
import json
12+
from collections import defaultdict
13+
from dataclasses import fields, is_dataclass
1114
from datetime import datetime, timezone
15+
from itertools import groupby
1216

1317
from . import template
1418
from .models import Analysis
1519

1620

21+
def serialize(data: object) -> object:
22+
if is_dataclass(data):
23+
return {f.name: serialize(getattr(data, f.name)) for f in fields(data)}
24+
match data:
25+
case str() | int() | float():
26+
return data
27+
case list() | tuple():
28+
return [serialize(v) for v in data]
29+
case dict() | defaultdict():
30+
return {k: serialize(v) for k, v in data.items()}
31+
case datetime():
32+
return data.isoformat()
33+
case _:
34+
return str(data)
35+
36+
1737
def report(reports: list[Analysis]) -> str:
1838
"""Assemble the final standalone HTML page."""
1939

@@ -39,3 +59,15 @@ def report(reports: list[Analysis]) -> str:
3959
}
4060

4161
return template.page_html.render(data)
62+
63+
64+
def report_json(reports: list[Analysis]) -> str:
65+
"""Dump report data as json"""
66+
reports.sort(key=lambda x: x.name)
67+
data = {
68+
"date": datetime.strftime(datetime.now(timezone.utc), "%Y-%m-%d"),
69+
"addons": {
70+
name: list(items) for name, items in groupby(reports, lambda r: r.name)
71+
},
72+
}
73+
return json.dumps(serialize(data), indent=2)

src/freecad/addons/static_analyzer/pipeline.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
from __future__ import annotations
1010

11-
from datetime import datetime, timezone
1211
import re
12+
from datetime import datetime, timezone
1313
from pathlib import Path
1414
from threading import Thread
1515

@@ -20,9 +20,9 @@
2020
from .check_package import check_package
2121
from .check_rules import check_rules
2222
from .check_stats import check_stats
23+
from .config import Config
2324
from .extract_deps import repo_requirements
2425
from .models import Analysis, Issue
25-
from .config import Config
2626

2727

2828
def search_files(base: Path, name: str, *, regex: bool = False) -> list[Path]:
@@ -37,11 +37,7 @@ def search_files(base: Path, name: str, *, regex: bool = False) -> list[Path]:
3737

3838
pattern = re.compile(name, re.IGNORECASE)
3939

40-
return [
41-
p
42-
for p in base.rglob("*")
43-
if pattern.fullmatch(str(p.relative_to(base)))
44-
]
40+
return [p for p in base.rglob("*") if pattern.fullmatch(str(p.relative_to(base)))]
4541

4642

4743
def check_file_present(
@@ -122,3 +118,4 @@ def start():
122118
date = datetime.strftime(datetime.now(timezone.utc), "%Y-%m-%d")
123119
reports.sort(key=lambda x: (-x.score, -x.stats.downloads_30d, x.name))
124120
(output_dir / f"report-{date}.html").write_text(fmt.report(reports))
121+
(output_dir / f"report-{date}.json").write_text(fmt.report_json(reports))

0 commit comments

Comments
 (0)