Spaces:
Sleeping
Sleeping
| """ | |
| ์ฌ๋ฌ ๋ชจ๋์์ ๊ณตํต์ผ๋ก ์ฌ์ฉํ๋ ์ ํธ๋ฆฌํฐ ํจ์ | |
| """ | |
| import os | |
| import json | |
| import uuid | |
| import logging | |
| from datetime import datetime | |
| from typing import Dict, List, Any, Optional, Union | |
| from src import config | |
| logger = config.setup_logger("common") | |
| def generate_unique_id(prefix: str = "") -> str: | |
| """ | |
| ๊ณ ์ ID ์์ฑ | |
| Args: | |
| prefix: ID ์ ๋์ฌ | |
| Returns: | |
| ๊ณ ์ ID ๋ฌธ์์ด | |
| """ | |
| unique_id = str(uuid.uuid4()) | |
| if prefix: | |
| return f"{prefix}-{unique_id}" | |
| return unique_id | |
| def save_json_data(data: Any, filename: str, directory: str = None) -> str: | |
| """ | |
| ๋ฐ์ดํฐ๋ฅผ JSON ํ์ผ๋ก ์ ์ฅ | |
| Args: | |
| data: ์ ์ฅํ ๋ฐ์ดํฐ (JSON ์ง๋ ฌํ ๊ฐ๋ฅ) | |
| filename: ํ์ผ๋ช | |
| directory: ์ ์ฅ ๋๋ ํ ๋ฆฌ (๊ธฐ๋ณธ๊ฐ: config.DATA_DIR) | |
| Returns: | |
| ์ ์ฅ๋ ํ์ผ์ ์ ์ฒด ๊ฒฝ๋ก | |
| """ | |
| directory = directory or config.DATA_DIR | |
| os.makedirs(directory, exist_ok=True) | |
| # ํ์์คํฌํ ์ถ๊ฐ (ํ์ผ๋ช ์ ์ด๋ฏธ ์์ผ๋ฉด ์ถ๊ฐํ์ง ์์) | |
| if not any(part.isdigit() and len(part) == 8 for part in filename.split('_')): | |
| timestamp = datetime.now().strftime("%Y%m%d") | |
| base_name, ext = os.path.splitext(filename) | |
| filename = f"{base_name}_{timestamp}{ext}" | |
| filepath = os.path.join(directory, filename) | |
| with open(filepath, 'w', encoding='utf-8') as f: | |
| json.dump(data, f, ensure_ascii=False, indent=2) | |
| logger.info(f"๋ฐ์ดํฐ ์ ์ฅ ์๋ฃ: {filepath}") | |
| return filepath | |
| def format_error_response(error: Exception) -> Dict[str, Any]: | |
| """ | |
| ์์ธ๋ฅผ API ์๋ต ํ์์ผ๋ก ๋ณํ | |
| Args: | |
| error: ์์ธ ๊ฐ์ฒด | |
| Returns: | |
| ์ค๋ฅ ์ ๋ณด๋ฅผ ํฌํจํ ๋์ ๋๋ฆฌ | |
| """ | |
| error_code = getattr(error, 'error_code', 'UNKNOWN') | |
| error_detail = { | |
| "status": "error", | |
| "error": { | |
| "type": error.__class__.__name__, | |
| "code": error_code, | |
| "message": str(error), | |
| "timestamp": datetime.now().isoformat() | |
| } | |
| } | |
| # ์ถ๊ฐ ์์ธ ์ ๋ณด๊ฐ ์์ผ๋ฉด ํฌํจ | |
| if hasattr(error, 'status_code'): | |
| error_detail["error"]["status_code"] = error.status_code | |
| if hasattr(error, 'response') and error.response: | |
| error_detail["error"]["response"] = error.response | |
| return error_detail | |
| def sanitize_string(text: str) -> str: | |
| """ | |
| ๋ฌธ์์ด์์ ์ํํ ๋ฌธ์ ๋๋ ์ฝ๋ ์ฝ์ ์๋ ์ ๊ฑฐ | |
| Args: | |
| text: ์ ์ ํ ๋ฌธ์์ด | |
| Returns: | |
| ์ ์ ๋ ๋ฌธ์์ด | |
| """ | |
| if not text: | |
| return "" | |
| # ๊ธฐ๋ณธ์ ์ธ ์ ์ (์ค๋ฐ๊ฟ, ์คํฌ๋ฆฝํธ ํ๊ทธ ๋ฑ ์ฒ๋ฆฌ) | |
| sanitized = text.replace('<script>', '<script>') \ | |
| .replace('</script>', '</script>') | |
| # ํ์ํ ๊ฒฝ์ฐ ์ถ๊ฐ ์ ์ ๋ก์ง ๊ตฌํ | |
| return sanitized | |
| def merge_metadata(base: Dict[str, Any], updates: Dict[str, Any]) -> Dict[str, Any]: | |
| """ | |
| ๋ฉํ๋ฐ์ดํฐ ๋ณํฉ | |
| Args: | |
| base: ๊ธฐ๋ณธ ๋ฉํ๋ฐ์ดํฐ | |
| updates: ์ ๋ฐ์ดํธํ ๋ฉํ๋ฐ์ดํฐ | |
| Returns: | |
| ๋ณํฉ๋ ๋ฉํ๋ฐ์ดํฐ | |
| """ | |
| result = base.copy() | |
| if updates: | |
| for key, value in updates.items(): | |
| if key in result and isinstance(result[key], dict) and isinstance(value, dict): | |
| # ์ค์ฒฉ๋ ๋์ ๋๋ฆฌ๋ ์ฌ๊ท์ ์ผ๋ก ๋ณํฉ | |
| result[key] = merge_metadata(result[key], value) | |
| else: | |
| # ๊ทธ ์ธ์๋ ๋ฎ์ด์ฐ๊ธฐ | |
| result[key] = value | |
| return result | |
| def normalize_doc_type(doc_type: str) -> str: | |
| """ | |
| ๋ฌธ์ ์ ํ ์ ๊ทํ | |
| Args: | |
| doc_type: ์ ๊ทํํ ๋ฌธ์ ์ ํ | |
| Returns: | |
| ์ ๊ทํ๋ ๋ฌธ์ ์ ํ | |
| """ | |
| if not doc_type: | |
| return "" | |
| # ๋๋ฌธ์๋ก ๋ณํํ๊ณ ๊ณต๋ฐฑ ์ ๊ฑฐ | |
| normalized = doc_type.upper().strip() | |
| # ์๋ ค์ง ๋ฌธ์ ์ ํ ๋งคํ | |
| mapping = { | |
| "KDS": "KDS", | |
| "KOREAN DESIGN STANDARD": "KDS", | |
| "KCS": "KCS", | |
| "KOREAN CONSTRUCTION SPECIFICATION": "KCS" | |
| } | |
| return mapping.get(normalized, normalized) | |