forked from ChelseaKR/power-content-check
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.py
More file actions
192 lines (161 loc) · 6.74 KB
/
Copy pathreport.py
File metadata and controls
192 lines (161 loc) · 6.74 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
"""Rendering a run as text or as JSON."""
from __future__ import annotations
import json
import textwrap
from typing import Any
from .checks import BY_ID
from .model import DocumentReport, ExitCode, Readability, RunReport, Status
_MARK = {
Status.CONFORMS: " ok ",
Status.DOES_NOT_CONFORM: " FAIL ",
Status.NOT_EVALUATED: " n/e ",
}
_WIDTH = 88
#: How many skipped file names the text report prints before summarising the
#: rest. A display choice and nothing more: the JSON always carries every one.
_SKIPPED_SHOWN = 10
def _wrap(text: str, indent: str) -> str:
return textwrap.fill(
text,
width=_WIDTH,
initial_indent=indent,
subsequent_indent=indent,
)
def render_json(report: RunReport) -> str:
return json.dumps(report.to_dict(), indent=2, sort_keys=False, ensure_ascii=False)
def _render_document(document: DocumentReport, verbose: bool) -> list[str]:
lines: list[str] = ["", document.path, "-" * min(len(document.path), _WIDTH)]
if document.readability is Readability.UNREADABLE:
lines.append(" NOT EVALUATED: this document could not be read.")
lines.append(_wrap(f"Reason: {document.unreadable_reason}", " "))
lines.append(
_wrap(
"No check was run. An unreadable document is never reported as conforming.",
" ",
)
)
return lines
if document.extraction_basis:
lines.append(_wrap(document.extraction_basis, " "))
for result in document.results:
spec = BY_ID[result.check_id].spec
if result.status is Status.NOT_EVALUATED and not spec.implemented and not verbose:
continue
if result.status is Status.CONFORMS and not verbose:
continue
lines.append(f" [{_MARK[result.status]}] {result.check_id} {spec.title}")
lines.append(_wrap(result.finding, " "))
if result.detail:
lines.append(_wrap(result.detail, " "))
lines.append(
_wrap(
f"Cited: {spec.citation.locator} of {spec.citation.source.key} "
f"<{spec.citation.source.url}>",
" ",
)
)
counts = document.counts
lines.append(
" Summary: "
f"{counts[Status.CONFORMS.value]} conform, "
f"{counts[Status.DOES_NOT_CONFORM.value]} do not conform, "
f"{counts[Status.NOT_EVALUATED.value]} not evaluated."
)
return lines
def _render_skipped(report: RunReport) -> list[str]:
"""Name the files that were in the folder and were not read.
A directory expands to the label formats this tool reads. Anything else in
it is dropped, and dropping it in silence is what this block prevents: the
Energy Commission publishes a second rendering of each label beside it, and
a reader is entitled to know which of the two the report is about. Nothing
here is a finding and none of it is in any count.
"""
if not report.skipped:
return []
count = len(report.skipped)
one = count == 1
lines = [
"",
_wrap(
f"{count} {'file' if one else 'files'} in the directories given "
f"{'is' if one else 'are'} not a format this tool reads, so nothing above "
f"is a statement about {'it' if one else 'them'}:",
"",
),
]
lines += [f" {path}" for path in report.skipped[:_SKIPPED_SHOWN]]
remainder = count - _SKIPPED_SHOWN
if remainder > 0:
lines.append(f" and {remainder} more, all of them in the JSON output.")
return lines
def render_text(report: RunReport, verbose: bool = False) -> str:
lines: list[str] = [
f"{report.tool} {report.tool_version}",
f"Ruleset: {report.ruleset_id} (effective {report.ruleset_effective})",
"",
_wrap(report.notice, ""),
]
if not report.documents:
lines += [
"",
"NOTHING CHECKED.",
_wrap(
"No label was checked, so no statement about conformance can be made. "
"This is not a pass. Check the paths you supplied.",
" ",
),
]
lines += _render_skipped(report)
return "\n".join(lines)
for document in report.documents:
lines += _render_document(document, verbose)
lines += _render_skipped(report)
summary = report.summary
lines += [
"",
"=" * _WIDTH,
f"Documents checked: {summary['documents_checked']}",
f" readable: {summary['documents_readable']}",
f" unreadable: {summary['documents_unreadable']}",
f"Checks conforming: {summary['conforms']}",
f"Checks not conforming:{summary['does_not_conform']}",
f"Checks not evaluated: {summary['not_evaluated']}",
f"Exit code: {report.exit_code} ({_exit_meaning(report.exit_code)})",
]
if not verbose:
lines.append("Pass --verbose to list conforming and unimplemented checks too.")
return "\n".join(lines)
def _exit_meaning(code: int) -> str:
return {
ExitCode.OK: "readable throughout, no deviation found",
ExitCode.NONCONFORMANCE: "at least one deviation from the prescribed format",
ExitCode.NOT_EVALUATED: "at least one check could not be evaluated",
ExitCode.NOTHING_CHECKED: "nothing was checked, which is not a pass",
ExitCode.USAGE_ERROR: "usage error",
}.get(code, "unknown")
def render_catalog(as_json: bool = False) -> str:
"""Print the catalog itself, so the rules are auditable without a label."""
from .checks import CHECKS
if as_json:
payload: list[dict[str, Any]] = [c.spec.to_dict() for c in CHECKS]
return json.dumps(payload, indent=2, ensure_ascii=False)
lines: list[str] = []
for registered in CHECKS:
spec = registered.spec
state = "implemented" if spec.implemented else "REGISTERED, ENFORCES NOTHING"
if spec.blocker:
state = f"{state}, {spec.blocker.value}"
lines.append(f"{spec.id} {spec.title} [{state}, basis: {spec.basis.value}]")
lines.append(_wrap(f"Cites: {spec.citation.locator} of", " "))
lines.append(_wrap(spec.citation.source.title, " "))
lines.append(_wrap(f"URL: {spec.citation.source.url}", " "))
lines.append(_wrap(f'Quote: "{spec.citation.quote}"', " "))
if spec.implemented:
lines.append(_wrap(f"Looks for: {spec.what_it_looks_for}", " "))
else:
blocker = spec.blocker.value if spec.blocker else "unclassified"
lines.append(
_wrap(f"Why not implemented [{blocker}]: {spec.unimplemented_reason}", " ")
)
lines.append("")
return "\n".join(lines)