reqtable 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,192 @@
1
+ """Read-only questions the GUI page asks while a form is being filled in: columns, tables, gates, baselines and systems.
2
+
3
+ Every function takes the form's query values, honours the same settings the CLI would, and reports a problem as an
4
+ ``error`` string instead of raising, so the page can show it beside the field it belongs to.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import csv
10
+ from pathlib import Path
11
+ from typing import Any, Mapping
12
+
13
+ from .baselines import find_gate, record_type, sort_manifests
14
+ from .cli import input_source, table_store
15
+ from .core import (
16
+ ReqtableError,
17
+ load_config,
18
+ prepare_trace_table,
19
+ read_csv_source,
20
+ resolve_gate_enforcement,
21
+ )
22
+ from .gui import guess_id_column, is_workbook_path
23
+ from .xlsx import Workbook, XlsxSelection, read_selection
24
+
25
+ PREVIEW_ROWS = 3
26
+ READ_ERRORS = (ReqtableError, OSError, UnicodeError, LookupError, csv.Error, ValueError)
27
+ TOGGLES = {"on": True, "off": False}
28
+
29
+
30
+ def resolve(workdir: Path, path: str) -> Path:
31
+ return workdir / path if path else workdir
32
+
33
+
34
+ def selection_of(query: Mapping[str, str]) -> XlsxSelection:
35
+ header_row = query.get("header_row", "")
36
+ return XlsxSelection(
37
+ query.get("table") or None,
38
+ query.get("sheet") or None,
39
+ int(header_row) if header_row else None,
40
+ )
41
+
42
+
43
+ def config_of(workdir: Path, query: Mapping[str, str]) -> dict[str, Any]:
44
+ return load_config(
45
+ str(resolve(workdir, query["config"])) if query.get("config") else None,
46
+ {
47
+ "id_column": query.get("id_column") or None,
48
+ "traceability": TOGGLES.get(query.get("traceability", "")),
49
+ "csv.encoding": query.get("encoding") or None,
50
+ "csv.delimiter": query.get("delimiter") or None,
51
+ },
52
+ )
53
+
54
+
55
+ def _preview(rows: list[list[str]]) -> dict[str, Any]:
56
+ data = [row for row in rows[1:] if any(cell.strip() for cell in row)]
57
+ return {
58
+ "columns": rows[0],
59
+ "row_count": len(data),
60
+ "rows": data[:PREVIEW_ROWS],
61
+ "id_guess": guess_id_column(rows[0]),
62
+ }
63
+
64
+
65
+ def columns(workdir: Path, query: Mapping[str, str]) -> dict[str, Any]:
66
+ path = resolve(workdir, query.get("path", ""))
67
+ try:
68
+ if is_workbook_path(str(path)):
69
+ return _preview(read_selection(path, selection_of(query)).rows)
70
+ config = config_of(
71
+ workdir, {k: v for k, v in query.items() if k != "id_column"}
72
+ )
73
+ with path.open("r", encoding=config["csv"]["encoding"], newline="") as handle:
74
+ reader = csv.reader(handle, delimiter=config["csv"]["delimiter"])
75
+ header = next(reader, [])
76
+ counted = [row for row in reader if any(cell.strip() for cell in row)]
77
+ return {**_preview([header, *counted]), "row_count": len(counted)}
78
+ except READ_ERRORS as exc:
79
+ return {
80
+ "columns": [],
81
+ "row_count": 0,
82
+ "rows": [],
83
+ "id_guess": "",
84
+ "error": str(exc),
85
+ }
86
+
87
+
88
+ def config_maps_workbook(workdir: Path, config: str) -> bool:
89
+ """Whether the settings file decides which tables are read, through xlsx.sources. A broken file decides nothing."""
90
+ if not config:
91
+ return False
92
+ try:
93
+ return bool(load_config(str(resolve(workdir, config)))["xlsx"]["sources"])
94
+ except ReqtableError:
95
+ return False
96
+
97
+
98
+ def tables(workdir: Path, query: Mapping[str, str]) -> dict[str, Any]:
99
+ maps = config_maps_workbook(workdir, query.get("config", ""))
100
+ try:
101
+ with Workbook.open(resolve(workdir, query.get("path", ""))) as workbook:
102
+ return {
103
+ "tables": [{"name": t.name, "sheet": t.sheet} for t in workbook.tables],
104
+ "sheets": [{"name": s.name, "state": s.state} for s in workbook.sheets],
105
+ "config_maps_workbook": maps,
106
+ }
107
+ except ReqtableError as exc:
108
+ return {
109
+ "tables": [],
110
+ "sheets": [],
111
+ "config_maps_workbook": maps,
112
+ "error": str(exc),
113
+ }
114
+
115
+
116
+ def gates(workdir: Path, query: Mapping[str, str]) -> dict[str, Any]:
117
+ try:
118
+ config = config_of(workdir, query)
119
+ except ReqtableError as exc:
120
+ return {"gates": [], "error": str(exc)}
121
+ result = []
122
+ for gate in config["gates"]:
123
+ resolved = resolve_gate_enforcement(gate, config["baselines"]["enforcement"])
124
+ result.append(
125
+ {
126
+ "id": gate["id"],
127
+ "name": gate.get("name", gate["id"]),
128
+ "scope": find_gate(config, gate["id"]).scope,
129
+ "roles": [
130
+ {"role": entry["role"], "min": entry["min"]}
131
+ for entry in gate.get("signoff", [])
132
+ ],
133
+ "checks": [
134
+ {
135
+ "id": check["id"],
136
+ "description": check["description"],
137
+ "enforcement": check["enforcement"],
138
+ }
139
+ for check in resolved["checks"]
140
+ ],
141
+ }
142
+ )
143
+ return {"gates": result, "store": config["baselines"]["store"]}
144
+
145
+
146
+ def baselines(workdir: Path, query: Mapping[str, str]) -> dict[str, Any]:
147
+ path = query.get("path", "")
148
+ if not path:
149
+ return {"baselines": []}
150
+ try:
151
+ config = config_of(workdir, query)
152
+ store = table_store(str(resolve(workdir, path)), selection_of(query), config)
153
+ found = sort_manifests(store.list(), config)
154
+ except READ_ERRORS as exc:
155
+ return {"baselines": [], "error": str(exc)}
156
+ return {
157
+ "baselines": [
158
+ {
159
+ "name": m.name,
160
+ "gate": m.gate,
161
+ "system": m.system or "",
162
+ "created": m.created,
163
+ "created_by": m.created_by,
164
+ }
165
+ for m in found
166
+ ],
167
+ "store": config["baselines"]["store"],
168
+ }
169
+
170
+
171
+ def systems(workdir: Path, query: Mapping[str, str]) -> dict[str, Any]:
172
+ path = query.get("path", "")
173
+ if not path:
174
+ return {"systems": []}
175
+ try:
176
+ config = config_of(workdir, query)
177
+ table = read_csv_source(
178
+ input_source(str(resolve(workdir, path)), selection_of(query), config),
179
+ config,
180
+ )
181
+ if config["traceability"]:
182
+ table, _ = prepare_trace_table(table, config)
183
+ except READ_ERRORS as exc:
184
+ return {"systems": [], "error": str(exc)}
185
+ id_column = config["id_column"]
186
+ return {
187
+ "systems": [
188
+ {"id": row[id_column], "name": row.get("Name", "")}
189
+ for row in table.rows
190
+ if record_type(row) == "System"
191
+ ]
192
+ }