AutoPercenty3/updateManager/update_history.py

99 lines
3.9 KiB
Python

import json
import os
import logging
from datetime import datetime
from typing import List, Dict, Any, Optional
from updateManager.update_types import UpdateInfo, UpdateLevel
# 설정 파일 경로를 상수로 정의
SETTINGS_FILE = "release_note_settings.json"
class UpdateHistory:
def __init__(self, logger: logging.Logger):
self.logger = logger
self.history_file = os.path.join(os.path.dirname(__file__), 'update_history.json')
self._load_history()
def _load_history(self) -> None:
"""업데이트 히스토리를 로드합니다."""
try:
if os.path.exists(self.history_file):
with open(self.history_file, 'r', encoding='utf-8') as f:
self.history = json.load(f)
else:
self.history = []
except Exception as e:
self.logger.log(f"업데이트 히스토리 로드 중 오류 발생: {str(e)}", level=logging.ERROR)
self.history = []
def _save_history(self) -> None:
"""업데이트 히스토리를 저장합니다."""
try:
with open(self.history_file, 'w', encoding='utf-8') as f:
json.dump(self.history, f, ensure_ascii=False, indent=2)
except Exception as e:
self.logger.log(f"업데이트 히스토리 저장 중 오류 발생: {str(e)}", level=logging.ERROR)
def add_update(self, update_info: UpdateInfo) -> None:
"""
업데이트 정보를 히스토리에 추가합니다.
:param update_info: 업데이트 정보 객체
"""
update_record = {
'version': update_info.version,
'level': update_info.level.value,
'release_date': update_info.release_date,
'release_note': update_info.release_note,
'is_mandatory': update_info.is_mandatory,
'download_url': update_info.download_url,
'update_date': datetime.now().isoformat()
}
self.history.append(update_record)
self._save_history()
def get_history(self) -> List[Dict[str, Any]]:
"""
업데이트 히스토리를 반환합니다.
:return: 업데이트 히스토리 목록
"""
return self.history
def get_last_update(self) -> Optional[Dict[str, Any]]:
"""
마지막 업데이트 정보를 반환합니다.
:return: 마지막 업데이트 정보 또는 None
"""
return self.history[-1] if self.history else None
def should_show_release_note(self, version: str) -> bool:
"""특정 버전의 릴리즈 노트를 표시해야 하는지 확인합니다."""
try:
if not os.path.exists(SETTINGS_FILE):
return True
with open(SETTINGS_FILE, "r", encoding='utf-8') as f:
settings = json.load(f)
return version not in settings.get("hidden_versions", [])
except Exception as e:
print(f"릴리즈 노트 설정 로드 중 오류: {e}")
return True
def mark_release_note_as_hidden(self, version: str):
"""특정 버전의 릴리즈 노트를 숨김으로 표시합니다."""
try:
settings = {}
if os.path.exists(SETTINGS_FILE):
with open(SETTINGS_FILE, "r", encoding='utf-8') as f:
settings = json.load(f)
hidden_versions = settings.get("hidden_versions", [])
if version not in hidden_versions:
hidden_versions.append(version)
settings["hidden_versions"] = hidden_versions
with open(SETTINGS_FILE, "w", encoding='utf-8') as f:
json.dump(settings, f, ensure_ascii=False, indent=2)
except Exception as e:
print(f"릴리즈 노트 설정 저장 중 오류: {e}")