forked from ChelseaKR/ceqa-preflight
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
374 lines (328 loc) · 12.7 KB
/
Copy pathcli.py
File metadata and controls
374 lines (328 loc) · 12.7 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
"""Command-line interface for CEQA Preflight."""
from __future__ import annotations
import json
from pathlib import Path
from typing import Annotated
import typer
from ceqa_preflight import __version__
from ceqa_preflight.ai.cli import ai_app
from ceqa_preflight.checker import check_package
from ceqa_preflight.manifest import ManifestError, load_manifest
from ceqa_preflight.models import FilingType
from ceqa_preflight.observability import configure_logging, event
from ceqa_preflight.package_loader import PackageLoadError
from ceqa_preflight.pilot import PilotDataError, summarize_pilot, write_pilot_templates
from ceqa_preflight.reporting import (
render_checklist,
render_console,
render_html,
render_json,
summarize_counts,
)
from ceqa_preflight.rule_registry import default_catalog
from ceqa_preflight.scaffold import write_manifest_template
from ceqa_preflight.synth import SyntheticDefect, write_synthetic_package
app = typer.Typer(
name="ceqa-preflight",
help=(
"Local, advisory preflight checks for CEQA Submit filing packages. "
"This tool does not provide legal advice or determine CEQA compliance."
),
no_args_is_help=True,
)
rules_app = typer.Typer(help="Inspect the built-in, source-cited rule catalog.")
pilot_app = typer.Typer(help="Create and summarize privacy-preserving pilot evidence files.")
app.add_typer(rules_app, name="rules")
app.add_typer(pilot_app, name="pilot")
app.add_typer(ai_app, name="ai")
def _show_version(value: bool) -> None:
if value:
typer.echo(__version__)
raise typer.Exit()
@app.callback()
def main(
version: Annotated[
bool,
typer.Option(
"--version",
callback=_show_version,
is_eager=True,
help="Show the installed CEQA Preflight version and exit.",
),
] = False,
log_format: Annotated[
str,
typer.Option("--log-format", help="Operational logs: text (off) or json (stderr)."),
] = "text",
) -> None:
"""Run local, advisory checks without uploading filing packages."""
if log_format not in {"text", "json"}:
raise typer.BadParameter("must be text or json", param_hint="--log-format")
configure_logging(log_format)
@app.command()
def version() -> None:
"""Show the installed CEQA Preflight version."""
typer.echo(__version__)
_RENDERERS = {
"console": render_console,
"html": render_html,
"json": render_json,
"checklist": render_checklist,
}
_REPORT_SUFFIXES = {"console": "txt", "html": "html", "json": "json", "checklist": "txt"}
def _parse_rule_ids(raw: str | None) -> set[str] | None:
if raw is None:
return None
identifiers = {token.strip().upper() for token in raw.split(",") if token.strip()}
if not identifiers:
raise typer.BadParameter("expected a comma-separated list of rule identifiers")
return identifiers
def _report_stem(source: Path, index: int, total: int) -> str:
if total == 1:
return "report"
safe = "".join(
character if character.isalnum() or character in "-_" else "-" for character in source.stem
).strip("-")
return f"report-{index + 1:02d}-{safe}" if safe else f"report-{index + 1:02d}"
@app.command()
def check(
sources: Annotated[
list[Path],
typer.Argument(
exists=True, readable=True, help="One or more package directories or ZIP files."
),
],
filing_type: Annotated[
FilingType,
typer.Option("--filing-type", help="The intended NOD or NOE filing type."),
],
manifest_path: Annotated[
Path | None,
typer.Option(
"--manifest",
help="Optional local YAML or JSON package manifest (single package only).",
),
] = None,
output_format: Annotated[
str,
typer.Option("--format", help="Report format: console, json, html, or checklist."),
] = "console",
output: Annotated[
Path | None,
typer.Option("--output", help="Optional directory for generated report files."),
] = None,
include_experimental: Annotated[
bool,
typer.Option(
"--include-experimental",
help="Include filing-specific pilot rules; they are advisory and not release-ready.",
),
] = False,
rules: Annotated[
str | None,
typer.Option("--rules", help="Run only these comma-separated rule identifiers."),
] = None,
exclude_rules: Annotated[
str | None,
typer.Option("--exclude-rules", help="Skip these comma-separated rule identifiers."),
] = None,
) -> None:
"""Inspect local packages without uploading or changing their source files."""
if output_format not in _RENDERERS:
raise typer.BadParameter("must be console, json, html, or checklist", param_hint="--format")
if manifest_path is not None and len(sources) > 1:
raise typer.BadParameter("a manifest applies to a single package", param_hint="--manifest")
rule_ids = _parse_rule_ids(rules)
exclude_rule_ids = _parse_rule_ids(exclude_rules)
worst_exit_code = 0
batch_lines: list[str] = []
for index, source in enumerate(sources):
try:
event("check_started", filing_type=filing_type.value, report_format=output_format)
manifest = load_manifest(manifest_path) if manifest_path is not None else None
report, exit_code = check_package(
source,
filing_type,
manifest=manifest,
include_experimental=include_experimental,
rule_ids=rule_ids,
exclude_rule_ids=exclude_rule_ids,
)
except (ManifestError, PackageLoadError, ValueError) as error:
typer.echo(f"Input error: {error}", err=True)
raise typer.Exit(code=2) from error
rendered = _RENDERERS[output_format](report)
if output is None:
if len(sources) > 1:
typer.echo(f"== Package: {source}")
typer.echo(rendered, nl=False)
else:
output.mkdir(parents=True, exist_ok=True)
stem = _report_stem(source, index, len(sources))
destination = output / f"{stem}.{_REPORT_SUFFIXES[output_format]}"
destination.write_text(rendered, encoding="utf-8")
typer.echo(f"Wrote advisory report to {destination}")
counts = summarize_counts(report)
batch_lines.append(
f"{source}: exit {exit_code}, {counts['failure']} failure(s), "
f"{counts['warning']} warning(s), {counts['manual']} manual-review item(s), "
f"{counts['not_run']} check(s) not run"
)
worst_exit_code = max(worst_exit_code, exit_code)
event(
"check_completed",
exit_code=exit_code,
findings=len(report.findings),
not_run=len(report.not_run),
)
if len(sources) > 1:
typer.echo("Batch summary")
for line in batch_lines:
typer.echo(f" {line}")
raise typer.Exit(code=worst_exit_code)
@app.command()
def init(
directory: Annotated[
Path, typer.Argument(help="Directory where package.yaml will be created.")
],
filing_type: Annotated[
FilingType,
typer.Option("--filing-type", help="The intended NOD or NOE filing type."),
],
from_package: Annotated[
bool,
typer.Option(
"--from-package",
help="Prepopulate document paths from PDFs already in the directory.",
),
] = False,
) -> None:
"""Create a non-overwriting, schema-valid local manifest template."""
try:
destination = write_manifest_template(directory, filing_type, from_package=from_package)
except (FileExistsError, ValueError) as error:
typer.echo(f"Input error: {error}", err=True)
raise typer.Exit(code=2) from error
typer.echo(f"Created manifest template at {destination}")
@app.command()
def synth(
directory: Annotated[
Path, typer.Argument(help="Empty or new directory for the synthetic package.")
],
filing_type: Annotated[
FilingType,
typer.Option("--filing-type", help="The intended NOD or NOE filing type."),
],
defect: Annotated[
list[SyntheticDefect] | None,
typer.Option("--defect", help="Seed an objective, detectable defect; repeatable."),
] = None,
) -> None:
"""Create a plainly fictional synthetic package for demos, tests, and pilot calibration."""
try:
created = write_synthetic_package(directory, filing_type, list(defect or []))
except (FileExistsError, ValueError) as error:
typer.echo(f"Input error: {error}", err=True)
raise typer.Exit(code=2) from error
typer.echo(f"Created a synthetic {filing_type.value} package with {len(created)} file(s):")
for path in created:
typer.echo(f" {path}")
@rules_app.command("list")
def list_rules(
filing_type: Annotated[
FilingType | None,
typer.Option("--filing-type", help="Optionally filter rules to one filing type."),
] = None,
output_format: Annotated[
str,
typer.Option("--format", help="Listing format: console or json."),
] = "console",
) -> None:
"""List rule identifiers, lifecycle, and source titles."""
if output_format not in {"console", "json"}:
raise typer.BadParameter("must be console or json", param_hint="--format")
catalog = default_catalog(filing_type)
if output_format == "json":
typer.echo(
json.dumps(
[rule.model_dump(mode="json") for rule in catalog.rules],
indent=2,
sort_keys=True,
)
)
return
for rule in catalog.rules:
typer.echo(f"{rule.id}\t{rule.lifecycle}\t{rule.title}\t{rule.source.title}")
@rules_app.command("show")
def show_rule(
rule_id: Annotated[str, typer.Argument(help="Rule identifier, for example NOE-001.")],
) -> None:
"""Show the full source-cited metadata for one built-in rule."""
normalized_id = rule_id.upper()
for rule in default_catalog().rules:
if rule.id == normalized_id:
typer.echo(rule.model_dump_json(indent=2))
return
typer.echo(f"Unknown rule identifier: {rule_id}", err=True)
raise typer.Exit(code=2)
@pilot_app.command("init")
def init_pilot(
directory: Annotated[
Path, typer.Argument(help="Directory where controlled-label CSV templates will be created.")
],
) -> None:
"""Create non-overwriting, package-content-free pilot evidence templates."""
try:
review_path, baseline_path = write_pilot_templates(directory)
except FileExistsError as error:
typer.echo(f"Input error: {error}", err=True)
raise typer.Exit(code=2) from error
typer.echo(f"Created pilot templates: {review_path.name}, {baseline_path.name}")
@pilot_app.command("summarize")
def summarize_pilot_data(
reviews: Annotated[
Path,
typer.Option(
"--reviews", exists=True, readable=True, help="Controlled-label finding review CSV."
),
],
baseline: Annotated[
Path,
typer.Option("--baseline", exists=True, readable=True, help="Manual baseline issue CSV."),
],
output_format: Annotated[
str,
typer.Option("--format", help="Summary format: console or json."),
] = "console",
) -> None:
"""Summarize pilot metrics without reading filing packages or free-text notes."""
if output_format not in {"console", "json"}:
raise typer.BadParameter("must be console or json", param_hint="--format")
try:
summary = summarize_pilot(reviews, baseline)
except PilotDataError as error:
typer.echo(f"Input error: {error}", err=True)
raise typer.Exit(code=2) from error
event("pilot_summarized", reviewed_findings=summary.reviewed_findings)
if output_format == "json":
typer.echo(summary.model_dump_json(indent=2))
return
typer.echo(
"\n".join(
(
"CEQA Preflight pilot summary",
f"Decision: {summary.go_no_go}",
f"Reviewed findings: {summary.reviewed_findings}",
f"Reviewed packages: {summary.reviewed_packages}",
f"Actionable precision: {_format_rate(summary.actionable_precision)}",
"High-severity false-negative rate: "
f"{_format_rate(summary.high_severity_false_negative_rate)}",
f"Median report time: {_format_seconds(summary.median_report_seconds)}",
*summary.reasons,
)
)
)
def _format_rate(value: float | None) -> str:
return "not measured" if value is None else f"{value:.1%}"
def _format_seconds(value: float | None) -> str:
return "not measured" if value is None else f"{value:.1f} seconds"