Implementation of a basic Logging/Debugging System

This commit is contained in:
gmorks
2025-01-30 09:03:31 -06:00
parent 51612bdbfd
commit 998a2d277f
5 changed files with 281 additions and 23 deletions
+65 -14
View File
@@ -3,18 +3,17 @@ CSV handling functionality for Animagine Prompt Node
"""
import pandas as pd
import logging
from typing import Optional, List, Dict
from typing import Optional, Dict
import os
logger = logging.getLogger('AnimaginePrompt')
import time
from .logger import logger
class CSVHandler:
"""
Handles loading and processing of CSV files containing character data
"""
REQUIRED_COLUMNS = ['GENDER', 'CHARACTER', 'COPYRIGHT'] # Notar las mayúsculas
REQUIRED_COLUMNS = ['GENDER', 'CHARACTER', 'COPYRIGHT']
def __init__(self):
self.current_data = None
@@ -26,21 +25,33 @@ class CSVHandler:
"""
try:
if not os.path.exists(filepath):
logger.error(f"CSV file not found: {filepath}")
logger.log_error(
FileNotFoundError(f"CSV file not found: {filepath}"),
"CSV Validation",
{"filepath": filepath}
)
return False
df = pd.read_csv(filepath)
logger.info(f"CSV columns found: {df.columns.tolist()}")
logger.log_csv_operation(
"Validation",
filepath,
{"columns_found": df.columns.tolist()}
)
missing_columns = [col for col in self.REQUIRED_COLUMNS if col not in df.columns]
if missing_columns:
logger.error(f"Missing required columns: {missing_columns}")
logger.log_error(
ValueError("Missing required columns"),
"CSV Validation",
{"missing_columns": missing_columns}
)
return False
return True
except Exception as e:
logger.error(f"Error validating CSV structure: {str(e)}")
logger.log_error(e, "CSV Validation", {"filepath": filepath})
return False
def load_csv(self, filepath: str) -> Optional[pd.DataFrame]:
@@ -48,20 +59,39 @@ class CSVHandler:
Loads CSV file with basic caching
"""
try:
start_time = time.time()
if filepath in self.cache:
logger.log_csv_operation(
"Cache Hit",
filepath,
{"cache_size": len(self.cache)}
)
return self.cache[filepath]
if not self.validate_csv_structure(filepath):
logger.error(f"Invalid CSV structure in file: {filepath}")
return None
df = pd.read_csv(filepath)
self.cache[filepath] = df
self.current_data = df
end_time = time.time()
logger.log_performance(
"CSV Load",
start_time,
end_time,
{
"filepath": filepath,
"rows": len(df),
"cached": True
}
)
return df
except Exception as e:
logger.error(f"Error loading CSV file: {str(e)}")
logger.log_error(e, "CSV Load", {"filepath": filepath})
return None
def get_entry(self, index: int) -> Optional[Dict[str, str]]:
@@ -70,18 +100,39 @@ class CSVHandler:
"""
try:
if self.current_data is None:
logger.log_error(
ValueError("No CSV data loaded"),
"Get Entry",
{"index": index}
)
return None
if index >= len(self.current_data):
logger.error(f"Index {index} out of bounds for CSV with {len(self.current_data)} entries")
logger.log_error(
IndexError(f"Index {index} out of bounds"),
"Get Entry",
{
"index": index,
"max_index": len(self.current_data) - 1
}
)
return None
row = self.current_data.iloc[index]
return {
entry = {
'gender': row['GENDER'],
'character': row['CHARACTER'],
'copyright': row['COPYRIGHT']
}
logger.log_csv_operation(
"Get Entry",
None,
{"index": index, "entry": entry}
)
return entry
except Exception as e:
logger.error(f"Error getting CSV entry: {str(e)}")
logger.log_error(e, "Get Entry", {"index": index})
return None