-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmash_tkinter.py
730 lines (600 loc) · 29.6 KB
/
smash_tkinter.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
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
import tkinter as tk
from tkinter import ttk, messagebox
import os
import webbrowser # 웹브라우저 열기 위한 모듈 추가
import re
from dotenv import load_dotenv
import pandas as pd
import yaml
from excel_to_notion import ExcelToNotionImporter
import time
import sys
import subprocess
import configparser
def resource_path(relative_path):
"""Get absolute path to resource, works for dev and for PyInstaller"""
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
class SmashTeamGenerator(tk.Tk):
def __init__(self):
super().__init__()
self.title("스매시 조 편성기 v2.0")
self.geometry("1200x800")
# 사용자 문서 폴더에 저장
# Windows: `C:\Users\사용자이름`
# macOS: `/ Users/사용자이름`
documents_path = os.path.join(os.path.expanduser("~"), "Documents")
self.config_dir = os.path.join(documents_path, "SmashTeamGenerator")
os.makedirs(self.config_dir, exist_ok=True)
# 설정 파일 경로
self.config_file_path = os.path.join(self.config_dir, "config.ini")
# Notion 토큰 초기화
self.NOTION_TOKEN = self.load_notion_token()
# Notion 관련 정보 설정
self.parent_page_id = "1a39f0ea074e80e085a5dbe8bfa5404f"
self.template_page_id = "1a79f0ea074e807b9b18c586b0890893"
# YAML 파일 경로 설정
self.yaml_file_path = os.path.join(self.config_dir, "groups.yaml")
# 기본 YAML 파일이 없으면 기본 파일 복사
self.ensure_yaml_file_exists()
# 엑셀 파일 저장 경로 설정
self.excel_file_path = os.path.join(
self.config_dir, "team_composition_result.xlsx")
self.notion_page_url = None # 생성된 Notion 페이지 URL 저장
# GUI 스타일 초기화
self.init_style()
# 메인 위젯 생성
self.create_widgets()
def init_style(self):
"""스타일 설정"""
self.style = ttk.Style()
self.style.configure('TFrame', background='#f5f5f5')
self.style.configure('TButton', font=('맑은 고딕', 10), padding=5)
self.style.configure('Header.TLabel', font=('맑은 고딕', 11, 'bold'))
def create_widgets(self):
"""메인 위젯 구성"""
main_frame = ttk.Frame(self)
main_frame.pack(pady=20, padx=20, fill=tk.BOTH, expand=True)
# 1. 입력 영역
self.create_input_section(main_frame)
# 2. 결과 영역
self.create_result_section(main_frame)
# 3. 상태 표시바
self.status_bar = ttk.Label(self, relief=tk.SUNKEN, anchor=tk.W)
self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)
def create_input_section(self, parent):
"""텍스트 입력 영역"""
input_frame = ttk.LabelFrame(parent, text="■ 입력 데이터 붙여넣기")
input_frame.pack(fill=tk.BOTH, expand=True, pady=10)
# 接龙 내용 입력
self.jielong_text = self.create_text_area(
input_frame, "接龙 내용 (接龙 전체 붙여넣기):", 0, "jielong")
# 제외 명단 입력
self.lesson_text = self.create_text_area(
input_frame, "제외 명단 (코치/레슨생):", 1, "lesson")
input_frame.grid_columnconfigure(0, weight=1) # 접룡 영역 가중치 1
input_frame.grid_columnconfigure(1, weight=1) # 제외명단 영역 가중치 1
input_frame.grid_rowconfigure(0, weight=1)
def create_text_area(self, parent, label_text, column, example):
"""재사용 가능한 텍스트 영역 생성"""
frame = ttk.Frame(parent)
frame.grid(row=0, column=column, sticky="nsew")
lbl = ttk.Label(frame, text=label_text, style='Header.TLabel')
lbl.pack(anchor=tk.W)
text_widget = tk.Text(frame, wrap=tk.WORD, height=10,
font=('맑은 고딕', 10))
# tmp 예시추가
if example == "jielong":
try:
file_path = resource_path('examples/jielong.txt')
with open(file_path, 'r', encoding='utf-8') as f:
example_text = f.read()
text_widget.insert(tk.END, example_text)
except FileNotFoundError:
print("jielong 예시 파일이 존재하지 않습니다")
elif example == "lesson":
try:
file_path = resource_path('examples/lesson.txt')
with open(file_path, 'r', encoding='utf-8') as f:
example_text = f.read()
text_widget.insert(tk.END, example_text)
except FileNotFoundError:
print("lesson 예시 파일이 존재하지 않습니다")
scroll = ttk.Scrollbar(frame, command=text_widget.yview)
text_widget.configure(yscrollcommand=scroll.set)
text_widget.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scroll.pack(side=tk.RIGHT, fill=tk.Y)
return text_widget
def create_result_section(self, parent):
"""결과 표시 영역"""
result_frame = ttk.LabelFrame(parent, text="■ 팀 구성 결과")
result_frame.pack(fill=tk.BOTH, expand=True)
# 상단: 트리뷰 영역 (좌우)
tree_frame = ttk.Frame(result_frame)
tree_frame.pack(fill=tk.BOTH, expand=True)
# 왼쪽: 페어링 프레임
pairing_frame = ttk.LabelFrame(tree_frame, text=" 페어링 결과")
pairing_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=5)
# 페어링 테이블
self.pairing_tree = ttk.Treeview(
pairing_frame,
columns=('index', 't1', 't2'),
show='headings',
)
self.pairing_tree.heading('index', text='No.', anchor='center')
self.pairing_tree.column(
'index', width=50, anchor='center', stretch=tk.NO)
self.pairing_tree.heading('t1', text='팀 1', anchor='center')
self.pairing_tree.column('t1', width=120, anchor='center')
self.pairing_tree.heading('t2', text='팀 2', anchor='center')
self.pairing_tree.column('t2', width=120, anchor='center')
# 스크롤바 추가
scrollbar = ttk.Scrollbar(pairing_frame, orient=tk.VERTICAL,
command=self.pairing_tree.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.pairing_tree.configure(yscroll=scrollbar.set)
self.pairing_tree.pack(fill='both', expand=True, padx=0, pady=0)
# 오른쪽: 조 편성 프레임
group_frame = ttk.LabelFrame(tree_frame, text=" 조 편성 결과 (A,B,C 조)")
group_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True, padx=5)
# 조 편성 테이블
self.group_tree = ttk.Treeview(
group_frame,
columns=('index', 'A', 'B', 'C'),
show='headings',
)
self.group_tree.heading('index', text='No.', anchor='center')
self.group_tree.column(
'index', width=50, anchor='center', stretch=tk.NO)
self.group_tree.heading('A', text='A조', anchor='center')
self.group_tree.column('A', width=100, anchor='center')
self.group_tree.heading('B', text='B조', anchor='center')
self.group_tree.column('B', width=100, anchor='center')
self.group_tree.heading('C', text='C조', anchor='center')
self.group_tree.column('C', width=100, anchor='center')
# 스크롤바 추가
scrollbar = ttk.Scrollbar(group_frame, orient=tk.VERTICAL,
command=self.group_tree.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.group_tree.configure(yscroll=scrollbar.set)
self.group_tree.pack(fill='both', expand=True, padx=0, pady=0)
# 하단: 제어 버튼 영역
btn_frame = ttk.Frame(result_frame)
btn_frame.pack(pady=10)
ttk.Button(btn_frame, text="팀 생성",
command=self.generate_teams).pack(side=tk.LEFT, padx=5)
ttk.Button(btn_frame, text="추출하기",
command=self.export_to_excel).pack(side=tk.LEFT, padx=5)
# Notion 업로드 버튼 추가
ttk.Button(btn_frame, text="Notion 업로드",
command=self.upload_to_notion).pack(side=tk.LEFT, padx=5)
ttk.Button(btn_frame, text="초기화",
command=self.clear_inputs).pack(side=tk.LEFT, padx=5)
# 새 버튼 추가: 그룹 설정 편집과 결과 폴더 열기
ttk.Button(btn_frame, text="그룹 설정 편집",
command=self.open_yaml_editor).pack(side=tk.LEFT, padx=5)
ttk.Button(btn_frame, text="결과 폴더 열기",
command=self.open_result_folder).pack(side=tk.LEFT, padx=5)
# Notion 링크 표시 레이블 추가
self.notion_link_frame = ttk.Frame(result_frame)
self.notion_link_frame.pack(pady=5)
self.notion_link_label = ttk.Label(
self.notion_link_frame,
text="",
foreground="blue",
cursor="hand2"
)
self.notion_link_label.pack(side=tk.LEFT)
# Notion 상태 표시 레이블
self.notion_status_label = ttk.Label(self.notion_link_frame, text="")
self.notion_status_label.pack(side=tk.LEFT, padx=10)
def generate_teams(self):
"""팀 생성 핵심 로직"""
try:
# 1. 입력 데이터 처리
# 1.1 입력 데이터 가져오기
jielong_content = self.jielong_text.get("1.0", tk.END).strip()
lesson_content = self.lesson_text.get("1.0", tk.END).strip()
# 接龙 입력값 검증
if not jielong_content:
raise ValueError("接龙 내용을 입력해주세요")
# 1.2 接龙 내용 추출
jielong_start = jielong_content.find('1.')
if jielong_start == -1:
raise ValueError("접룡 내용에 '1.' 표시가 없습니다")
# 接龙 명단 추출
jielong_names_list = re.findall(
r'(?:게스트\s\([가-힣]+\)|[가-힣]+[A-Z]?)', jielong_content[jielong_start:])
print(f"접룡 인원: {len(jielong_names_list)} 명")
# print(jielong_names_list)
# 1.3 레슨 내용 추출
lesson_names_list = []
if lesson_content:
lesson_start = lesson_content.find('1.')
if lesson_start == -1:
raise ValueError("레슨 인원에 '1.' 표시가 없습니다")
# 레슨 명단 추출
lesson_names_list = re.findall(
r'[가-힣]+[A-Z]?', lesson_content[lesson_start:])
lesson_names_list = [
name for name in lesson_names_list if "코치" not in name]
print(f"레슨 인원: {len(lesson_names_list)} 명")
# print(lesson_names_list)
# 2. 그룹 데이터 로드 (사용자 문서 폴더에서)
try:
with open(self.yaml_file_path, 'r', encoding='utf-8') as f:
groups_data = yaml.safe_load(f)
except FileNotFoundError:
# 파일이 없는 경우 기본 파일에서 로드
yaml_path = resource_path('groups.yaml')
with open(yaml_path, 'r', encoding='utf-8') as f:
groups_data = yaml.safe_load(f)
# 파일이 사라진 경우 다시 생성
self.ensure_yaml_file_exists()
# 3. 1st 페어링 편성
# 3.1 접룡 인원 정렬
ordered_list = []
for group in groups_data['groups'].values():
ordered_list.extend(group)
jielong_ordered_list = sorted(
jielong_names_list, key=lambda x: ordered_list.index(x))
# 3.2 접룡 인원에서 레슨 인원 제외
pairing_list = [
name for name in jielong_ordered_list if name not in lesson_names_list]
# 홀수 짝수 확인, 홀수면 单打 인원 선택/입력 창 호출
solo_player = None
if len(pairing_list) % 2 != 0:
# 单打 인원 선택/입력 창
selected_player = self.show_solo_selection_dialog(pairing_list)
if not selected_player:
raise ValueError("팀 생성에 필요한 单打 인원을 선택해야 합니다")
solo_player = selected_player
pairing_list.remove(solo_player) # 单打 인원 제외
# 3.3 정렬된 인원 페어링 편성
half = len(pairing_list) // 2
first_column = pairing_list[:half]
second_column = pairing_list[half:][::-1]
# 3.4 트리뷰 초기화 및 데이터 삽입
self.pairing_tree.delete(*self.pairing_tree.get_children())
# 페어링 인원 추가
for i, (t1, t2) in enumerate(zip(first_column, second_column), start=1):
self.pairing_tree.insert('', tk.END, values=(i, t1, t2))
# 单打 인원 추가 (빨간색)
if solo_player:
last_index = len(self.pairing_tree.get_children()) + 1
self.pairing_tree.insert(
'', tk.END,
values=(last_index, solo_player, ""),
tags=('solo',)
)
self.pairing_tree.tag_configure('solo', background='#FFE4E1')
# 레슨 인원 추가 (노란색으로 변경)
if lesson_names_list:
lesson_pairs = []
for i in range(0, len(lesson_names_list), 2):
pair = (
lesson_names_list[i],
lesson_names_list[i+1] if i+1 < len(lesson_names_list) else '')
lesson_pairs.append(pair)
last_index = len(self.pairing_tree.get_children())
for i, (t1, t2) in enumerate(lesson_pairs, start=1):
self.pairing_tree.insert(
'', tk.END,
values=(i+last_index, t1, t2),
tags=('lesson',)
)
self.pairing_tree.tag_configure(
'lesson', background='#FFEB9C')
# 4. 2nd 조 편성
# 4.1 접룡 인원 그룹별로 분리
filtered_groups = {
'A': [name for name in groups_data['groups']['A'] if name in jielong_names_list],
'B': [name for name in groups_data['groups']['B'] if name in jielong_names_list],
'C': [name for name in groups_data['groups']['C'] if name in jielong_names_list]
}
# 4.2 트리뷰에 표시
self.group_tree.delete(*self.group_tree.get_children())
max_length = max(len(filtered_groups['A']),
len(filtered_groups['B']),
len(filtered_groups['C']))
for i in range(max_length):
#fmt: off
a = filtered_groups['A'][i] if i < len(filtered_groups['A']) else ''
b = filtered_groups['B'][i] if i < len(filtered_groups['B']) else ''
c = filtered_groups['C'][i] if i < len(filtered_groups['C']) else ''
#fmt: on
self.group_tree.insert('', tk.END, values=(i+1, a, b, c))
self.status_bar.config(text="팀 생성 완료")
except Exception as e:
messagebox.showerror("오류", str(e))
def export_to_excel(self):
"""Excel 추출 기능 (시트 분리)"""
try:
group_data = [
self.group_tree.item(item)['values'][1:] # 인덱스 제거
for item in self.group_tree.get_children()
]
# 결과 데이터 추출
pair_data = []
self.lesson_indices = [] # 레슨 행 인덱스
self.solo_indices = [] # 단식 행 인덱스
for i, item in enumerate(self.pairing_tree.get_children()):
values = self.pairing_tree.item(item)['values'][1:] # 인덱스 제거
pair_data.append(values)
# 태그 확인하여 인덱스 저장
tags = self.pairing_tree.item(item, 'tags')
if 'lesson' in tags:
self.lesson_indices.append(i)
if 'solo' in tags:
self.solo_indices.append(i)
# Excel 파일 생성
with pd.ExcelWriter(self.excel_file_path, engine='xlsxwriter') as writer:
# 페어링 데이터프레임 생성
pairing_df = pd.DataFrame(pair_data, columns=['t1', 't2'])
# 페어링 시트에 데이터 쓰기
pairing_df.to_excel(
writer,
sheet_name='페어링',
index=False,
)
# 워크북과 워크시트 객체 가져오기
workbook = writer.book
pairing_sheet = writer.sheets['페어링']
# 포맷 생성
yellow_format = workbook.add_format(
{'bg_color': '#FFEB9C'}) # 노란색 (레슨)
red_format = workbook.add_format(
{'bg_color': '#FFE4E1'}) # 빨간색 (단식)
# 레슨 행에 노란색 배경 적용
for row_idx in self.lesson_indices:
pairing_sheet.write(
row_idx + 1, 0, pairing_df.iloc[row_idx, 0], yellow_format)
pairing_sheet.write(
row_idx + 1, 1, pairing_df.iloc[row_idx, 1], yellow_format)
# 단식 행에 빨간색 배경 적용
for row_idx in self.solo_indices:
pairing_sheet.write(
row_idx + 1, 0, pairing_df.iloc[row_idx, 0], red_format)
pairing_sheet.write(
row_idx + 1, 1, pairing_df.iloc[row_idx, 1], red_format)
group_df = pd.DataFrame(group_data, columns=['A', 'B', 'C'])
group_df.to_excel(
writer,
sheet_name='조편성',
index=False,
)
self.status_bar.config(text=f"엑셀 추출 완료: {self.excel_file_path}")
messagebox.showinfo("성공",
f"엑셀 파일이 성공적으로 저장되었습니다.\n\n"
f"저장 위치: {self.excel_file_path}\n\n"
f"'결과 폴더 열기' 버튼을 클릭하여 파일을 찾을 수 있습니다.")
except Exception as e:
messagebox.showerror("오류", str(e))
def clear_inputs(self):
"""입력 초기화"""
self.jielong_text.delete("1.0", tk.END)
self.lesson_text.delete("1.0", tk.END)
self.pairing_tree.delete(*self.pairing_tree.get_children())
self.group_tree.delete(*self.group_tree.get_children())
self.notion_link_label.config(text="")
self.notion_status_label.config(text="")
self.notion_link_label.unbind("<Button-1>")
self.notion_page_url = None
self.status_bar.config(text="입력 초기화 완료")
def show_solo_selection_dialog(self, players):
"""单打 인원 선택/입력 창"""
dialog = tk.Toplevel(self)
dialog.title("单打 인원 선택창")
dialog.geometry("300x120")
dialog.transient(self) # 메인 윈도우의 자식 창으로 설정
dialog.grab_set() # 모든 이벤트를 이 창으로 포커스
dialog.resizable(False, False) # 크기 변경 비활성화
# 창 위치를 메인 윈도우 중앙에
self.update_idletasks()
main_x = self.winfo_x()
main_y = self.winfo_y()
main_width = self.winfo_width()
main_height = self.winfo_height()
dialog.geometry(
f"+{main_x + (main_width//2 - 150)}+{main_y + (main_height//2 - 60)}")
selected_player = tk.StringVar()
ttk.Label(dialog, text="인원이 홀수입니다.\n单打 인원을 선택/입력 하세요 :",
anchor='center').pack(pady=5)
cb = ttk.Combobox(dialog, textvariable=selected_player, values=players)
cb.pack(pady=5)
def on_confirm():
dialog.selected_player = selected_player.get().strip()
dialog.destroy()
ttk.Button(dialog, text="확인", command=on_confirm).pack(pady=5)
dialog.wait_window() # 다이얼로그 닫힐 때까지 대기
return getattr(dialog, 'selected_player', None)
def upload_to_notion(self):
"""Notion에 업로드하는 기능"""
# 엑셀 파일이 존재하는지 확인
if not os.path.exists(self.excel_file_path):
messagebox.showerror("오류", "엑셀 파일이 존재하지 않습니다. 먼저 '추출하기'를 실행해주세요.")
return
try:
# 기존 바인딩 제거 및 레이블 초기화
self.notion_link_label.config(text="")
self.notion_link_label.unbind("<Button-1>")
# 업로드 시작 표시
self.notion_status_label.config(
text="Notion 업로드 중...", foreground="orange")
self.update_idletasks() # UI 업데이트
# 실행 시간 측정 시작
t1 = time.time()
# lesson_indices 확인 - 없으면 빈 리스트로 초기화
lesson_indices = getattr(self, 'lesson_indices', [])
# ExcelToNotionImporter 인스턴스 생성 및 실행
cloner = ExcelToNotionImporter(
self.NOTION_TOKEN,
self.parent_page_id,
self.template_page_id,
self.excel_file_path,
lesson_indices,
)
# 템플릿 페이지 복제
self.notion_page_url = cloner.duplicate_template_page()
# 블록 업데이트
cloner.update_block()
# 실행 시간 측정 종료
t2 = time.time()
elapsed_time = t2 - t1
# 링크 레이블 새로 설정
self.notion_link_label.config(
text=f"Notion 페이지 바로가기",
font=('맑은 고딕', 10, 'underline'),
foreground="blue",
cursor="hand2"
)
# 바인딩 다시 설정
self.notion_link_label.bind("<Button-1>", self.open_notion_page)
# 상태 및 소요 시간 표시
self.notion_status_label.config(
text=f"업로드 완료 (소요 시간: {elapsed_time:.1f}초)",
foreground="green"
)
# 상태바 업데이트
self.status_bar.config(
text=f"Notion 업로드 완료: {self.notion_page_url}")
# 성공 메시지 표시
messagebox.showinfo("성공", "Notion에 팀 구성 데이터가 성공적으로 업로드되었습니다.")
except Exception as e:
error_msg = str(e)
self.notion_link_label.config(text="")
self.notion_status_label.config(text="업로드 실패", foreground="red")
self.status_bar.config(text=f"Notion 업로드 오류: {error_msg}")
# EOF 오류 발생 시 VPN 관련 메시지 표시
if "EOF occurred" in error_msg or "violation" in error_msg.lower():
messagebox.showerror("Notion 업로드 오류",
f"네트워크 연결 오류가 발생했습니다: {error_msg}\n\n"
"VPN이 켜져 있다면 끄고 다시 시도해보세요.")
else:
messagebox.showerror("Notion 업로드 오류", error_msg)
def open_notion_page(self, event):
"""Notion 페이지 링크 클릭 시 웹 브라우저에서 열기"""
if self.notion_page_url:
webbrowser.open(self.notion_page_url)
def ensure_yaml_file_exists(self):
"""기본 YAML 파일이 없으면 기본 파일을 복사"""
if not os.path.exists(self.yaml_file_path):
# 기본 YAML 파일(패키지에 포함된) 경로
default_yaml_path = resource_path('groups.yaml')
try:
# 기본 파일 내용 읽기
with open(default_yaml_path, 'r', encoding='utf-8') as src_file:
yaml_content = src_file.read()
# 사용자 디렉토리에 파일 복사
with open(self.yaml_file_path, 'w', encoding='utf-8') as dst_file:
dst_file.write(yaml_content)
except Exception as e:
print(f"YAML 파일 복사 중 오류: {e}")
def open_yaml_editor(self):
"""YAML 파일을 시스템 기본 에디터로 열기"""
try:
if os.name == 'nt': # Windows
os.startfile(self.yaml_file_path)
elif os.name == 'posix': # macOS, Linux
if sys.platform == 'darwin': # macOS
subprocess.call(('open', self.yaml_file_path))
else: # Linux
subprocess.call(('xdg-open', self.yaml_file_path))
messagebox.showinfo("안내",
"그룹 설정 파일이 열렸습니다.\n\n"
"파일을 편집한 후 저장하고, 프로그램에서 '팀 생성' 버튼을 다시 클릭하면 "
"그룹 파일 변경 사항이 적용됩니다.")
except Exception as e:
messagebox.showerror("오류", f"파일을 열 수 없습니다: {e}")
def open_result_folder(self):
"""결과 파일이 저장된 폴더 열기"""
try:
if os.name == 'nt': # Windows
os.startfile(self.config_dir)
elif os.name == 'posix': # macOS, Linux
if sys.platform == 'darwin': # macOS
subprocess.call(('open', self.config_dir))
else: # Linux
subprocess.call(('xdg-open', self.config_dir))
except Exception as e:
messagebox.showerror("오류", f"폴더를 열 수 없습니다: {e}")
def load_notion_token(self):
"""설정 파일에서 Notion 토큰을 로드하거나 사용자에게 요청"""
config = configparser.ConfigParser()
# 기본 토큰 (개발용, 실제 배포 시 빈 문자열로 변경)
default_token = ""
# 설정 파일이 있는지 확인
if os.path.exists(self.config_file_path):
try:
config.read(self.config_file_path)
if 'Notion' in config and 'token' in config['Notion']:
return config['Notion']['token']
except Exception as e:
print(f"설정 파일 로드 중 오류: {e}")
# 설정 파일이 없거나 토큰이 없으면 사용자에게 요청
token = self.ask_for_notion_token(default_token)
# 새 토큰 저장
if token:
if not 'Notion' in config:
config['Notion'] = {}
config['Notion']['token'] = token
try:
with open(self.config_file_path, 'w') as f:
config.write(f)
except Exception as e:
print(f"설정 파일 저장 중 오류: {e}")
return token
def ask_for_notion_token(self, default_token=""):
"""사용자에게 Notion 토큰을 요청하는 다이얼로그"""
dialog = tk.Toplevel(self)
dialog.title("Notion 토큰 설정")
dialog.geometry("500x200")
dialog.transient(self)
dialog.grab_set()
dialog.resizable(False, False)
# 창 중앙 위치
self.update_idletasks()
main_x = self.winfo_x()
main_y = self.winfo_y()
main_width = self.winfo_width()
main_height = self.winfo_height()
dialog.geometry(
f"+{main_x + (main_width//2 - 250)}+{main_y + (main_height//2 - 100)}")
tk.Label(dialog, text="Notion 통합을 위한 API 토큰을 입력해주세요.",
font=('맑은 고딕', 10, 'bold')).pack(pady=(15, 5))
tk.Label(dialog, text="※ 토큰은 컴퓨터에 안전하게 저장됩니다.",
font=('맑은 고딕', 9)).pack(pady=(0, 10))
token_var = tk.StringVar(value=default_token)
token_entry = tk.Entry(
dialog, textvariable=token_var, width=50, show="*")
token_entry.pack(pady=5, padx=20)
# 토큰 표시/숨김 체크박스
show_var = tk.BooleanVar(value=False)
def toggle_show():
if show_var.get():
token_entry.config(show="")
else:
token_entry.config(show="*")
tk.Checkbutton(dialog, text="토큰 표시", variable=show_var,
command=toggle_show).pack(pady=5)
def on_confirm():
dialog.token = token_var.get().strip()
dialog.destroy()
def on_cancel():
dialog.token = default_token
dialog.destroy()
btn_frame = tk.Frame(dialog)
btn_frame.pack(pady=15)
tk.Button(btn_frame, text="확인", command=on_confirm,
width=10).pack(side=tk.LEFT, padx=10)
tk.Button(btn_frame, text="취소", command=on_cancel,
width=10).pack(side=tk.LEFT, padx=10)
dialog.wait_window()
return getattr(dialog, 'token', default_token)
if __name__ == "__main__":
app = SmashTeamGenerator()
app.mainloop()