quantumgui 1.0.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.
- quantumgui/__init__.py +9 -0
- quantumgui/__main__.py +6 -0
- quantumgui/cli.py +341 -0
- quantumgui/core/__init__.py +39 -0
- quantumgui/core/animation.py +221 -0
- quantumgui/core/audit.py +323 -0
- quantumgui/core/context.py +54 -0
- quantumgui/core/data_source.py +426 -0
- quantumgui/core/draw.py +428 -0
- quantumgui/core/enterprise.py +402 -0
- quantumgui/core/enterprise_app.py +419 -0
- quantumgui/core/events.py +34 -0
- quantumgui/core/geometry.py +108 -0
- quantumgui/core/native_dialogs.py +276 -0
- quantumgui/core/pdf_report.py +390 -0
- quantumgui/core/rules.py +284 -0
- quantumgui/core/state.py +42 -0
- quantumgui/core/task_runner.py +181 -0
- quantumgui/core/widget.py +498 -0
- quantumgui/core/window.py +920 -0
- quantumgui/core/xlsx_export.py +145 -0
- quantumgui/py.typed +0 -0
- quantumgui/theme/__init__.py +32 -0
- quantumgui/theme/hct.py +191 -0
- quantumgui/theme/theme.py +93 -0
- quantumgui/theme/tokens.py +423 -0
- quantumgui/ui.py +2142 -0
- quantumgui/widgets/__init__.py +168 -0
- quantumgui/widgets/advanced.py +499 -0
- quantumgui/widgets/button.py +224 -0
- quantumgui/widgets/charts.py +625 -0
- quantumgui/widgets/charts_bi.py +528 -0
- quantumgui/widgets/charts_extra.py +430 -0
- quantumgui/widgets/charts_pro.py +568 -0
- quantumgui/widgets/checkbox.py +199 -0
- quantumgui/widgets/chip.py +219 -0
- quantumgui/widgets/containers.py +321 -0
- quantumgui/widgets/data_controls.py +487 -0
- quantumgui/widgets/data_grid.py +956 -0
- quantumgui/widgets/data_table.py +551 -0
- quantumgui/widgets/date_picker.py +407 -0
- quantumgui/widgets/display_media.py +372 -0
- quantumgui/widgets/display_pro.py +407 -0
- quantumgui/widgets/drawer.py +252 -0
- quantumgui/widgets/enterprise_controls.py +934 -0
- quantumgui/widgets/fab.py +261 -0
- quantumgui/widgets/forms_pro.py +703 -0
- quantumgui/widgets/forms_pro_media.py +587 -0
- quantumgui/widgets/inputs.py +870 -0
- quantumgui/widgets/layout_pro.py +162 -0
- quantumgui/widgets/list_view.py +307 -0
- quantumgui/widgets/masked_input.py +131 -0
- quantumgui/widgets/master_detail.py +155 -0
- quantumgui/widgets/navigation.py +382 -0
- quantumgui/widgets/overlays.py +355 -0
- quantumgui/widgets/productivity.py +500 -0
- quantumgui/widgets/productivity_boards.py +544 -0
- quantumgui/widgets/radio.py +209 -0
- quantumgui/widgets/scroll_view.py +129 -0
- quantumgui/widgets/select.py +373 -0
- quantumgui/widgets/sheets.py +249 -0
- quantumgui/widgets/slider.py +209 -0
- quantumgui/widgets/switch.py +196 -0
- quantumgui/widgets/tabs.py +175 -0
- quantumgui/widgets/text.py +101 -0
- quantumgui/widgets/text_field.py +490 -0
- quantumgui/widgets/time_picker.py +499 -0
- quantumgui-1.0.0.dist-info/METADATA +137 -0
- quantumgui-1.0.0.dist-info/RECORD +73 -0
- quantumgui-1.0.0.dist-info/WHEEL +5 -0
- quantumgui-1.0.0.dist-info/entry_points.txt +2 -0
- quantumgui-1.0.0.dist-info/licenses/LICENSE +32 -0
- quantumgui-1.0.0.dist-info/top_level.txt +1 -0
quantumgui/core/audit.py
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
"""Enterprise Audit Trail (`ui.audit_log`) & Undo/Redo Command History (`ui.history`)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import csv
|
|
6
|
+
import json
|
|
7
|
+
import sqlite3
|
|
8
|
+
from dataclasses import asdict, dataclass, field as dc_field
|
|
9
|
+
from datetime import datetime, timezone
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Any, Callable
|
|
12
|
+
|
|
13
|
+
from .enterprise_app import AuthContext, default_auth
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True)
|
|
17
|
+
class AuditEntry:
|
|
18
|
+
"""Single immutable audit trail record."""
|
|
19
|
+
|
|
20
|
+
timestamp: str
|
|
21
|
+
user: str
|
|
22
|
+
action: str
|
|
23
|
+
entity: str
|
|
24
|
+
record_id: str | None = None
|
|
25
|
+
field: str | None = None
|
|
26
|
+
old_value: Any = None
|
|
27
|
+
new_value: Any = None
|
|
28
|
+
metadata: dict[str, Any] = dc_field(default_factory=dict)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class AuditLog:
|
|
32
|
+
"""Persistent or in-memory Enterprise Audit Trail (`ui.audit_log`).
|
|
33
|
+
|
|
34
|
+
Supports memory, JSONL file (`"audit.jsonl"`), or SQLite database (`"audit.db"`).
|
|
35
|
+
Automatically resolves the active user from `AuthContext` (`ui.auth`).
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def __init__(
|
|
39
|
+
self,
|
|
40
|
+
storage: str | Path | sqlite3.Connection | None = None,
|
|
41
|
+
*,
|
|
42
|
+
auth: AuthContext | None = None,
|
|
43
|
+
) -> None:
|
|
44
|
+
self.auth: AuthContext = auth if auth is not None else default_auth
|
|
45
|
+
self._entries: list[AuditEntry] = []
|
|
46
|
+
self._listeners: list[Callable[[AuditEntry], None]] = []
|
|
47
|
+
self._jsonl_path: Path | None = None
|
|
48
|
+
self._conn: sqlite3.Connection | None = None
|
|
49
|
+
self._owns_conn: bool = False
|
|
50
|
+
|
|
51
|
+
if isinstance(storage, sqlite3.Connection):
|
|
52
|
+
self._conn = storage
|
|
53
|
+
self._init_sqlite()
|
|
54
|
+
elif isinstance(storage, (str, Path)):
|
|
55
|
+
p = Path(storage)
|
|
56
|
+
p.parent.mkdir(parents=True, exist_ok=True)
|
|
57
|
+
if p.suffix.lower() in (".db", ".sqlite", ".sqlite3"):
|
|
58
|
+
self._conn = sqlite3.connect(str(p), check_same_thread=False)
|
|
59
|
+
self._owns_conn = True
|
|
60
|
+
self._init_sqlite()
|
|
61
|
+
else:
|
|
62
|
+
self._jsonl_path = p
|
|
63
|
+
|
|
64
|
+
def _init_sqlite(self) -> None:
|
|
65
|
+
assert self._conn is not None
|
|
66
|
+
self._conn.execute(
|
|
67
|
+
"""
|
|
68
|
+
CREATE TABLE IF NOT EXISTS audit_log (
|
|
69
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
70
|
+
timestamp TEXT NOT NULL,
|
|
71
|
+
user TEXT NOT NULL,
|
|
72
|
+
action TEXT NOT NULL,
|
|
73
|
+
entity TEXT NOT NULL,
|
|
74
|
+
record_id TEXT,
|
|
75
|
+
field TEXT,
|
|
76
|
+
old_value TEXT,
|
|
77
|
+
new_value TEXT,
|
|
78
|
+
metadata TEXT
|
|
79
|
+
)
|
|
80
|
+
"""
|
|
81
|
+
)
|
|
82
|
+
self._conn.commit()
|
|
83
|
+
|
|
84
|
+
def subscribe(self, fn: Callable[[AuditEntry], None]) -> AuditLog:
|
|
85
|
+
if fn not in self._listeners:
|
|
86
|
+
self._listeners.append(fn)
|
|
87
|
+
return self
|
|
88
|
+
|
|
89
|
+
def record(
|
|
90
|
+
self,
|
|
91
|
+
action: str,
|
|
92
|
+
entity: str,
|
|
93
|
+
*,
|
|
94
|
+
record_id: Any = None,
|
|
95
|
+
field: str | None = None,
|
|
96
|
+
old_value: Any = None,
|
|
97
|
+
new_value: Any = None,
|
|
98
|
+
user: str | None = None,
|
|
99
|
+
metadata: dict[str, Any] | None = None,
|
|
100
|
+
) -> AuditEntry:
|
|
101
|
+
entry = AuditEntry(
|
|
102
|
+
timestamp=datetime.now(timezone.utc).isoformat(timespec="seconds"),
|
|
103
|
+
user=user if user is not None else (self.auth.user or "system"),
|
|
104
|
+
action=action,
|
|
105
|
+
entity=entity,
|
|
106
|
+
record_id=None if record_id is None else str(record_id),
|
|
107
|
+
field=field,
|
|
108
|
+
old_value=old_value,
|
|
109
|
+
new_value=new_value,
|
|
110
|
+
metadata=dict(metadata) if metadata else {},
|
|
111
|
+
)
|
|
112
|
+
self._entries.append(entry)
|
|
113
|
+
|
|
114
|
+
if self._jsonl_path is not None:
|
|
115
|
+
with self._jsonl_path.open("a", encoding="utf-8") as fh:
|
|
116
|
+
fh.write(json.dumps(asdict(entry), ensure_ascii=False, default=str) + "\n")
|
|
117
|
+
elif self._conn is not None:
|
|
118
|
+
self._conn.execute(
|
|
119
|
+
"""
|
|
120
|
+
INSERT INTO audit_log
|
|
121
|
+
(timestamp, user, action, entity, record_id, field, old_value, new_value, metadata)
|
|
122
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
123
|
+
""",
|
|
124
|
+
(
|
|
125
|
+
entry.timestamp,
|
|
126
|
+
entry.user,
|
|
127
|
+
entry.action,
|
|
128
|
+
entry.entity,
|
|
129
|
+
entry.record_id,
|
|
130
|
+
entry.field,
|
|
131
|
+
json.dumps(entry.old_value, ensure_ascii=False, default=str),
|
|
132
|
+
json.dumps(entry.new_value, ensure_ascii=False, default=str),
|
|
133
|
+
json.dumps(entry.metadata, ensure_ascii=False, default=str),
|
|
134
|
+
),
|
|
135
|
+
)
|
|
136
|
+
self._conn.commit()
|
|
137
|
+
|
|
138
|
+
for fn in list(self._listeners):
|
|
139
|
+
fn(entry)
|
|
140
|
+
return entry
|
|
141
|
+
|
|
142
|
+
def query(
|
|
143
|
+
self,
|
|
144
|
+
*,
|
|
145
|
+
entity: str | None = None,
|
|
146
|
+
user: str | None = None,
|
|
147
|
+
action: str | None = None,
|
|
148
|
+
limit: int = 200,
|
|
149
|
+
) -> list[AuditEntry]:
|
|
150
|
+
if self._conn is not None:
|
|
151
|
+
clauses: list[str] = []
|
|
152
|
+
params: list[Any] = []
|
|
153
|
+
if entity is not None:
|
|
154
|
+
clauses.append("entity = ?")
|
|
155
|
+
params.append(entity)
|
|
156
|
+
if user is not None:
|
|
157
|
+
clauses.append("user = ?")
|
|
158
|
+
params.append(user)
|
|
159
|
+
if action is not None:
|
|
160
|
+
clauses.append("action = ?")
|
|
161
|
+
params.append(action)
|
|
162
|
+
where = f"WHERE {' AND '.join(clauses)}" if clauses else ""
|
|
163
|
+
sql = (
|
|
164
|
+
f"SELECT timestamp, user, action, entity, record_id, field, old_value, new_value, metadata "
|
|
165
|
+
f"FROM audit_log {where} ORDER BY id DESC LIMIT ?"
|
|
166
|
+
)
|
|
167
|
+
params.append(int(limit))
|
|
168
|
+
rows = self._conn.execute(sql, tuple(params)).fetchall()
|
|
169
|
+
out: list[AuditEntry] = []
|
|
170
|
+
for r in rows:
|
|
171
|
+
out.append(
|
|
172
|
+
AuditEntry(
|
|
173
|
+
timestamp=r[0],
|
|
174
|
+
user=r[1],
|
|
175
|
+
action=r[2],
|
|
176
|
+
entity=r[3],
|
|
177
|
+
record_id=r[4],
|
|
178
|
+
field=r[5],
|
|
179
|
+
old_value=json.loads(r[6]) if r[6] is not None else None,
|
|
180
|
+
new_value=json.loads(r[7]) if r[7] is not None else None,
|
|
181
|
+
metadata=json.loads(r[8]) if r[8] else {},
|
|
182
|
+
)
|
|
183
|
+
)
|
|
184
|
+
return out
|
|
185
|
+
|
|
186
|
+
filtered = [
|
|
187
|
+
e
|
|
188
|
+
for e in reversed(self._entries)
|
|
189
|
+
if (entity is None or e.entity == entity)
|
|
190
|
+
and (user is None or e.user == user)
|
|
191
|
+
and (action is None or e.action == action)
|
|
192
|
+
]
|
|
193
|
+
return filtered[:limit]
|
|
194
|
+
|
|
195
|
+
def attach_grid(self, grid: Any, *, entity: str = "grid") -> AuditLog:
|
|
196
|
+
"""Automatically log every inline cell edit performed on a `DataGrid`."""
|
|
197
|
+
prev = getattr(grid, "on_cell_edit", None)
|
|
198
|
+
|
|
199
|
+
def _on_edit(real_row: int, col_idx: int, old_val: Any, new_val: Any) -> None:
|
|
200
|
+
col_name = (
|
|
201
|
+
grid.columns[col_idx].title
|
|
202
|
+
if hasattr(grid, "columns") and col_idx < len(grid.columns)
|
|
203
|
+
else str(col_idx)
|
|
204
|
+
)
|
|
205
|
+
self.record(
|
|
206
|
+
"update_cell",
|
|
207
|
+
entity,
|
|
208
|
+
record_id=real_row,
|
|
209
|
+
field=col_name,
|
|
210
|
+
old_value=old_val,
|
|
211
|
+
new_value=new_val,
|
|
212
|
+
)
|
|
213
|
+
if callable(prev):
|
|
214
|
+
prev(real_row, col_idx, old_val, new_val)
|
|
215
|
+
|
|
216
|
+
setattr(grid, "on_cell_edit", _on_edit)
|
|
217
|
+
return self
|
|
218
|
+
|
|
219
|
+
def export_csv(self, path: str | Path) -> Path:
|
|
220
|
+
out = Path(path)
|
|
221
|
+
out.parent.mkdir(parents=True, exist_ok=True)
|
|
222
|
+
entries = list(reversed(self.query(limit=100_000)))
|
|
223
|
+
with out.open("w", encoding="utf-8-sig", newline="") as fh:
|
|
224
|
+
w = csv.writer(fh)
|
|
225
|
+
w.writerow(["timestamp", "user", "action", "entity", "record_id", "field", "old_value", "new_value"])
|
|
226
|
+
for e in entries:
|
|
227
|
+
w.writerow([e.timestamp, e.user, e.action, e.entity, e.record_id or "", e.field or "", e.old_value, e.new_value])
|
|
228
|
+
return out
|
|
229
|
+
|
|
230
|
+
def close(self) -> None:
|
|
231
|
+
if self._owns_conn and self._conn is not None:
|
|
232
|
+
self._conn.close()
|
|
233
|
+
self._owns_conn = False
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
# ---------------------------------------------------------------------------
|
|
237
|
+
# Undo / Redo Command History (`ui.history`)
|
|
238
|
+
# ---------------------------------------------------------------------------
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
@dataclass(frozen=True)
|
|
242
|
+
class _Command:
|
|
243
|
+
label: str
|
|
244
|
+
undo: Callable[[], None]
|
|
245
|
+
redo: Callable[[], None]
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
class CommandHistory:
|
|
249
|
+
"""Reversible Undo/Redo stack (`Ctrl+Z` / `Ctrl+Y`) for forms, grids, and editors."""
|
|
250
|
+
|
|
251
|
+
def __init__(self, *, max_depth: int = 100) -> None:
|
|
252
|
+
self.max_depth = max(1, int(max_depth))
|
|
253
|
+
self._undo_stack: list[_Command] = []
|
|
254
|
+
self._redo_stack: list[_Command] = []
|
|
255
|
+
self._listeners: list[Callable[[CommandHistory], None]] = []
|
|
256
|
+
|
|
257
|
+
@property
|
|
258
|
+
def can_undo(self) -> bool:
|
|
259
|
+
return len(self._undo_stack) > 0
|
|
260
|
+
|
|
261
|
+
@property
|
|
262
|
+
def can_redo(self) -> bool:
|
|
263
|
+
return len(self._redo_stack) > 0
|
|
264
|
+
|
|
265
|
+
@property
|
|
266
|
+
def undo_label(self) -> str | None:
|
|
267
|
+
return self._undo_stack[-1].label if self._undo_stack else None
|
|
268
|
+
|
|
269
|
+
@property
|
|
270
|
+
def redo_label(self) -> str | None:
|
|
271
|
+
return self._redo_stack[-1].label if self._redo_stack else None
|
|
272
|
+
|
|
273
|
+
def subscribe(self, fn: Callable[[CommandHistory], None]) -> CommandHistory:
|
|
274
|
+
if fn not in self._listeners:
|
|
275
|
+
self._listeners.append(fn)
|
|
276
|
+
return self
|
|
277
|
+
|
|
278
|
+
def _notify(self) -> None:
|
|
279
|
+
for fn in list(self._listeners):
|
|
280
|
+
fn(self)
|
|
281
|
+
|
|
282
|
+
def push(self, label: str, *, undo: Callable[[], None], redo: Callable[[], None]) -> None:
|
|
283
|
+
"""Record an already-applied action onto the undo stack."""
|
|
284
|
+
self._undo_stack.append(_Command(label=label, undo=undo, redo=redo))
|
|
285
|
+
if len(self._undo_stack) > self.max_depth:
|
|
286
|
+
self._undo_stack.pop(0)
|
|
287
|
+
self._redo_stack.clear()
|
|
288
|
+
self._notify()
|
|
289
|
+
|
|
290
|
+
def execute(self, label: str, *, do: Callable[[], None], undo: Callable[[], None]) -> None:
|
|
291
|
+
"""Execute `do()` immediately and record it for undo/redo."""
|
|
292
|
+
do()
|
|
293
|
+
self.push(label, undo=undo, redo=do)
|
|
294
|
+
|
|
295
|
+
def undo(self) -> bool:
|
|
296
|
+
if not self._undo_stack:
|
|
297
|
+
return False
|
|
298
|
+
cmd = self._undo_stack.pop()
|
|
299
|
+
cmd.undo()
|
|
300
|
+
self._redo_stack.append(cmd)
|
|
301
|
+
self._notify()
|
|
302
|
+
return True
|
|
303
|
+
|
|
304
|
+
def redo(self) -> bool:
|
|
305
|
+
if not self._redo_stack:
|
|
306
|
+
return False
|
|
307
|
+
cmd = self._redo_stack.pop()
|
|
308
|
+
cmd.redo()
|
|
309
|
+
self._undo_stack.append(cmd)
|
|
310
|
+
self._notify()
|
|
311
|
+
return True
|
|
312
|
+
|
|
313
|
+
def clear(self) -> None:
|
|
314
|
+
self._undo_stack.clear()
|
|
315
|
+
self._redo_stack.clear()
|
|
316
|
+
self._notify()
|
|
317
|
+
|
|
318
|
+
def bind_shortcuts(self, window: Any) -> CommandHistory:
|
|
319
|
+
"""Bind `Ctrl+Z` (Undo) and `Ctrl+Y` / `Ctrl+Shift+Z` (Redo) on a `Window`."""
|
|
320
|
+
window.shortcut("Ctrl+Z", self.undo)
|
|
321
|
+
window.shortcut("Ctrl+Y", self.redo)
|
|
322
|
+
window.shortcut("Ctrl+Shift+Z", self.redo)
|
|
323
|
+
return self
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""Context stack enabling NiceGUI-style `with` container syntax."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from contextlib import contextmanager
|
|
6
|
+
from typing import TYPE_CHECKING, Iterator
|
|
7
|
+
|
|
8
|
+
if TYPE_CHECKING:
|
|
9
|
+
from .widget import Widget
|
|
10
|
+
from .window import Window
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class _ContextStack:
|
|
14
|
+
def __init__(self) -> None:
|
|
15
|
+
self._containers: list[Widget] = []
|
|
16
|
+
self._windows: list[Window] = []
|
|
17
|
+
self.last_window: Window | None = None
|
|
18
|
+
self._detached_depth: int = 0
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
def is_detached(self) -> bool:
|
|
22
|
+
return self._detached_depth > 0
|
|
23
|
+
|
|
24
|
+
@contextmanager
|
|
25
|
+
def detached(self) -> Iterator[None]:
|
|
26
|
+
self._detached_depth += 1
|
|
27
|
+
try:
|
|
28
|
+
yield
|
|
29
|
+
finally:
|
|
30
|
+
self._detached_depth -= 1
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def current_container(self) -> Widget | None:
|
|
34
|
+
return self._containers[-1] if self._containers else None
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def current_window(self) -> Window | None:
|
|
38
|
+
return self._windows[-1] if self._windows else None
|
|
39
|
+
|
|
40
|
+
def push_container(self, widget: Widget) -> None:
|
|
41
|
+
self._containers.append(widget)
|
|
42
|
+
|
|
43
|
+
def pop_container(self) -> Widget | None:
|
|
44
|
+
return self._containers.pop() if self._containers else None
|
|
45
|
+
|
|
46
|
+
def push_window(self, window: Window) -> None:
|
|
47
|
+
self._windows.append(window)
|
|
48
|
+
self.last_window = window
|
|
49
|
+
|
|
50
|
+
def pop_window(self) -> Window | None:
|
|
51
|
+
return self._windows.pop() if self._windows else None
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
context = _ContextStack()
|