-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodebase_consolidator_gui.py
More file actions
870 lines (749 loc) · 29.5 KB
/
codebase_consolidator_gui.py
File metadata and controls
870 lines (749 loc) · 29.5 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
#!/usr/bin/env python3
"""
Codebase Consolidator - GUI (Tkinter + ttkbootstrap)
This GUI wraps the existing CodebaseConsolidator logic and presents a simple
interface to:
- select codebase directory
- choose target number of output files
- choose output directory (optional)
- set a custom folder name (optional)
- run consolidation in a background thread
- stream logs to the UI
"""
from __future__ import annotations
import os
import queue
import subprocess
import sys
import threading
from datetime import datetime
from pathlib import Path
from typing import List, Optional, Tuple
import ttkbootstrap as tb
from ttkbootstrap.dialogs import Messagebox
from ttkbootstrap.constants import (
LEFT,
RIGHT,
BOTTOM,
X,
Y,
BOTH,
YES,
NORMAL,
DISABLED,
SUCCESS,
DANGER,
INFO,
SECONDARY,
END,
)
from ttkbootstrap.scrolled import ScrolledText
from tkinter import filedialog
from tkinter import ttk
from codebase_consolidator import CodebaseConsolidator
class EnhancedCodebaseConsolidator(CodebaseConsolidator):
"""Extended consolidator with GUI formatting options."""
def __init__(
self,
root_paths: List[str],
target_files: int = 50,
syntax_theme: str = "github",
line_numbers: bool = True,
use_xml: bool = False,
include_tree: bool = True,
max_part_size: int = 512000,
):
super().__init__(
root_paths,
target_files,
use_xml=use_xml,
include_tree=include_tree,
max_part_size=max_part_size,
)
self.syntax_theme = syntax_theme
self.line_numbers = line_numbers
def _write_part_header(self, f, part_num: int, total_parts: int, file_count: int, all_files: List[Path] = None):
"""Override to add GUI-specific metadata to the header"""
super()._write_part_header(f, part_num, total_parts, file_count, all_files)
f.write(f"**Syntax Theme:** {self.syntax_theme} \n")
f.write(
f"**Line Numbers:** {'Enabled' if self.line_numbers else 'Disabled'} \n"
)
f.write(f"**Format:** {'XML Tags' if self.use_xml else 'Markdown Code Blocks'} \n\n")
def _format_code_block(self, content: str, language: str, file_path: Path) -> str:
"""Format code block with optional line numbers and syntax theme info."""
# For RAG optimization: skip line numbers if using XML
effective_line_numbers = self.line_numbers and not self.use_xml
# Add syntax theme as a comment if it's not the default (only for markdown)
theme_comment = ""
if self.syntax_theme != "github" and not self.use_xml:
theme_comment = f"<!-- Syntax theme: {self.syntax_theme} -->\n"
# Add line numbers if enabled
if effective_line_numbers:
lines = content.split("\n")
# Handle case where split produces an empty string at the end if content ends with newline
if lines and not lines[-1]:
lines.pop()
numbered_lines = []
for i, line in enumerate(lines, 1):
numbered_lines.append(f"{i:4d} | {line}")
content = "\n".join(numbered_lines)
# Re-add newline if it was there (to match original behavior mostly)
content += "\n"
rel_path = self._get_rel_path_with_root(file_path).as_posix()
if self.use_xml:
return f'<file path="{rel_path}" language="{language}">\n{content}</file>'
return f"{theme_comment}```{language}\n# File: {rel_path}\n{content}```"
def _create_index(self, output_path: Path, files: List[Path], file_buckets: List[List[Path]], actual_files: int):
"""Override to add formatting options to the index file"""
index_file = output_path / "README.md"
with open(index_file, "w", encoding="utf-8") as f:
f.write("# 📚 Consolidated Codebase\n\n")
f.write("**Source Directories:** \n")
for p in self.root_paths:
f.write(f"- `{p.absolute()}` \n")
f.write(
f"\n**Generated:** {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} \n"
)
f.write(f"**Total Files Processed:** {len(files)} \n")
f.write(f"**Target Output Files:** {self.target_files} \n")
f.write(f"**Actual Output Files:** {actual_files} \n")
f.write(f"**Syntax Theme:** {self.syntax_theme} \n")
f.write(
f"**Line Numbers:** {'Enabled' if self.line_numbers else 'Disabled'} \n"
)
f.write(f"**XML Format:** {'Yes' if self.use_xml else 'No'} \n")
f.write(f"**File Tree:** {'Included' if self.include_tree else 'Excluded'} \n\n")
f.write("## 🎨 Formatting Options\n\n")
f.write(f"- **Syntax Highlighting Theme:** `{self.syntax_theme}`\n")
f.write(
f"- **Line Numbers:** {'✅ Enabled' if self.line_numbers else '❌ Disabled'}\n"
)
f.write(f"- **XML Tags:** {'✅ Enabled' if self.use_xml else '❌ Disabled'}\n")
f.write(
f"- **File Tree:** {'✅ Included' if self.include_tree else '❌ Excluded'}\n\n"
)
f.write("## 📋 File Structure\n\n")
f.write("| Part | Files | Description |\n")
f.write("|------|-------|-------------|\n")
for i, bucket in enumerate(file_buckets):
file_list = ", ".join(
[f"`{self._get_rel_path_with_root(f)}`" for f in bucket[:3]]
)
if len(bucket) > 3:
file_list += f" and {len(bucket) - 3} more..."
f.write(
f"| [Part {i + 1}](./codebase_part_{i + 1:03d}.md) | {len(bucket)} | {file_list} |\n"
)
f.write("\n## 🚀 Usage\n\n")
f.write(
"This consolidated codebase was generated using the Codebase Consolidator GUI.\n"
)
f.write(
"Each markdown file contains multiple source files with enhanced formatting:\n\n"
)
f.write(
f"- **Syntax highlighting** optimized for `{self.syntax_theme}` theme\n"
)
f.write(
f"- **Line numbers** {'included' if self.line_numbers else 'excluded'} for easier reference\n"
)
f.write(
"- **Metadata** for each file including size and modification date\n\n"
)
return index_file
class QueueWriter:
"""A file-like object that writes to a Queue for GUI consumption."""
def __init__(self, q: queue.Queue[str]):
self.q = q
def write(self, s: str) -> int:
if s:
self.q.put(s)
return len(s)
def flush(self) -> None: # for compatibility
pass
class ConsolidatorGUI(tb.Window):
def __init__(self):
super().__init__(title="Codebase Consolidator", themename="solar")
self.geometry("1280x200")
self.minsize(1280, 900)
# state
self._worker: Optional[threading.Thread] = None
self._stop_flag = threading.Event()
self._log_queue: queue.Queue[str] = queue.Queue()
self._stdout_backup = sys.stdout
self._stderr_backup = sys.stderr
self._last_output_path: Optional[Path] = None
self._tree_item_paths: dict[str, Path] = {}
self._preview_worker: Optional[threading.Thread] = None
# build UI
self._build_header()
self._build_form()
self._build_formatting_options()
self._build_main_content()
self._build_actions()
# start polling log queue
self.after(100, self._drain_log_queue)
def _build_header(self) -> None:
header = tb.Frame(self, padding=(15, 10))
header.pack(fill=X)
title = tb.Label(
header,
text="📚 Codebase Consolidator",
font=("Segoe UI", 16, "bold"),
)
title.pack(side=LEFT)
# Get current theme name and available themes
current_theme = getattr(self.style.theme, "name", "flatly")
self.theme_var = tb.StringVar(value=current_theme)
# Get theme names using the correct API
try:
# Try the newer API first
theme_names = sorted(self.style.theme_names())
except AttributeError:
# Fallback to a default list of common ttkbootstrap themes
theme_names = sorted(
[
"cosmo",
"flatly",
"journal",
"litera",
"lumen",
"minty",
"pulse",
"sandstone",
"united",
"yeti",
"morph",
"simplex",
"cerculean",
"solar",
"superhero",
"darkly",
"cyborg",
"vapor",
]
)
theme_cb = tb.Combobox(
header,
textvariable=self.theme_var,
values=theme_names,
width=18,
state="readonly",
)
theme_cb.pack(side=RIGHT, padx=(10, 0))
theme_cb.bind("<<ComboboxSelected>>", self._on_theme_change)
theme_lbl = tb.Label(header, text="Theme:")
theme_lbl.pack(side=RIGHT)
def _build_form(self) -> None:
frm = tb.Labelframe(self, text="Settings", padding=10)
frm.pack(fill=X, padx=12, pady=(0, 8))
# Codebase directories list
row1 = tb.Frame(frm)
row1.pack(fill=X, pady=5)
tb.Label(row1, text="Source Directories:", width=20).pack(side=LEFT, anchor="n")
# Container for list and buttons
list_container = tb.Frame(row1)
list_container.pack(side=LEFT, fill=X, expand=YES)
self.path_tree = ttk.Treeview(list_container, show="tree", height=3)
self.path_tree.pack(side=LEFT, fill=X, expand=YES)
path_btns = tb.Frame(list_container)
path_btns.pack(side=LEFT, padx=(8, 0), anchor="n")
tb.Button(
path_btns, text="Add Folder", bootstyle=SECONDARY, command=self._add_codebase, width=12
).pack(pady=(0, 5))
tb.Button(
path_btns, text="Remove Selected", bootstyle=DANGER, command=self._remove_codebase, width=12
).pack()
# Output directory
row2 = tb.Frame(frm)
row2.pack(fill=X, pady=5)
tb.Label(row2, text="Output Directory:", width=20).pack(side=LEFT)
self.output_var = tb.StringVar()
self.output_entry = tb.Entry(row2, textvariable=self.output_var)
self.output_entry.pack(side=LEFT, fill=X, expand=YES)
tb.Button(
row2, text="Browse", bootstyle=SECONDARY, command=self._browse_output
).pack(side=LEFT, padx=(8, 0))
# Number of files
row3 = tb.Frame(frm)
row3.pack(fill=X, pady=5)
tb.Label(row3, text="Target # of files:", width=20).pack(side=LEFT)
self.num_files_var = tb.IntVar(value=50)
self.num_files_spin = tb.Spinbox(
row3,
from_=1,
to=5000,
textvariable=self.num_files_var,
width=10,
)
self.num_files_spin.pack(side=LEFT)
# Max Part Size (KB)
tb.Label(row3, text="Max Part Size (KB):", width=20).pack(side=LEFT, padx=(20, 0))
self.max_size_var = tb.IntVar(value=500)
self.max_size_spin = tb.Spinbox(
row3,
from_=10,
to=10000,
textvariable=self.max_size_var,
width=10,
)
self.max_size_spin.pack(side=LEFT)
# Custom folder name
row4 = tb.Frame(frm)
row4.pack(fill=X, pady=5)
tb.Label(row4, text="Custom Folder Name:", width=20).pack(side=LEFT)
self.folder_var = tb.StringVar()
self.folder_entry = tb.Entry(row4, textvariable=self.folder_var)
self.folder_entry.pack(side=LEFT, fill=X, expand=YES)
# Verbose
row5 = tb.Frame(frm)
row5.pack(fill=X, pady=5)
self.verbose_var = tb.BooleanVar(value=False)
tb.Checkbutton(
row5, text="Verbose", variable=self.verbose_var, bootstyle="round-toggle"
).pack(side=LEFT)
def _build_formatting_options(self) -> None:
frm = tb.Labelframe(self, text="Code Formatting Options", padding=10)
frm.pack(fill=X, padx=12, pady=(0, 8))
# Syntax highlighting theme
row1 = tb.Frame(frm)
row1.pack(fill=X, pady=5)
tb.Label(row1, text="Syntax Theme:", width=20).pack(side=LEFT)
self.syntax_theme_var = tb.StringVar(value="github")
syntax_themes = [
"github",
"monokai",
"solarized-light",
"solarized-dark",
"vs",
"vs-dark",
"atom-one-light",
"atom-one-dark",
"default",
"colorful",
"emacs",
"friendly",
"vim",
]
self.syntax_theme_cb = tb.Combobox(
row1,
textvariable=self.syntax_theme_var,
values=syntax_themes,
width=20,
state="readonly",
)
self.syntax_theme_cb.pack(side=LEFT)
# Line numbers toggle
row2 = tb.Frame(frm)
row2.pack(fill=X, pady=5)
self.line_numbers_var = tb.BooleanVar(value=True)
tb.Checkbutton(
row2,
text="Include Line Numbers",
variable=self.line_numbers_var,
bootstyle="round-toggle",
).pack(side=LEFT)
# XML Tags toggle
self.xml_var = tb.BooleanVar(value=False)
tb.Checkbutton(
row2,
text="Use XML Tags (Better for RAG)",
variable=self.xml_var,
bootstyle="round-toggle",
).pack(side=LEFT, padx=(20, 0))
# File Tree toggle
self.tree_var = tb.BooleanVar(value=True)
tb.Checkbutton(
row2,
text="Include File Tree",
variable=self.tree_var,
bootstyle="round-toggle",
).pack(side=LEFT, padx=(20, 0))
def _build_main_content(self) -> None:
# Create a notebook (tabbed interface) for preview and log
self.notebook = tb.Notebook(self)
self.notebook.pack(fill=BOTH, expand=YES, padx=12, pady=(0, 8))
# Preview tab
self.preview_frame = tb.Frame(self.notebook)
self.notebook.add(self.preview_frame, text="📋 File Preview")
# Preview controls
preview_controls = tb.Frame(self.preview_frame, padding=5)
preview_controls.pack(fill=X)
self.refresh_btn = tb.Button(
preview_controls,
text="🔄 Refresh Preview",
bootstyle=INFO,
command=self._refresh_preview,
)
self.refresh_btn.pack(side=LEFT)
self.file_count_label = tb.Label(preview_controls, text="No files loaded")
self.file_count_label.pack(side=LEFT, padx=(10, 0))
# File tree
tree_frame = tb.Frame(self.preview_frame)
tree_frame.pack(fill=BOTH, expand=YES, padx=5, pady=5)
# Create treeview with scrollbars
self.file_tree = ttk.Treeview(
tree_frame, columns=("size", "type"), show="tree headings"
)
self.file_tree.heading("#0", text="File Path")
self.file_tree.heading("size", text="Size")
self.file_tree.heading("type", text="Type")
self.file_tree.column("#0", width=400)
self.file_tree.column("size", width=80)
self.file_tree.column("type", width=100)
# Scrollbars for treeview
tree_scroll_y = tb.Scrollbar(
tree_frame, orient="vertical", command=self.file_tree.yview
)
tree_scroll_x = tb.Scrollbar(
tree_frame, orient="horizontal", command=self.file_tree.xview
)
self.file_tree.configure(
yscrollcommand=tree_scroll_y.set, xscrollcommand=tree_scroll_x.set
)
# Pack scrollbars first, then treeview
tree_scroll_y.pack(side=RIGHT, fill=Y)
tree_scroll_x.pack(side=BOTTOM, fill=X)
self.file_tree.pack(side=LEFT, fill=BOTH, expand=YES)
self.file_tree.bind("<Double-1>", self._on_tree_item_double_click)
# Log tab
self.log_frame = tb.Frame(self.notebook)
self.notebook.add(self.log_frame, text="📝 Processing Log")
self.log = ScrolledText(
self.log_frame, autohide=True, height=18, width=80, padding=2
)
self.log.pack(fill=BOTH, expand=YES, padx=5, pady=5)
self._append_log(
"Welcome! Configure your options and click 'Refresh Preview' to see which files will be processed.\n"
)
def _build_actions(self) -> None:
act = tb.Frame(self, padding=(12, 0))
act.pack(fill=X)
self.run_btn = tb.Button(
act, text="Run Consolidation", bootstyle=SUCCESS, command=self._on_run
)
self.run_btn.pack(side=LEFT)
self.stop_btn = tb.Button(
act, text="Stop", bootstyle=DANGER, state=DISABLED, command=self._on_stop
)
self.stop_btn.pack(side=LEFT, padx=(8, 0))
self.open_btn = tb.Button(
act,
text="Open Output Folder",
bootstyle=INFO,
state=DISABLED,
command=self._open_output,
)
self.open_btn.pack(side=LEFT, padx=(8, 0))
self.prog = tb.Progressbar(act, mode="indeterminate")
self.prog.pack(side=RIGHT, fill=X, expand=YES)
# UI callbacks
def _on_theme_change(self, _evt=None):
self.style.theme_use(self.theme_var.get())
def _add_codebase(self):
path = filedialog.askdirectory(title="Select Codebase Directory")
if path:
# Check if already in list
for item in self.path_tree.get_children():
if self.path_tree.item(item, "text") == path:
return
self.path_tree.insert("", "end", text=path)
def _remove_codebase(self):
selected = self.path_tree.selection()
if not selected:
return
for item in selected:
self.path_tree.delete(item)
def _browse_output(self):
path = filedialog.askdirectory(title="Select Output Base Directory")
if path:
self.output_var.set(path)
def _refresh_preview(self):
"""Refresh the file preview tree."""
paths = [self.path_tree.item(i, "text") for i in self.path_tree.get_children()]
if not paths:
Messagebox.show_error(
"Please add at least one codebase directory.", "Missing Input"
)
return
for path in paths:
p = Path(path).expanduser()
if not p.exists() or not p.is_dir():
Messagebox.show_error(
f"The directory '{path}' is invalid.", "Invalid Input"
)
return
if self._preview_worker and self._preview_worker.is_alive():
Messagebox.show_info(
"A preview refresh is already running. Please wait.",
"Refresh In Progress",
)
return
self.refresh_btn.configure(state=DISABLED)
self.file_count_label.configure(text="Scanning codebases...")
self._preview_worker = threading.Thread(
target=self._run_preview_worker,
args=(paths,),
daemon=True,
)
self._preview_worker.start()
def _run_preview_worker(self, paths: List[str]):
try:
consolidator = CodebaseConsolidator(paths, 1)
files = consolidator._collect_files()
file_data: List[Tuple[Path, int, str]] = []
for file_path in files:
size = consolidator._get_file_size(file_path)
language = consolidator._get_language_from_extension(file_path)
file_data.append((file_path, size, language))
except Exception as e:
self.after(0, lambda err=e: self._handle_preview_error(err))
return
self.after(
0,
lambda: self._populate_preview_tree(paths, file_data),
)
def _handle_preview_error(self, error: Exception):
Messagebox.show_error(f"Error scanning directory:\n{error}", "Preview Error")
self.refresh_btn.configure(state=NORMAL)
self.file_count_label.configure(text="Preview unavailable")
self._preview_worker = None
def _populate_preview_tree(
self, paths: List[str], file_data: List[Tuple[Path, int, str]]
):
# Clear existing tree
for item in self.file_tree.get_children():
self.file_tree.delete(item)
self._tree_item_paths.clear()
root_paths = [Path(p).resolve() for p in paths]
dir_nodes: dict[str, str] = {}
for file_path, file_size, file_type in file_data:
# Find which root this file belongs to
target_root = None
for root in root_paths:
try:
rel_path = file_path.relative_to(root)
target_root = root
break
except ValueError:
continue
if not target_root:
continue
# Prefix with root name
prefixed_rel_path = Path(target_root.name) / rel_path
parts = prefixed_rel_path.parts
# Create directory nodes if they don't exist
current_path = ""
parent = ""
for part in parts[:-1]:
current_path = str(Path(current_path) / part) if current_path else part
if current_path not in dir_nodes:
node_id = self.file_tree.insert(
parent,
"end",
text=f"📁 {part}",
values=("", "Directory"),
open=True,
)
dir_nodes[current_path] = node_id
# This path mapping might be slightly broken for virtual root names
# but double-click on file still works fine since it uses file_path
parent = dir_nodes[current_path]
# Format file size string
if file_size < 1024:
size_str = f"{file_size} B"
elif file_size < 1024 * 1024:
size_str = f"{file_size / 1024:.1f} KB"
else:
size_str = f"{file_size / (1024 * 1024):.1f} MB"
item_id = self.file_tree.insert(
parent,
"end",
text=f"📄 {parts[-1]}",
values=(size_str, file_type),
)
self._tree_item_paths[item_id] = file_path
file_count = len(file_data)
self.file_count_label.configure(
text=f"Found {file_count} file{'s' if file_count != 1 else ''} to process"
)
self.notebook.select(self.preview_frame)
self.refresh_btn.configure(state=NORMAL)
self._preview_worker = None
def _on_tree_item_double_click(self, event):
item_id = self.file_tree.identify_row(event.y)
if not item_id:
return
path = self._tree_item_paths.get(item_id)
if not path or not path.is_file():
return
try:
if sys.platform.startswith("win"):
os.startfile(str(path)) # type: ignore[attr-defined]
elif sys.platform == "darwin":
subprocess.run(["open", str(path)], check=False)
else:
subprocess.run(["xdg-open", str(path)], check=False)
except Exception as e:
Messagebox.show_error(f"Could not open file:\n{e}", "Open File Error")
def _on_run(self):
if self._worker and self._worker.is_alive():
return
paths = [self.path_tree.item(i, "text") for i in self.path_tree.get_children()]
if not paths:
Messagebox.show_error(
"Please add at least one codebase directory.", "Missing Input"
)
return
validated_paths = []
for path in paths:
p = Path(path).expanduser()
if not p.exists() or not p.is_dir():
Messagebox.show_error(
f"The directory '{path}' is invalid.", "Invalid Input"
)
return
validated_paths.append(str(p.resolve()))
try:
n_files = int(self.num_files_var.get())
if n_files <= 0:
raise ValueError
except Exception:
Messagebox.show_error(
"Target number of files must be a positive integer.", "Invalid Input"
)
return
output_dir = self.output_var.get().strip() or None
if output_dir:
out_path = Path(output_dir).expanduser()
try:
out_path.mkdir(parents=True, exist_ok=True)
except Exception as e:
Messagebox.show_error(
f"Cannot create output directory:\n{e}", "Output Error"
)
return
folder_name = self.folder_var.get().strip() or None
max_size = self.max_size_var.get() * 1024 # Convert KB to bytes
# Prepare run
self._stop_flag.clear()
self._last_output_path = None
self.open_btn.configure(state=DISABLED)
self.run_btn.configure(state=DISABLED)
self.stop_btn.configure(state=NORMAL)
self.prog.start(10)
# Get formatting options
syntax_theme = self.syntax_theme_var.get()
line_numbers = self.line_numbers_var.get()
use_xml = self.xml_var.get()
include_tree = self.tree_var.get()
# Switch to log tab
self.notebook.select(self.log_frame)
# Start worker thread
args = (
validated_paths,
n_files,
output_dir,
folder_name,
self.verbose_var.get(),
syntax_theme,
line_numbers,
use_xml,
include_tree,
max_size,
)
self._worker = threading.Thread(target=self._run_worker, args=args, daemon=True)
self._worker.start()
def _on_stop(self):
if self._worker and self._worker.is_alive():
self._stop_flag.set()
self._append_log(
"\n❌ Stop requested. Waiting for current step to finish...\n"
)
def _open_output(self):
if not self._last_output_path:
return
path = str(self._last_output_path)
try:
if sys.platform.startswith("win"):
os.startfile(path) # type: ignore[attr-defined]
elif sys.platform == "darwin":
subprocess.run(["open", path], check=False)
else:
subprocess.run(["xdg-open", path], check=False)
except Exception as e:
Messagebox.show_error(f"Could not open folder:\n{e}", "Open Error")
# Worker logic
def _run_worker(
self,
codebase_paths: List[str],
n_files: int,
output_dir: Optional[str],
folder_name: Optional[str],
verbose: bool,
syntax_theme: str,
line_numbers: bool,
use_xml: bool,
include_tree: bool,
max_size: int,
):
# Redirect stdout/stderr to GUI queue
qwriter = QueueWriter(self._log_queue)
sys.stdout = qwriter
sys.stderr = qwriter
try:
print("🚀 Codebase Consolidator GUI")
print("=" * 50)
print(f"📁 Source paths: {len(codebase_paths)}")
print(f"🎨 Using syntax theme: {syntax_theme}")
print(f"🔢 Line numbers: {'enabled' if line_numbers else 'disabled'}")
print(f"🏷️ XML Output: {'enabled' if use_xml else 'disabled'}")
print(f"🌳 File Tree: {'enabled' if include_tree else 'disabled'}")
print(f"📏 Max Part Size: {max_size / 1024:.1f} KB")
consolidator = EnhancedCodebaseConsolidator(
codebase_paths,
n_files,
syntax_theme,
line_numbers,
use_xml=use_xml,
include_tree=include_tree,
max_part_size=max_size,
)
result = consolidator.consolidate(output_dir, folder_name)
self._last_output_path = Path(result) if result else None
except KeyboardInterrupt:
print("\n❌ Operation cancelled by user")
except Exception as e:
print(f"❌ Error: {e}")
if verbose:
import traceback
traceback.print_exc()
finally:
# Restore stdout/stderr
sys.stdout = self._stdout_backup
sys.stderr = self._stderr_backup
self.after(0, self._on_worker_done)
def _on_worker_done(self):
self.prog.stop()
self.run_btn.configure(state=NORMAL)
self.stop_btn.configure(state=DISABLED)
if self._last_output_path:
self.open_btn.configure(state=NORMAL)
# Logging helpers
def _append_log(self, text: str) -> None:
self.log.insert(END, text)
self.log.see(END)
def _drain_log_queue(self):
try:
while True:
chunk = self._log_queue.get_nowait()
self._append_log(chunk)
except queue.Empty:
pass
self.after(100, self._drain_log_queue)
def main():
app = ConsolidatorGUI()
app.mainloop()
if __name__ == "__main__":
main()