|
| 1 | +"""Tkinter GUI launcher for codedocent.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import subprocess # nosec B404 |
| 6 | +import sys |
| 7 | + |
| 8 | +from codedocent.ollama_utils import check_ollama, fetch_ollama_models |
| 9 | + |
| 10 | +try: |
| 11 | + import tkinter as tk |
| 12 | + from tkinter import ttk, filedialog |
| 13 | +except ImportError: |
| 14 | + tk = None # type: ignore[assignment] |
| 15 | + |
| 16 | +_HAS_TK = tk is not None |
| 17 | + |
| 18 | +# Re-export for testability |
| 19 | +_check_ollama = check_ollama |
| 20 | +_fetch_ollama_models = fetch_ollama_models |
| 21 | + |
| 22 | + |
| 23 | +def _build_gui() -> None: |
| 24 | + """Build and run the tkinter GUI window.""" |
| 25 | + root = tk.Tk() |
| 26 | + root.title("codedocent") |
| 27 | + root.resizable(False, False) |
| 28 | + |
| 29 | + frame = ttk.Frame(root, padding=16) |
| 30 | + frame.grid(row=0, column=0, sticky="nsew") |
| 31 | + |
| 32 | + # --- Folder picker --- |
| 33 | + ttk.Label(frame, text="Folder to analyze:").grid( |
| 34 | + row=0, column=0, sticky="w", pady=(0, 4), |
| 35 | + ) |
| 36 | + |
| 37 | + folder_var = tk.StringVar() |
| 38 | + folder_entry = ttk.Entry(frame, textvariable=folder_var, width=40) |
| 39 | + folder_entry.grid(row=1, column=0, sticky="ew", padx=(0, 6)) |
| 40 | + |
| 41 | + def _browse() -> None: |
| 42 | + path = filedialog.askdirectory() |
| 43 | + if path: |
| 44 | + folder_var.set(path) |
| 45 | + |
| 46 | + ttk.Button(frame, text="Browse...", command=_browse).grid( |
| 47 | + row=1, column=1, sticky="w", |
| 48 | + ) |
| 49 | + |
| 50 | + # --- Model dropdown --- |
| 51 | + ttk.Label(frame, text="Model:").grid( |
| 52 | + row=2, column=0, sticky="w", pady=(12, 4), |
| 53 | + ) |
| 54 | + |
| 55 | + ollama_ok = _check_ollama() |
| 56 | + models = _fetch_ollama_models() if ollama_ok else [] |
| 57 | + |
| 58 | + model_values = models if models else ["No AI"] |
| 59 | + model_var = tk.StringVar(value=model_values[0]) |
| 60 | + model_combo = ttk.Combobox( |
| 61 | + frame, textvariable=model_var, values=model_values, |
| 62 | + state="readonly", width=37, |
| 63 | + ) |
| 64 | + model_combo.grid(row=3, column=0, columnspan=2, sticky="ew") |
| 65 | + |
| 66 | + # --- Mode selector --- |
| 67 | + ttk.Label(frame, text="Mode:").grid( |
| 68 | + row=4, column=0, sticky="w", pady=(12, 4), |
| 69 | + ) |
| 70 | + |
| 71 | + mode_var = tk.StringVar(value="interactive") |
| 72 | + modes_frame = ttk.Frame(frame) |
| 73 | + modes_frame.grid(row=5, column=0, columnspan=2, sticky="w") |
| 74 | + |
| 75 | + ttk.Radiobutton( |
| 76 | + modes_frame, text="Interactive", variable=mode_var, |
| 77 | + value="interactive", |
| 78 | + ).pack(anchor="w") |
| 79 | + ttk.Radiobutton( |
| 80 | + modes_frame, text="Full export", variable=mode_var, |
| 81 | + value="full", |
| 82 | + ).pack(anchor="w") |
| 83 | + ttk.Radiobutton( |
| 84 | + modes_frame, text="Text tree", variable=mode_var, |
| 85 | + value="text", |
| 86 | + ).pack(anchor="w") |
| 87 | + |
| 88 | + # --- Go button --- |
| 89 | + def _go() -> None: |
| 90 | + folder = folder_var.get().strip() |
| 91 | + if not folder: |
| 92 | + return |
| 93 | + |
| 94 | + cmd = [sys.executable, "-m", "codedocent", folder] |
| 95 | + |
| 96 | + selected_model = model_var.get() |
| 97 | + if selected_model == "No AI": |
| 98 | + cmd.append("--no-ai") |
| 99 | + else: |
| 100 | + cmd.extend(["--model", selected_model]) |
| 101 | + |
| 102 | + mode = mode_var.get() |
| 103 | + if mode == "full": |
| 104 | + cmd.append("--full") |
| 105 | + elif mode == "text": |
| 106 | + cmd.append("--text") |
| 107 | + |
| 108 | + subprocess.Popen(cmd) # pylint: disable=consider-using-with # nosec B603 # noqa: E501 |
| 109 | + root.destroy() |
| 110 | + |
| 111 | + ttk.Button(frame, text="Go", command=_go).grid( |
| 112 | + row=6, column=0, columnspan=2, pady=(16, 0), |
| 113 | + ) |
| 114 | + |
| 115 | + root.mainloop() |
| 116 | + |
| 117 | + |
| 118 | +def main() -> None: |
| 119 | + """Entry point for codedocent-gui.""" |
| 120 | + if not _HAS_TK: |
| 121 | + print("tkinter is not installed.") |
| 122 | + print("Install it with: sudo apt install python3-tk") |
| 123 | + raise SystemExit(1) |
| 124 | + _build_gui() |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + main() |
0 commit comments