Files
crossfit/session_config.py
2025-07-21 02:26:32 +02:00

58 lines
2.3 KiB
Python

import json
import logging
# Class to handle session configuration
class SessionConfig:
"""
Class to handle loading and managing preferred sessions configuration.
"""
@staticmethod
def load_preferred_sessions(config_file="preferred_sessions.json"):
"""
Load preferred sessions from a JSON file.
Args:
config_file (str): Path to the JSON file containing preferred sessions.
Returns:
List[Tuple[int, str, str]]: List of preferred sessions in the format
(day_of_week, start_time, session_name_contains)
"""
preferred_sessions = []
try:
# Attempt to open and read the JSON file
with open(config_file, "r") as f:
data = json.load(f)
# Validate and parse each item in the JSON data
for item in data:
day_of_week = item.get("day_of_week", 0) # Default to Monday if not specified
start_time = item.get("start_time", "00:00") # Default to midnight if not specified
session_name_contains = item.get("session_name_contains", "") # Default to empty string if not specified
# Append the parsed session to the list
preferred_sessions.append((day_of_week, start_time, session_name_contains))
except FileNotFoundError:
# Log a warning if the file is not found
logging.warning(f"Configuration file '{config_file}' not found. Falling back to default settings.")
except json.JSONDecodeError:
# Log a warning if the file is not a valid JSON
logging.warning(f"Failed to decode JSON from file '{config_file}'. Falling back to default settings.")
# Fallback to default hardcoded sessions if no valid sessions were loaded
if not preferred_sessions:
preferred_sessions = [
(2, "18:30", "CONDITIONING"), # Wednesday 18:30 CONDITIONING
(4, "17:00", "WEIGHTLIFTING"), # Friday 17:00 WEIGHTLIFTING
(5, "12:30", "HYROX"), # Saturday 12:30 HYROX
]
return preferred_sessions
# Load preferred sessions using the SessionConfig class
PREFERRED_SESSIONS = SessionConfig.load_preferred_sessions()