forked from ChelseaKR/qfer-preflight
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
307 lines (266 loc) · 10.4 KB
/
Copy pathcli.py
File metadata and controls
307 lines (266 loc) · 10.4 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
"""Command line interface.
Exit codes:
0 no error-level findings
1 at least one error-level finding, or, with --strict, anything the tool
could not reach a verdict on: a rule that was not evaluated, or an
advisory the reader raised
2 the tool was asked for something it could not do
"""
from __future__ import annotations
import argparse
import csv
import os
import sys
from collections.abc import Sequence
from pathlib import Path
from . import __version__
from .engine import TOOL_NAME, validate_path
from .model import BatchEntry, Status
from .profiles import PROFILES, QFER_PROGRAM_URL, Profile, detect_profiles, get_profile
from .report import (
batch_to_json,
batch_to_sarif,
batch_to_text,
report_to_sarif,
rules_to_json,
rules_to_text,
to_json,
to_text,
)
from .rules import RULE_SPECS, rules_for
EXIT_OK = 0
EXIT_FINDINGS = 1
EXIT_USAGE = 2
_EPILOG = (
"qfer-preflight runs entirely on your machine. It opens no network "
"connection, keeps no account and sends no telemetry.\n"
"It is an independent utility. It is not affiliated with, endorsed by or "
"approved by the California Energy Commission.\n"
f"Program page for the forms it reads: {QFER_PROGRAM_URL}"
)
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="qfer-preflight",
description=(
"Offline pre-submission validator for California Energy "
"Commission QFER Consumption CSV filings."
),
epilog=_EPILOG,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("--version", action="version", version=__version__)
sub = parser.add_subparsers(dest="command", required=True)
check = sub.add_parser("check", help="validate a CSV submission")
check.add_argument(
"paths",
nargs="+",
help=(
"path to the CSV file to validate, or several of them, or "
"directories, whose files are validated in name order"
),
)
check.add_argument(
"--profile",
help=(
"form profile, for example CEC-1306A-S1. Omitted, it is detected "
"from the file's header row, and only an exact match against one "
"published template is accepted"
),
)
check.add_argument(
"--format", choices=("text", "json", "sarif"), default="text", help="output format"
)
check.add_argument(
"--strict",
action="store_true",
help=(
"also exit non-zero when any rule could not be evaluated or the "
"reader raised an advisory"
),
)
rules = sub.add_parser("rules", help="list the rule registry with citations")
rules.add_argument("--profile", help="limit to the rules that apply to one profile")
rules.add_argument("--format", choices=("text", "json"), default="text", help="output format")
sub.add_parser("profiles", help="list the supported form profiles")
return parser
def _detect_profile(path: str) -> tuple[Profile | None, str | None]:
"""Read the file's header row and match it against the published templates.
Returns the one matching profile, or a refusal explaining why detection
declined to guess. Detection reads the header only; it never validates, and
a BOM stripped here is still reported by the validation run as ADV-BOM.
"""
try:
with open(path, newline="", encoding="utf-8-sig") as handle:
header = next(csv.reader(handle), None)
except OSError as exc:
return None, f"could not read {path}: {exc}"
except (UnicodeDecodeError, csv.Error):
return None, (
f"could not detect a profile for {path}: its first row could not "
"be read as UTF-8 CSV. Pass --profile explicitly to have the "
"report say what is wrong with it"
)
if not header:
return None, (
f"could not detect a profile for {path}: the file has no rows. "
"Pass --profile explicitly"
)
matches = detect_profiles(header)
if not matches:
return None, (
f"could not detect a profile for {path}: its header does not "
"match any published template byte for byte. Pass --profile "
"explicitly"
)
if len(matches) > 1:
ids = ", ".join(sorted(p.id for p in matches))
return None, (
f"could not detect a profile for {path}: its header matches "
f"several templates ({ids}). Pass --profile explicitly"
)
return matches[0], None
def _expand_inputs(paths: Sequence[str]) -> tuple[list[str] | None, str | None]:
"""Directories become their files, in name order; everything else passes through."""
expanded: list[str] = []
for path in paths:
if os.path.isdir(path):
inside = sorted(str(item) for item in Path(path).iterdir() if item.is_file())
if not inside:
return None, f"no files found in {path}"
expanded.extend(inside)
else:
expanded.append(path)
if len(set(expanded)) != len(expanded):
seen: set[str] = set()
deduped = []
for path in expanded:
if path not in seen:
deduped.append(path)
seen.add(path)
expanded = deduped
return expanded, None
def _validate_one(path: str, profile: Profile | None) -> BatchEntry:
"""Validate a single input for the batch, never raising.
Every refusal becomes an entry that says what happened, because in a batch
one unreadable file must not take the whole run down and must not be
silently skipped either.
"""
chosen: Profile | None = profile
if chosen is None:
detected, problem = _detect_profile(path)
if detected is None:
return BatchEntry(input_name=path, problem=problem or "profile detection failed")
chosen = detected
try:
return BatchEntry(input_name=path, report=validate_path(path, chosen))
except OSError as exc:
return BatchEntry(input_name=path, problem=f"could not read {path}: {exc}")
def _cmd_check(args: argparse.Namespace) -> int:
# Exactly one named file keeps the single-document output exactly as
# published, whatever it is: an existing filing, or a path that fails to
# open and reports its refusal on stderr as before. Anything else,
# including several paths and directories, produces the batch envelope.
single_request = len(args.paths) == 1 and not os.path.isdir(args.paths[0])
inputs, problem = _expand_inputs(args.paths)
if problem is not None or inputs is None:
print(problem or "no inputs", file=sys.stderr)
return EXIT_USAGE
if single_request:
return _check_single(inputs[0], args)
return _check_batch(inputs, args)
def _resolve_profile(args: argparse.Namespace) -> tuple[Profile | None, str | None]:
if args.profile:
try:
return get_profile(args.profile), None
except KeyError as exc:
return None, str(exc)
return None, None
def _check_single(path: str, args: argparse.Namespace) -> int:
profile, problem = _resolve_profile(args)
if problem is not None:
print(problem, file=sys.stderr)
return EXIT_USAGE
entry = _validate_one(path, profile)
if entry.report is None:
# Reachable when --profile was omitted and detection refused the
# header: single-file mode reports that refusal on stderr, exactly as
# detection did before batch mode existed.
print(entry.problem or "could not validate the input", file=sys.stderr)
return EXIT_USAGE
output = (
report_to_sarif(entry.report)
if args.format == "sarif"
else to_json(entry.report)
if args.format == "json"
else to_text(entry.report)
)
sys.stdout.write(output)
report = entry.report
if report.status is Status.FAIL:
return EXIT_FINDINGS
if args.strict and report.status is Status.UNVALIDATED:
return EXIT_FINDINGS
return EXIT_OK
def _check_batch(paths: Sequence[str], args: argparse.Namespace) -> int:
profile, problem = _resolve_profile(args)
if problem is not None:
print(problem, file=sys.stderr)
return EXIT_USAGE
entries = [_validate_one(path, profile) for path in paths]
output = (
batch_to_sarif(entries, TOOL_NAME, __version__)
if args.format == "sarif"
else batch_to_json(entries, TOOL_NAME, __version__)
if args.format == "json"
else batch_to_text(entries, TOOL_NAME)
)
sys.stdout.write(output)
statuses = [entry.report.status for entry in entries if entry.report is not None]
had_findings = any(
status is Status.FAIL or (args.strict and status is Status.UNVALIDATED)
for status in statuses
)
if had_findings:
return EXIT_FINDINGS
if any(entry.problem is not None for entry in entries):
return EXIT_USAGE
return EXIT_OK
def _cmd_rules(args: argparse.Namespace) -> int:
if args.profile:
try:
profile = get_profile(args.profile)
except KeyError as exc:
print(str(exc), file=sys.stderr)
return EXIT_USAGE
rules = list(rules_for(profile))
else:
# Bind every rule to the first profile it applies to, purely so that a
# citation can be rendered. The registry itself is profile agnostic.
rules = []
for spec in RULE_SPECS:
target = next((p for p in PROFILES.values() if spec.applies(p)), None)
if target is not None:
rules.append(spec.bind(target))
output = rules_to_json(rules) if args.format == "json" else rules_to_text(rules)
sys.stdout.write(output)
return EXIT_OK
def _cmd_profiles(_: argparse.Namespace) -> int:
for pid, profile in sorted(PROFILES.items()):
print(f"{pid}")
print(f" {profile.title}")
print(f" authority: {profile.authority}")
print(f" header : {','.join(profile.header)}")
print(f" template : {profile.template_url}")
print()
return EXIT_OK
def main(argv: Sequence[str] | None = None) -> int:
parser = build_parser()
args = parser.parse_args(argv)
handlers = {
"check": _cmd_check,
"rules": _cmd_rules,
"profiles": _cmd_profiles,
}
return handlers[args.command](args)
if __name__ == "__main__": # pragma: no cover
raise SystemExit(main())