Spaces:
Sleeping
Sleeping
File size: 1,645 Bytes
a0d7d94 | 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 | import json
import os
from pathlib import Path
from typing import Dict, Any, Optional
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class DataService:
def __init__(self, data_dir: str = "data"):
"""
Initialize data service.
Args:
data_dir: Path to data directory (default: "data")
"""
self.data_dir = Path(data_dir)
if not self.data_dir.exists():
raise ValueError(f"Data directory '{data_dir}' does not exist")
logger.info(f"DataService initialized with directory: {self.data_dir}")
def get_data(self, filename: str) -> Optional[Dict[str, Any]]:
"""
Read and return complete data from JSON file.
Args:
filename: Name of JSON file (e.g., "GetCustomers.json")
Returns:
Dictionary with complete JSON data or None if error
"""
file_path = self.data_dir / filename
try:
if not file_path.exists():
logger.error(f"File not found: {file_path}")
return None
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
logger.info(f"Successfully loaded data from {filename}")
return data
except json.JSONDecodeError as e:
logger.error(f"Invalid JSON in {filename}: {e}")
return None
except Exception as e:
logger.error(f"Error reading {filename}: {e}")
return None
|