|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import tempfile |
| 7 | +import textwrap |
| 8 | +import time |
| 9 | +import tkinter as tk |
| 10 | +from pathlib import Path |
| 11 | +from tkinter import scrolledtext |
| 12 | +from typing import Dict, List |
| 13 | + |
| 14 | +from PIL import ImageGrab |
| 15 | + |
| 16 | +PROJECT_ROOT = Path(__file__).resolve().parents[1] |
| 17 | +if str(PROJECT_ROOT) not in sys.path: |
| 18 | + sys.path.insert(0, str(PROJECT_ROOT)) |
| 19 | + |
| 20 | +from MethodenAnalyser3 import ( |
| 21 | + OUTPUT_FONT, |
| 22 | + OUTPUT_HEIGHT, |
| 23 | + OUTPUT_WIDTH, |
| 24 | + analyze_project, |
| 25 | + analyze_source, |
| 26 | + generate_project_report, |
| 27 | + generate_report, |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +README_SCREENSHOT = PROJECT_ROOT / "README" / "screenshots" / "main.png" |
| 32 | +SCREENSHOT_DIR = PROJECT_ROOT / "releases" / "windowsstore" / "screenshots" |
| 33 | +MANIFEST_PATH = SCREENSHOT_DIR / "manifest.json" |
| 34 | +WINDOW_GEOMETRY = "1440x920" |
| 35 | + |
| 36 | + |
| 37 | +def _write(path: Path, content: str) -> None: |
| 38 | + path.write_text(textwrap.dedent(content).strip() + "\n", encoding="utf-8") |
| 39 | + |
| 40 | + |
| 41 | +def _build_file_report() -> str: |
| 42 | + result = analyze_source( |
| 43 | + textwrap.dedent( |
| 44 | + """ |
| 45 | + import os |
| 46 | + import json |
| 47 | +
|
| 48 | + def load_settings(): |
| 49 | + return {"theme": "light"} |
| 50 | +
|
| 51 | + def helper_debug(): |
| 52 | + return os.getcwd() |
| 53 | +
|
| 54 | + print(load_settings()) |
| 55 | + """ |
| 56 | + ).strip() |
| 57 | + + "\n", |
| 58 | + source_name="settings_panel.py", |
| 59 | + ) |
| 60 | + return "Datei-Modus\n\n" + generate_report(result) |
| 61 | + |
| 62 | + |
| 63 | +def _build_duplicate_report() -> str: |
| 64 | + result = analyze_source( |
| 65 | + textwrap.dedent( |
| 66 | + """ |
| 67 | + def normalize_name(value): |
| 68 | + cleaned = value.strip().lower() |
| 69 | + return cleaned.replace("-", "_") |
| 70 | +
|
| 71 | + def normalize_slug(value): |
| 72 | + cleaned = value.strip().lower() |
| 73 | + return cleaned.replace("-", "_") |
| 74 | +
|
| 75 | + print(normalize_name("Demo")) |
| 76 | + """ |
| 77 | + ).strip() |
| 78 | + + "\n", |
| 79 | + source_name="duplicate_candidates.py", |
| 80 | + ) |
| 81 | + return "Duplikat-Suche\n\n" + generate_report(result) |
| 82 | + |
| 83 | + |
| 84 | +def _build_project_report() -> str: |
| 85 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 86 | + root = Path(tmpdir) |
| 87 | + pkg = root / "demo_project" |
| 88 | + pkg.mkdir() |
| 89 | + _write( |
| 90 | + pkg / "main.py", |
| 91 | + """ |
| 92 | + import math |
| 93 | + from helper import area |
| 94 | +
|
| 95 | + print(area(2), math.pi) |
| 96 | + """, |
| 97 | + ) |
| 98 | + _write( |
| 99 | + pkg / "helper.py", |
| 100 | + """ |
| 101 | + import os |
| 102 | +
|
| 103 | + def area(radius): |
| 104 | + return radius * radius * 3.14159 |
| 105 | +
|
| 106 | + def unused_helper(): |
| 107 | + return os.getcwd() |
| 108 | + """, |
| 109 | + ) |
| 110 | + _write( |
| 111 | + pkg / "broken.py", |
| 112 | + """ |
| 113 | + def broken(: |
| 114 | + return 1 |
| 115 | + """, |
| 116 | + ) |
| 117 | + result = analyze_project(str(pkg)) |
| 118 | + return "Projekt-Modus\n\n" + generate_project_report(result) |
| 119 | + |
| 120 | + |
| 121 | +def build_scenarios() -> List[Dict[str, str]]: |
| 122 | + return [ |
| 123 | + { |
| 124 | + "filename": "file-analysis.png", |
| 125 | + "title": "Einzeldateien schnell prüfen", |
| 126 | + "subtitle": "AST-basierte Analyse mit ungenutzten Imports und Definitionen", |
| 127 | + "report": _build_file_report(), |
| 128 | + }, |
| 129 | + { |
| 130 | + "filename": "project-analysis.png", |
| 131 | + "title": "Projektüberblick mit Sammelreport", |
| 132 | + "subtitle": "Mehrere Python-Dateien inklusive Fehlerdateien gemeinsam auswerten", |
| 133 | + "report": _build_project_report(), |
| 134 | + }, |
| 135 | + { |
| 136 | + "filename": "duplicate-detection.png", |
| 137 | + "title": "Ähnliche Code-Blöcke sichtbar machen", |
| 138 | + "subtitle": "Duplikat-Hinweise und Refactoring-Kandidaten im selben Report", |
| 139 | + "report": _build_duplicate_report(), |
| 140 | + }, |
| 141 | + ] |
| 142 | + |
| 143 | + |
| 144 | +def _create_window(report: str, title: str, subtitle: str) -> tk.Tk: |
| 145 | + root = tk.Tk() |
| 146 | + root.title("MethodenAnalyser - Windows Store Screenshots") |
| 147 | + root.geometry(WINDOW_GEOMETRY) |
| 148 | + root.configure(bg="#eef2f7") |
| 149 | + |
| 150 | + outer = tk.Frame(root, bg="#eef2f7") |
| 151 | + outer.pack(fill=tk.BOTH, expand=True, padx=24, pady=24) |
| 152 | + |
| 153 | + hero = tk.Frame(outer, bg="#102235") |
| 154 | + hero.pack(fill=tk.X, pady=(0, 18)) |
| 155 | + tk.Label( |
| 156 | + hero, |
| 157 | + text="MethodenAnalyser", |
| 158 | + font=("Segoe UI", 22, "bold"), |
| 159 | + fg="white", |
| 160 | + bg="#102235", |
| 161 | + anchor="w", |
| 162 | + ).pack(fill=tk.X, padx=22, pady=(20, 4)) |
| 163 | + tk.Label( |
| 164 | + hero, |
| 165 | + text=title, |
| 166 | + font=("Segoe UI", 15, "bold"), |
| 167 | + fg="#d8e8ff", |
| 168 | + bg="#102235", |
| 169 | + anchor="w", |
| 170 | + ).pack(fill=tk.X, padx=22) |
| 171 | + tk.Label( |
| 172 | + hero, |
| 173 | + text=subtitle, |
| 174 | + font=("Segoe UI", 11), |
| 175 | + fg="#c0d1e5", |
| 176 | + bg="#102235", |
| 177 | + anchor="w", |
| 178 | + ).pack(fill=tk.X, padx=22, pady=(6, 18)) |
| 179 | + |
| 180 | + button_frame = tk.Frame(outer, bg="#eef2f7") |
| 181 | + button_frame.pack(fill=tk.X, pady=(0, 10)) |
| 182 | + buttons = [ |
| 183 | + ("📂 Datei analysieren", "#2e7d32"), |
| 184 | + ("ℹ️ Info", "#1565c0"), |
| 185 | + ("🔧 Auto-Fix Imports", "#ef6c00"), |
| 186 | + ("Projekt analysieren", "#7b1fa2"), |
| 187 | + ] |
| 188 | + for text, color in buttons: |
| 189 | + tk.Button( |
| 190 | + button_frame, |
| 191 | + text=text, |
| 192 | + bg=color, |
| 193 | + fg="white", |
| 194 | + relief=tk.FLAT, |
| 195 | + font=("Segoe UI", 10, "bold"), |
| 196 | + padx=18, |
| 197 | + pady=10, |
| 198 | + ).pack(side=tk.LEFT, padx=(0, 8)) |
| 199 | + |
| 200 | + output = scrolledtext.ScrolledText( |
| 201 | + outer, |
| 202 | + width=OUTPUT_WIDTH, |
| 203 | + height=OUTPUT_HEIGHT, |
| 204 | + font=OUTPUT_FONT, |
| 205 | + wrap=tk.WORD, |
| 206 | + bg="#f7f9fc", |
| 207 | + fg="#1f2933", |
| 208 | + bd=0, |
| 209 | + padx=16, |
| 210 | + pady=16, |
| 211 | + ) |
| 212 | + output.pack(fill=tk.BOTH, expand=True) |
| 213 | + output.insert("1.0", report) |
| 214 | + output.configure(state=tk.DISABLED) |
| 215 | + return root |
| 216 | + |
| 217 | + |
| 218 | +def _capture(root: tk.Tk, destination: Path) -> None: |
| 219 | + root.update_idletasks() |
| 220 | + root.update() |
| 221 | + root.lift() |
| 222 | + root.attributes("-topmost", True) |
| 223 | + root.update() |
| 224 | + time.sleep(0.6) |
| 225 | + left = root.winfo_rootx() |
| 226 | + top = root.winfo_rooty() |
| 227 | + right = left + root.winfo_width() |
| 228 | + bottom = top + root.winfo_height() |
| 229 | + image = ImageGrab.grab(bbox=(left, top, right, bottom)) |
| 230 | + image.save(destination, "PNG") |
| 231 | + root.destroy() |
| 232 | + |
| 233 | + |
| 234 | +def generate_store_screenshots() -> Dict[str, object]: |
| 235 | + SCREENSHOT_DIR.mkdir(parents=True, exist_ok=True) |
| 236 | + scenarios = build_scenarios() |
| 237 | + assets = [] |
| 238 | + |
| 239 | + for scenario in scenarios: |
| 240 | + target = SCREENSHOT_DIR / scenario["filename"] |
| 241 | + root = _create_window( |
| 242 | + report=scenario["report"], |
| 243 | + title=scenario["title"], |
| 244 | + subtitle=scenario["subtitle"], |
| 245 | + ) |
| 246 | + _capture(root, target) |
| 247 | + assets.append( |
| 248 | + { |
| 249 | + "path": target.name, |
| 250 | + "title": scenario["title"], |
| 251 | + "subtitle": scenario["subtitle"], |
| 252 | + } |
| 253 | + ) |
| 254 | + |
| 255 | + if README_SCREENSHOT.exists(): |
| 256 | + (SCREENSHOT_DIR / "main.png").write_bytes(README_SCREENSHOT.read_bytes()) |
| 257 | + assets.insert( |
| 258 | + 0, |
| 259 | + { |
| 260 | + "path": "main.png", |
| 261 | + "title": "Hauptfenster", |
| 262 | + "subtitle": "Die klassische Desktop-Oberfläche für lokale Python-Dateien", |
| 263 | + }, |
| 264 | + ) |
| 265 | + |
| 266 | + manifest = { |
| 267 | + "generated_at": time.strftime("%Y-%m-%d %H:%M:%S"), |
| 268 | + "count": len(assets), |
| 269 | + "screenshots": assets, |
| 270 | + } |
| 271 | + MANIFEST_PATH.write_text( |
| 272 | + json.dumps(manifest, ensure_ascii=False, indent=2) + "\n", |
| 273 | + encoding="utf-8", |
| 274 | + ) |
| 275 | + return manifest |
| 276 | + |
| 277 | + |
| 278 | +def main() -> int: |
| 279 | + manifest = generate_store_screenshots() |
| 280 | + print(f"{manifest['count']} Screenshots aktualisiert: {SCREENSHOT_DIR}") |
| 281 | + return 0 |
| 282 | + |
| 283 | + |
| 284 | +if __name__ == "__main__": |
| 285 | + raise SystemExit(main()) |
0 commit comments