Files
LEDMatrix/scripts/utils/clear_cache.py
Chuck 55161f309b fix: remove unused imports and bare exception aliases (pyflakes F401/F841)
Remove unused imports across 86 files in src/, web_interface/, test/,
and scripts/ using autoflake. No logic changes — only dead import
statements and unused names in from-imports are removed.

Also remove bare exception aliases where the variable is never
referenced in the handler body:
- src/cache/disk_cache.py: except (IOError, OSError, PermissionError) as e
- src/cache_manager.py: except (OSError, IOError, PermissionError) as perm_error
- src/plugin_system/resource_monitor.py: except Exception as e
- web_interface/app.py: except Exception as read_err

86 files changed, 205 lines removed, 18 pre-existing test failures unchanged.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-14 10:41:55 -04:00

128 lines
4.4 KiB
Python

#!/usr/bin/env python3
"""
Cache clearing utility for LEDMatrix
This script allows manual clearing of specific cache keys or all cache data.
"""
import os
import sys
import argparse
# Add the src directory to the path so we can import our modules
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
from cache_manager import CacheManager
def list_cache_keys(cache_manager):
"""List all available cache keys."""
cache_dir = cache_manager.cache_dir
if not cache_dir or not os.path.exists(cache_dir):
print(f"Cache directory does not exist: {cache_dir}")
return []
cache_files = []
for file in os.listdir(cache_dir):
if file.endswith('.json'):
cache_files.append(file[:-5]) # Remove .json extension
return cache_files
def clear_specific_cache(cache_manager, key):
"""Clear a specific cache key."""
try:
cache_manager.clear_cache(key)
print(f"✓ Cleared cache key: {key}")
return True
except Exception as e:
print(f"✗ Error clearing cache key '{key}': {e}")
return False
def clear_all_cache(cache_manager):
"""Clear all cache data."""
try:
cache_manager.clear_cache()
print("✓ Cleared all cache data")
return True
except Exception as e:
print(f"✗ Error clearing all cache: {e}")
return False
def show_cache_info(cache_manager, key=None):
"""Show information about cache entries."""
if key:
try:
data = cache_manager.get(key)
if data is not None:
print(f"Cache key '{key}' exists with data type: {type(data)}")
if isinstance(data, dict):
print(f" Keys: {list(data.keys())}")
if 'games' in data:
print(f" Number of games: {len(data['games']) if isinstance(data['games'], dict) else 'N/A'}")
elif isinstance(data, list):
print(f" Number of items: {len(data)}")
else:
print(f" Data: {str(data)[:100]}...")
else:
print(f"Cache key '{key}' does not exist or is expired")
except Exception as e:
print(f"Error checking cache key '{key}': {e}")
else:
# Show all cache keys
keys = list_cache_keys(cache_manager)
if keys:
print("Available cache keys:")
for key in sorted(keys):
print(f" - {key}")
else:
print("No cache keys found")
def main():
parser = argparse.ArgumentParser(description='Clear LEDMatrix cache data')
parser.add_argument('--list', '-l', action='store_true',
help='List all available cache keys')
parser.add_argument('--clear-all', '-a', action='store_true',
help='Clear all cache data')
parser.add_argument('--clear', '-c', type=str, metavar='KEY',
help='Clear a specific cache key')
parser.add_argument('--info', '-i', type=str, metavar='KEY',
help='Show information about a specific cache key')
args = parser.parse_args()
# Initialize cache manager
cache_manager = CacheManager()
if args.list:
show_cache_info(cache_manager)
elif args.clear_all:
print("Clearing all cache data...")
clear_all_cache(cache_manager)
elif args.clear:
print(f"Clearing cache key: {args.clear}")
clear_specific_cache(cache_manager, args.clear)
elif args.info:
show_cache_info(cache_manager, args.info)
else:
# Default: show available options
print("LEDMatrix Cache Utility")
print("=" * 30)
print()
print("Available commands:")
print(" --list, -l List all cache keys")
print(" --clear-all, -a Clear all cache data")
print(" --clear KEY, -c Clear specific cache key")
print(" --info KEY, -i Show info about cache key")
print()
print("Examples:")
print(" python clear_cache.py --list")
print(" python clear_cache.py --clear milb_live_api_data")
print(" python clear_cache.py --clear-all")
print(" python clear_cache.py --info milb_upcoming_api_data")
print()
# Show current cache status
show_cache_info(cache_manager)
if __name__ == "__main__":
main()