-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_utils.py
More file actions
260 lines (219 loc) · 8.49 KB
/
Copy pathpdf_utils.py
File metadata and controls
260 lines (219 loc) · 8.49 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
import sqlite3
import difflib
from pathlib import Path
from datetime import datetime
# UI helpers (Dateidialoge & Hinweise)
from tkinter import filedialog, messagebox
# PDF
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
from reportlab.lib import colors
# Daten-Helfer
from db_utils import load_snapshot_content, get_previous_snapshot_content
PROJECT_DIR = Path(__file__).resolve().parent
DB_PATH = str(PROJECT_DIR / "conum.db")
# --------------------------------------------------------------------
# Einzel-PDF-Export
# --------------------------------------------------------------------
def export_snapshot_to_pdf(snapshot_id, file_display, timestamp, fingerprint):
"""Erzeugt einen kompakten, gut lesbaren Einzelreport."""
filename = filedialog.asksaveasfilename(
defaultextension=".pdf",
initialfile=f"snapshot_{snapshot_id}.pdf",
filetypes=[("PDF files", "*.pdf"), ("All files", "*.*")]
)
if not filename:
return
c = canvas.Canvas(filename, pagesize=A4)
width, height = A4
# Header
top_y = height - 36
c.setFont("Helvetica-Bold", 18)
c.drawString(40, top_y, "CoNuM Snapshot Report")
# Meta
c.setFont("Helvetica", 11)
meta_y = top_y - 22
for line in (
f"Snapshot ID: {snapshot_id}",
f"Datei: {file_display}",
f"Zeit: {timestamp}",
f"Fingerprint: {fingerprint}",
):
c.drawString(40, meta_y, line)
meta_y -= 15
# Inhalt
code, dna, source_info = load_snapshot_content(snapshot_id)
prev_id, prev_dna, prev_code, prev_fp = get_previous_snapshot_content(snapshot_id)
content_y = meta_y - 10
c.setFont("Helvetica-Oblique", 9)
c.setFillColor(colors.grey)
c.drawString(40, content_y, source_info or "")
c.setFillColor(colors.black)
content_y -= 8
# Body (Diff wenn möglich, sonst Volltext)
y = content_y - 6
c.setFont("Courier", 9)
line_h = 10
if prev_id and prev_fp != fingerprint and prev_code and code:
diff_lines = difflib.unified_diff(
prev_code.splitlines(), code.splitlines(),
fromfile=f"OLD (Snapshot {prev_id})",
tofile=f"NEW (Snapshot {snapshot_id})",
n=3
)
for line in diff_lines:
if y < 50:
c.showPage()
y = height - 40
c.setFont("Courier", 9)
if line.startswith("+") and not line.startswith("+++"):
c.setFillColor(colors.green)
elif line.startswith("-") and not line.startswith("---"):
c.setFillColor(colors.red)
else:
c.setFillColor(colors.black)
c.drawString(40, y, line[:110])
y -= line_h
else:
c.setFillColor(colors.black)
for line in (code or dna or "").splitlines():
if y < 50:
c.showPage()
y = height - 40
c.setFont("Courier", 9)
c.drawString(40, y, line[:110])
y -= line_h
c.save()
messagebox.showinfo("PDF Export", f"Report gespeichert:\n{filename}")
# --------------------------------------------------------------------
# Batch-PDF-Export (Datei-DB)
# --------------------------------------------------------------------
def export_batch_to_pdf(selected_ids):
"""Erzeugt einen kompakten Sammelreport. `selected_ids` = Liste von Snapshot-IDs."""
if not selected_ids:
messagebox.showwarning("Batch-Export", "Bitte mindestens einen Snapshot anhaken!")
return
filename = filedialog.asksaveasfilename(
defaultextension=".pdf",
initialfile="batch_report.pdf",
filetypes=[("PDF files", "*.pdf"), ("All files", "*.*")]
)
if not filename:
return
c = canvas.Canvas(filename, pagesize=A4)
width, height = A4
# Titelseite
c.setFont("Helvetica-Bold", 18)
c.drawString(40, height - 60, "CoNuM Batch Snapshot Report")
c.setFont("Helvetica", 12)
c.drawString(40, height - 90, f"Anzahl Snapshots: {len(selected_ids)}")
c.drawString(40, height - 110, f"Erstellt am: {datetime.now().isoformat(sep=' ', timespec='seconds')}")
y = height - 140
# Nacheinander rendern
for sid in selected_ids:
conn = sqlite3.connect(DB_PATH)
cur = conn.cursor()
cur.execute("SELECT file, timestamp, fingerprint FROM snapshots WHERE id=?", (sid,))
row = cur.fetchone()
conn.close()
if not row:
continue
file_display, timestamp, fingerprint = row
if y < 140:
c.showPage()
y = height - 40
# Header des Eintrags
c.setFont("Helvetica-Bold", 13)
c.drawString(40, y, f"Snapshot ID: {sid}")
y -= 15
c.setFont("Helvetica", 10)
for line in (
f"Datei: {file_display}",
f"Zeit: {timestamp}",
f"Fingerprint: {fingerprint}",
):
c.drawString(40, y, line)
y -= 12
# Source-Info
code, dna, source_info = load_snapshot_content(sid)
prev_id, prev_dna, prev_code, prev_fp = get_previous_snapshot_content(sid)
c.setFont("Helvetica-Oblique", 9)
c.setFillColor(colors.grey)
c.drawString(40, y, source_info or "")
c.setFillColor(colors.black)
y -= 10
# Body
c.setFont("Courier", 9)
line_h = 10
if prev_id and prev_fp != fingerprint and prev_code and code:
diff_lines = difflib.unified_diff(
prev_code.splitlines(), code.splitlines(),
fromfile=f"OLD (Snapshot {prev_id})",
tofile=f"NEW (Snapshot {sid})",
n=3
)
for line in diff_lines:
if y < 50:
c.showPage()
y = height - 40
c.setFont("Courier", 9)
if line.startswith("+") and not line.startswith("+++"):
c.setFillColor(colors.green)
elif line.startswith("-") and not line.startswith("---"):
c.setFillColor(colors.red)
else:
c.setFillColor(colors.black)
c.drawString(40, y, line[:110])
y -= line_h
else:
c.setFillColor(colors.black)
for line in (code or dna or "").splitlines():
if y < 50:
c.showPage()
y = height - 40
c.setFont("Courier", 9)
c.drawString(40, y, line[:110])
y -= line_h
# Trennlinie
y -= 6
c.setStrokeColor(colors.lightgrey)
c.line(40, y, width - 40, y)
y -= 12
c.save()
messagebox.showinfo("Batch-PDF Export", f"Sammelreport gespeichert:\n{filename}")
# --------------------------------------------------------------------
# Batch-PDF-Export (Ordner-DB)
# --------------------------------------------------------------------
def export_batch_folder_to_pdf(snapshot_ids, db_path):
"""
Exportiert mehrere Snapshots aus einer Ordner-DB als PDF.
Zeigt jeweils die Diffs (mit Kontext) zwischen den Snapshots.
"""
if not snapshot_ids:
messagebox.showwarning("Batch-Export", "Bitte mindestens einen Snapshot anhaken!")
return
filename = filedialog.asksaveasfilename(
defaultextension=".pdf",
initialfile="folder_batch_report.pdf",
filetypes=[("PDF files", "*.pdf"), ("All files", "*.*")]
)
if not filename:
return
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Preformatted
from reportlab.lib.styles import getSampleStyleSheet
styles = getSampleStyleSheet()
story = []
for sid in snapshot_ids:
code, dna, source_info = load_snapshot_content(sid, db_path)
prev_id, prev_dna, prev_code, prev_fp = get_previous_snapshot_content(sid, db_path)
story.append(Paragraph(f"<b>Snapshot {sid}</b> – {source_info}", styles["Heading3"]))
if prev_code and code:
diff = list(difflib.ndiff(prev_code.splitlines(), code.splitlines()))
diff_text = "\n".join(diff[:200]) # max. 200 Zeilen
story.append(Preformatted(diff_text, styles["Code"]))
else:
story.append(Preformatted(code or "⚠️ Kein Inhalt verfügbar", styles["Code"]))
story.append(Spacer(1, 12))
doc = SimpleDocTemplate(filename, pagesize=A4)
doc.build(story)
messagebox.showinfo("Batch-PDF Export", f"Ordner-Sammelreport gespeichert:\n{filename}")