Added — 도전과제 시스템 (153개 자동 평가) - core/achievements.py: 16개 카테고리, 5단계 등급, 시크릿 9개, 메타 도전과제 - ui/achievements_view.py: 4탭 다이얼로그 (전체/진행중/완료/시크릿) - 5분 throttle 자동 평가 + 시스템 알림 + Discord embed push - achievements 테이블 확장 (code/category/tier/is_secret/progress/target) - hire_date 자동 추적, 뷰 진입 카운터 8개 settings 키 Changed — 다크 테마 디자인 리뉴얼 - ui/dark_components.py: 재사용 가능한 다크 컴포넌트 (header/card/button/progress) - 통계/도움말/도전과제 다이얼로그 일관 다크 톤 - matplotlib 차트 다크 테마 적용 (figure/axes/grid/legend) - 등급별 카드 그라디언트 (브론즈/실버/골드/플래티넘/레전드) Fixed — 안정성·일관성 - 타임존 자정 경계 버그 (has_notification_today UTC vs localtime mismatch) - DB 연결 누수: _conn() 컨텍스트 매니저 도입, 40+ 메서드 변환 - DB 이중 부트스트랩 제거 (MainWindow(db=None) 옵션 인자) - crash_handler 다단계 폴백 (DB → 파일 → stderr) - updater PID race: 지수 backoff 재시도 (총 ~9초) - Discord URL 형식 검증 (snowflake regex) - 일일 보고서 저녁 섹션, MealTimeDialog 출/퇴근 범위 검증 - check_dinner_reminder 신규, 알림 임계값 5개 설정화 - closeEvent timer/notifier 정리 (aboutToQuit hook) - 마이그레이션 12개 모두 _conn() + try/finally - DB 인덱스 5개 추가 (break/overtime/leave date) Tests - pytest 116/116 PASS, 통합 시나리오 48/48 PASS Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
229 lines
8.4 KiB
Python
229 lines
8.4 KiB
Python
"""
|
|
전역 예외 후킹 + Gitea Issues 자동 보고.
|
|
|
|
sys.excepthook을 등록해 처리되지 않은 예외를 가로채:
|
|
1. crash_log 테이블에 저장
|
|
2. 사용자에게 다이얼로그로 알림 + "Gitea에 보고" / "복사" 옵션
|
|
"""
|
|
from __future__ import annotations
|
|
import json
|
|
import sys
|
|
import traceback
|
|
import urllib.request
|
|
import urllib.error
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
# 자체 호스팅 Gitea (updater_client와 동일)
|
|
GITEA_API = 'https://kindnick-git.duckdns.org/api/v1/repos/kindnick/Clock_out_Time_Calculator'
|
|
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ClockOutCalculator/2.4'
|
|
|
|
|
|
def install_global_handler(db, app_version: str = 'unknown') -> None:
|
|
"""sys.excepthook 등록. db는 crash_log 저장용.
|
|
|
|
각 단계(log → dialog → 파일 fallback → original hook)는 모두 독립 try로 감싸
|
|
하나가 실패해도 다음 단계가 동작. 가장 최후 fallback은 stderr + 로컬 파일.
|
|
"""
|
|
original = sys.excepthook
|
|
|
|
def hook(exc_type, exc_value, exc_tb):
|
|
if exc_type is KeyboardInterrupt:
|
|
original(exc_type, exc_value, exc_tb)
|
|
return
|
|
|
|
# 1단계: traceback 직렬화 (실패하면 minimal fallback)
|
|
try:
|
|
tb_str = ''.join(traceback.format_exception(exc_type, exc_value, exc_tb))
|
|
except Exception:
|
|
tb_str = f"{exc_type}: {exc_value} (traceback formatting failed)"
|
|
|
|
type_name = getattr(exc_type, '__name__', str(exc_type))
|
|
msg = str(exc_value)
|
|
|
|
# 2단계: DB 로깅 (DB 잠금/디스크 풀 등으로 실패 가능 — 단계 분리)
|
|
log_ok = False
|
|
try:
|
|
_log_crash(db, type_name, msg, tb_str, app_version)
|
|
log_ok = True
|
|
except Exception as log_err:
|
|
_fallback_to_file(type_name, msg, tb_str, app_version,
|
|
f"DB log failed: {log_err}")
|
|
|
|
# 3단계: 다이얼로그 (PyQt 미초기화/이미 종료 등으로 실패 가능)
|
|
try:
|
|
_show_dialog(db, type_name, msg, tb_str, app_version)
|
|
except Exception as dlg_err:
|
|
# 다이얼로그 실패 시 stderr + 파일에 기록 (DB 로깅도 실패했으면 이게 유일한 흔적)
|
|
if not log_ok:
|
|
_fallback_to_file(type_name, msg, tb_str, app_version,
|
|
f"DB+dialog both failed; dialog err: {dlg_err}")
|
|
try:
|
|
print(f"\n[CRASH] {type_name}: {msg}\n{tb_str}", file=sys.stderr)
|
|
except Exception:
|
|
pass
|
|
|
|
# 4단계: 원래 hook도 호출 (콘솔 출력 + 종료)
|
|
try:
|
|
original(exc_type, exc_value, exc_tb)
|
|
except Exception:
|
|
pass # 마지막 단계라 더 할 게 없음
|
|
|
|
sys.excepthook = hook
|
|
|
|
|
|
def _fallback_to_file(exc_type: str, message: str, tb: str,
|
|
version: str, reason: str) -> None:
|
|
"""DB 로깅이 실패한 경우 ~/.clockout_logs/crashes.log에 append.
|
|
|
|
Best-effort — 파일 쓰기 실패도 silent (이 시점엔 뭐 할 게 없음).
|
|
"""
|
|
try:
|
|
from pathlib import Path
|
|
log_dir = Path.home() / '.clockout_logs'
|
|
log_dir.mkdir(parents=True, exist_ok=True)
|
|
log_file = log_dir / 'crashes.log'
|
|
with open(log_file, 'a', encoding='utf-8') as f:
|
|
f.write(
|
|
f"\n=== {datetime.now().isoformat(timespec='seconds')} ===\n"
|
|
f"version={version} reason={reason}\n"
|
|
f"{exc_type}: {message}\n{tb}\n"
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def _log_crash(db, exc_type: str, message: str, tb: str, version: str) -> None:
|
|
"""crash_log 테이블에 기록."""
|
|
try:
|
|
conn = db.get_connection()
|
|
cursor = conn.cursor()
|
|
# 테이블 자동 생성 (마이그레이션 누락 대비)
|
|
cursor.execute('''
|
|
CREATE TABLE IF NOT EXISTS crash_log (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
exception_type TEXT,
|
|
message TEXT,
|
|
traceback TEXT,
|
|
app_version TEXT,
|
|
reported BOOLEAN DEFAULT 0
|
|
)
|
|
''')
|
|
cursor.execute('''
|
|
INSERT INTO crash_log (exception_type, message, traceback, app_version)
|
|
VALUES (?, ?, ?, ?)
|
|
''', (exc_type, message, tb, version))
|
|
conn.commit()
|
|
conn.close()
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def _show_dialog(db, exc_type: str, message: str, tb: str, version: str) -> None:
|
|
"""크래시 알림 + Gitea 보고/복사 버튼."""
|
|
try:
|
|
from PyQt5.QtWidgets import (QApplication, QMessageBox, QDialog,
|
|
QVBoxLayout, QLabel, QTextEdit,
|
|
QHBoxLayout, QPushButton, QLineEdit)
|
|
except ImportError:
|
|
return
|
|
|
|
app = QApplication.instance()
|
|
if app is None:
|
|
return
|
|
|
|
dlg = QDialog()
|
|
dlg.setWindowTitle("⚠️ 오류 발생")
|
|
dlg.setMinimumSize(560, 420)
|
|
layout = QVBoxLayout()
|
|
|
|
title = QLabel(f"<b>{exc_type}</b>: {message[:200]}")
|
|
title.setWordWrap(True)
|
|
layout.addWidget(title)
|
|
|
|
layout.addWidget(QLabel("무엇을 하다가 발생했나요? (선택)"))
|
|
user_note = QLineEdit()
|
|
user_note.setPlaceholderText("예: 출근 버튼 누른 직후")
|
|
layout.addWidget(user_note)
|
|
|
|
layout.addWidget(QLabel("기술 정보 (자동 보고에 포함):"))
|
|
tb_view = QTextEdit()
|
|
tb_view.setReadOnly(True)
|
|
tb_view.setFont(__import__('PyQt5.QtGui', fromlist=['QFont']).QFont('Consolas', 9))
|
|
tb_view.setText(tb[-3000:]) # 너무 긴 traceback 제한
|
|
layout.addWidget(tb_view, 1)
|
|
|
|
btn_row = QHBoxLayout()
|
|
cancel_btn = QPushButton("닫기")
|
|
copy_btn = QPushButton("📋 복사")
|
|
report_btn = QPushButton("📤 Gitea에 보고")
|
|
|
|
has_token = bool(db.get_setting('gitea_feedback_token', '') or '')
|
|
enabled = db.get_setting('gitea_feedback_enabled', 'false').lower() == 'true'
|
|
if not (has_token and enabled):
|
|
report_btn.setEnabled(False)
|
|
report_btn.setToolTip("설정 → 데이터 관리 → Gitea 피드백 토큰 입력 후 활성화 필요")
|
|
|
|
def do_copy():
|
|
clipboard = QApplication.clipboard()
|
|
text = (
|
|
f"## {exc_type}\n\n{message}\n\n"
|
|
f"**Version**: {version}\n**Note**: {user_note.text()}\n\n"
|
|
f"```\n{tb}\n```"
|
|
)
|
|
clipboard.setText(text)
|
|
copy_btn.setText("✓ 복사됨")
|
|
|
|
def do_report():
|
|
token = db.get_setting('gitea_feedback_token', '') or ''
|
|
if not token:
|
|
QMessageBox.warning(dlg, "토큰 없음", "Gitea PAT를 설정에서 먼저 입력하세요.")
|
|
return
|
|
title_str = f"[Auto] {exc_type}: {message[:80]}"
|
|
body = (
|
|
f"**Version**: `{version}`\n"
|
|
f"**Time**: {datetime.now().isoformat(timespec='seconds')}\n"
|
|
f"**User note**: {user_note.text() or '(none)'}\n\n"
|
|
f"### Traceback\n```\n{tb[-3000:]}\n```"
|
|
)
|
|
ok = _send_to_gitea(token, title_str, body)
|
|
if ok:
|
|
QMessageBox.information(dlg, "보고 완료", "Gitea Issues에 등록되었습니다.")
|
|
report_btn.setText("✓ 보고됨")
|
|
report_btn.setEnabled(False)
|
|
else:
|
|
QMessageBox.warning(dlg, "보고 실패", "네트워크 또는 토큰 권한 문제일 수 있습니다.")
|
|
|
|
cancel_btn.clicked.connect(dlg.reject)
|
|
copy_btn.clicked.connect(do_copy)
|
|
report_btn.clicked.connect(do_report)
|
|
btn_row.addWidget(cancel_btn)
|
|
btn_row.addStretch()
|
|
btn_row.addWidget(copy_btn)
|
|
btn_row.addWidget(report_btn)
|
|
layout.addLayout(btn_row)
|
|
|
|
dlg.setLayout(layout)
|
|
dlg.exec_()
|
|
|
|
|
|
def _send_to_gitea(token: str, title: str, body: str) -> bool:
|
|
"""Gitea Issues API로 issue 생성."""
|
|
payload = json.dumps({'title': title, 'body': body}).encode('utf-8')
|
|
req = urllib.request.Request(
|
|
f"{GITEA_API}/issues",
|
|
data=payload,
|
|
headers={
|
|
'Authorization': f'token {token}',
|
|
'Content-Type': 'application/json',
|
|
'User-Agent': USER_AGENT,
|
|
},
|
|
method='POST',
|
|
)
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=10) as resp:
|
|
return 200 <= resp.status < 300
|
|
except (urllib.error.URLError, urllib.error.HTTPError, OSError, TimeoutError):
|
|
return False
|