-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd_file_copier_gui.py
executable file
·185 lines (149 loc) · 6.86 KB
/
md_file_copier_gui.py
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
import sys
import os
from pathlib import Path
import shutil
from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QLabel, QLineEdit, QPushButton,
QProgressBar, QTextEdit, QFileDialog)
from PyQt6.QtCore import Qt, QThread, pyqtSignal
def generate_unique_filename(target_path: Path, original_name: str, parent_folder: str = None) -> str:
"""Generate a unique filename by prepending parent folder name if needed."""
base_name = original_name
if parent_folder:
name_without_ext = Path(original_name).stem
base_name = f"{parent_folder}_{name_without_ext}.md"
if not (target_path / base_name).exists():
return base_name
counter = 1
while True:
name_without_ext = Path(base_name).stem
new_name = f"{name_without_ext}_{counter}.md"
if not (target_path / new_name).exists():
return new_name
counter += 1
class CopyWorker(QThread):
progress = pyqtSignal(int)
status = pyqtSignal(str)
finished = pyqtSignal()
error = pyqtSignal(str)
def __init__(self, source_dir, target_dir, folder_name):
super().__init__()
self.source_dir = source_dir
self.target_dir = target_dir
self.folder_name = folder_name
def run(self):
try:
source_path = Path(self.source_dir)
target_path = Path(self.target_dir) / self.folder_name
# Create target directory
target_path.mkdir(parents=True, exist_ok=True)
# Count total files for progress
total_files = sum(1 for root, _, files in os.walk(source_path)
for file in files if file.endswith(('.md', '.mdx')))
if total_files == 0:
self.error.emit("No markdown files found in source directory.")
return
processed_files = 0
# Copy files
for root, _, files in os.walk(source_path):
for file in files:
if file.endswith(('.md', '.mdx')):
source_file = Path(root) / file
parent_folder = (Path(root).relative_to(source_path).parts[0]
if len(Path(root).relative_to(source_path).parts) > 0
else None)
target_filename = file[:-4] + '.md' if file.endswith('.mdx') else file
final_filename = generate_unique_filename(target_path, target_filename, parent_folder)
target_file = target_path / final_filename
# Copy the file
shutil.copy2(source_file, target_file)
processed_files += 1
# Update progress and status
progress = int((processed_files / total_files) * 100)
self.progress.emit(progress)
self.status.emit(f"Copied: {source_file.name} -> {target_file.name}")
self.status.emit("File copying completed successfully!")
self.finished.emit()
except Exception as e:
self.error.emit(str(e))
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Markdown File Copier")
self.setMinimumWidth(600)
# Create main widget and layout
main_widget = QWidget()
self.setCentralWidget(main_widget)
layout = QVBoxLayout(main_widget)
# Source directory selection
source_layout = QHBoxLayout()
self.source_input = QLineEdit()
source_button = QPushButton("Select Source")
source_button.clicked.connect(lambda: self.select_directory(self.source_input))
source_layout.addWidget(QLabel("Source Directory:"))
source_layout.addWidget(self.source_input)
source_layout.addWidget(source_button)
layout.addLayout(source_layout)
# Target directory selection
target_layout = QHBoxLayout()
self.target_input = QLineEdit()
target_button = QPushButton("Select Target")
target_button.clicked.connect(lambda: self.select_directory(self.target_input))
target_layout.addWidget(QLabel("Target Directory:"))
target_layout.addWidget(self.target_input)
target_layout.addWidget(target_button)
layout.addLayout(target_layout)
# Folder name input
folder_layout = QHBoxLayout()
self.folder_input = QLineEdit()
folder_layout.addWidget(QLabel("Folder Name:"))
folder_layout.addWidget(self.folder_input)
layout.addLayout(folder_layout)
# Progress bar
self.progress_bar = QProgressBar()
layout.addWidget(self.progress_bar)
# Status text area
self.status_text = QTextEdit()
self.status_text.setReadOnly(True)
self.status_text.setMinimumHeight(200)
layout.addWidget(self.status_text)
# Copy button
self.copy_button = QPushButton("Copy Files")
self.copy_button.clicked.connect(self.start_copy)
layout.addWidget(self.copy_button)
self.worker = None
def select_directory(self, input_field):
directory = QFileDialog.getExistingDirectory(self, "Select Directory")
if directory:
input_field.setText(directory)
def start_copy(self):
source_dir = self.source_input.text()
target_dir = self.target_input.text()
folder_name = self.folder_input.text()
if not all([source_dir, target_dir, folder_name]):
self.status_text.append("Error: Please fill in all fields")
return
if not os.path.isdir(source_dir):
self.status_text.append("Error: Source directory does not exist")
return
self.copy_button.setEnabled(False)
self.progress_bar.setValue(0)
self.status_text.clear()
self.worker = CopyWorker(source_dir, target_dir, folder_name)
self.worker.progress.connect(self.progress_bar.setValue)
self.worker.status.connect(self.status_text.append)
self.worker.error.connect(self.handle_error)
self.worker.finished.connect(self.handle_finished)
self.worker.start()
def handle_error(self, error_msg):
self.status_text.append(f"Error: {error_msg}")
self.copy_button.setEnabled(True)
def handle_finished(self):
self.copy_button.setEnabled(True)
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
if __name__ == '__main__':
main()