diff --git a/debug_of_the_day.py b/debug_of_the_day.py new file mode 100644 index 00000000..5ead4940 --- /dev/null +++ b/debug_of_the_day.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python3 +""" +Debug script for OfTheDayManager issues +Run this on the Raspberry Pi to diagnose the problem + +Usage: +1. Copy this file to your Raspberry Pi +2. Run: python3 debug_of_the_day.py +3. Check the output for any errors or issues + +This script will help identify why the OfTheDayManager is not loading data files. +""" + +import json +import os +import sys +from datetime import date + +def debug_of_the_day(): + print("=== OfTheDayManager Debug Script ===") + print(f"Current working directory: {os.getcwd()}") + print(f"Python path: {sys.path}") + + # Check if we're in the right directory + if not os.path.exists('config/config.json'): + print("ERROR: config/config.json not found. Make sure you're running from the LEDMatrix root directory.") + return + + # Load the actual config + try: + with open('config/config.json', 'r') as f: + config = json.load(f) + print("✓ Successfully loaded config.json") + except Exception as e: + print(f"ERROR loading config.json: {e}") + return + + # Check of_the_day configuration + of_the_day_config = config.get('of_the_day', {}) + print(f"OfTheDay enabled: {of_the_day_config.get('enabled', False)}") + + if not of_the_day_config.get('enabled', False): + print("OfTheDay is disabled in config!") + return + + categories = of_the_day_config.get('categories', {}) + print(f"Categories configured: {list(categories.keys())}") + + # Test each category + today = date.today() + day_of_year = today.timetuple().tm_yday + print(f"Today is day {day_of_year} of the year") + + for category_name, category_config in categories.items(): + print(f"\n--- Testing category: {category_name} ---") + print(f"Category enabled: {category_config.get('enabled', True)}") + + if not category_config.get('enabled', True): + print("Category is disabled, skipping...") + continue + + data_file = category_config.get('data_file') + print(f"Data file: {data_file}") + + # Test path resolution + if not os.path.isabs(data_file): + if data_file.startswith('of_the_day/'): + file_path = os.path.join(os.getcwd(), data_file) + else: + file_path = os.path.join(os.getcwd(), 'of_the_day', data_file) + else: + file_path = data_file + + file_path = os.path.abspath(file_path) + print(f"Resolved path: {file_path}") + print(f"File exists: {os.path.exists(file_path)}") + + if not os.path.exists(file_path): + print(f"ERROR: Data file not found at {file_path}") + continue + + # Test JSON loading + try: + with open(file_path, 'r', encoding='utf-8') as f: + data = json.load(f) + print(f"✓ Successfully loaded JSON with {len(data)} items") + + # Check for today's entry + day_key = str(day_of_year) + if day_key in data: + item = data[day_key] + print(f"✓ Found entry for day {day_of_year}: {item.get('title', 'No title')}") + else: + print(f"✗ No entry found for day {day_of_year}") + # Show some nearby entries + nearby_days = [k for k in data.keys() if k.isdigit() and abs(int(k) - day_of_year) <= 5] + print(f"Nearby days with entries: {sorted(nearby_days)}") + + except Exception as e: + print(f"ERROR loading JSON: {e}") + import traceback + traceback.print_exc() + + print("\n=== Debug complete ===") + +if __name__ == "__main__": + debug_of_the_day() diff --git a/src/of_the_day_manager.py b/src/of_the_day_manager.py index 8ede25cd..197bd55a 100644 --- a/src/of_the_day_manager.py +++ b/src/of_the_day_manager.py @@ -108,6 +108,8 @@ class OfTheDayManager: return logger.info(f"Loading data files for {len(self.categories)} categories") + logger.info(f"Current working directory: {os.getcwd()}") + logger.info(f"Script directory: {os.path.dirname(__file__)}") for category_name, category_config in self.categories.items(): logger.debug(f"Processing category: {category_name}") @@ -130,16 +132,49 @@ class OfTheDayManager: else: file_path = os.path.join(os.path.dirname(__file__), '..', 'of_the_day', data_file) + # Convert to absolute path for better logging + file_path = os.path.abspath(file_path) + logger.debug(f"Attempting to load {category_name} from: {file_path}") + if os.path.exists(file_path): + logger.debug(f"File exists, checking permissions...") + if not os.access(file_path, os.R_OK): + logger.error(f"File exists but is not readable: {file_path}") + self.data_files[category_name] = {} + continue + + # Get file size for debugging + file_size = os.path.getsize(file_path) + logger.debug(f"File size: {file_size} bytes") + with open(file_path, 'r', encoding='utf-8') as f: self.data_files[category_name] = json.load(f) + logger.info(f"Loaded data file for {category_name}: {len(self.data_files[category_name])} items") logger.debug(f"Sample keys from {category_name}: {list(self.data_files[category_name].keys())[:5]}") + + # Validate that we have data + if not self.data_files[category_name]: + logger.warning(f"Loaded data file for {category_name} but it's empty!") + else: logger.error(f"Data file not found for {category_name}: {file_path}") + logger.error(f"Directory contents: {os.listdir(os.path.dirname(file_path)) if os.path.exists(os.path.dirname(file_path)) else 'Parent directory does not exist'}") self.data_files[category_name] = {} + + except json.JSONDecodeError as e: + logger.error(f"JSON decode error loading data file for {category_name}: {e}") + logger.error(f"File path: {file_path}") + self.data_files[category_name] = {} + except UnicodeDecodeError as e: + logger.error(f"Unicode decode error loading data file for {category_name}: {e}") + logger.error(f"File path: {file_path}") + self.data_files[category_name] = {} except Exception as e: - logger.error(f"Error loading data file for {category_name}: {e}") + logger.error(f"Unexpected error loading data file for {category_name}: {e}") + logger.error(f"File path: {file_path}") + import traceback + logger.error(f"Traceback: {traceback.format_exc()}") self.data_files[category_name] = {} def _load_todays_items(self): @@ -150,6 +185,7 @@ class OfTheDayManager: today = date.today() day_of_year = today.timetuple().tm_yday logger.info(f"Loading items for day {day_of_year} of the year") + logger.debug(f"Available data files: {list(self.data_files.keys())}") self.current_items = {} @@ -161,9 +197,13 @@ class OfTheDayManager: data = self.data_files.get(category_name, {}) if not data: logger.warning(f"No data loaded for category: {category_name}") + logger.debug(f"Data files available: {list(self.data_files.keys())}") + logger.debug(f"Category config: {category_config}") continue logger.debug(f"Checking category {category_name} for day {day_of_year}") + logger.debug(f"Data file contains {len(data)} items") + # Get item for today (day of year) item = data.get(str(day_of_year)) if item: @@ -171,7 +211,11 @@ class OfTheDayManager: logger.info(f"Loaded {category_name} item for day {day_of_year}: {item.get('title', 'No title')}") else: logger.warning(f"No item found for {category_name} on day {day_of_year}") - logger.debug(f"Available days in {category_name}: {list(data.keys())[:10]}...") + # Show more detailed information about available days + available_days = [k for k in data.keys() if k.isdigit()] + nearby_days = [k for k in available_days if abs(int(k) - day_of_year) <= 5] + logger.debug(f"Available days in {category_name}: {sorted(available_days)[:10]}...") + logger.debug(f"Days near {day_of_year}: {sorted(nearby_days)}") self.current_day = today self.current_category_index = 0