# calendar_manager.py import json import uuid from datetime import datetime class CalendarEntry: def __init__(self, start_date, end_date, keyword, entry_id=None, corrected_start_date=None, corrected_end_date=None, commentary: str = ""): self.id = entry_id if entry_id else str(uuid.uuid4()) # Convert string dates to datetime if necessary self.start_date = ( datetime.fromisoformat(start_date) if isinstance(start_date, str) else start_date ) self.end_date = ( datetime.fromisoformat(end_date) if isinstance(end_date, str) else end_date ) self.keyword = keyword self.corrected_start_date = corrected_start_date self.corrected_end_date = corrected_end_date self.commentary = commentary def to_dict(self): return { 'id': self.id, 'start_date': self.start_date.isoformat(), 'end_date': self.end_date.isoformat(), 'keyword': self.keyword, 'corrected_start_date': self.corrected_start_date.isoformat() if self.corrected_start_date else None, 'corrected_end_date': self.corrected_end_date.isoformat() if self.corrected_end_date else None, 'commentary': self.commentary, } @classmethod def from_dict(cls, data): return cls( start_date=datetime.fromisoformat(data['start_date']), end_date=datetime.fromisoformat(data['end_date']), keyword=data['keyword'], entry_id=data['id'], corrected_start_date=datetime.fromisoformat(data['corrected_start_date']) if data.get('corrected_start_date') else None, corrected_end_date=datetime.fromisoformat(data['corrected_end_date']) if data.get('corrected_end_date') else None, commentary=data.get('commentary', ""), ) def __repr__(self): return (f"CalendarEntry(id={self.id}, start_date={self.start_date}, " f"end_date={self.end_date}, keyword='{self.keyword}', " f"corrected_start_date={self.corrected_start_date}, corrected_end_date={self.corrected_end_date})") class CalendarManager: def __init__(self, filename=None): self.filename = filename self.entries = [] if filename: self.entries = self.add_entries_from_file(filename) def switch_file(self, new_filename): """Switch to a new file.""" self.filename = new_filename def load_file(self, new_filename): """Clears current entries and loads entries from new file.""" self.clear_entries() self.add_entries_from_file(new_filename) self.switch_file(new_filename) def add_entries_from_file(self, file_path): """Add events from another JSON file to the current calendar.""" try: with open(file_path, 'r') as f: data = json.load(f) new_entries = [CalendarEntry.from_dict(entry) for entry in data] self.entries.extend(new_entries) self.save_entries() return new_entries except (FileNotFoundError, json.JSONDecodeError) as e: print(f"Error reading file {file_path}: {e}") return [] def clear_entries(self): self.entries = [] def save_entries(self): """Save the current list of events to the file.""" if self.filename: try: with open(self.filename, 'w') as f: json.dump([entry.to_dict() for entry in self.entries], f, indent=4) except Exception as e: print(f"Error writing to file {self.filename}: {e}") def add_entry(self, start_date, end_date, keyword, commentary: str = ""): """Add a new event to the calendar.""" new_entry = CalendarEntry(start_date, end_date, keyword, commentary=commentary) self.entries.append(new_entry) self.save_entries() return new_entry def modify_entry(self, entry_id, start_date=None, end_date=None, keyword=None, commentary=None): """Modify an existing event by ID.""" entry = self.get_entry_by_id(entry_id) if entry: if start_date: entry.start_date = start_date if end_date: entry.end_date = end_date if keyword: entry.keyword = keyword if commentary is not None: entry.commentary = commentary self.save_entries() return entry return None def delete_entry(self, entry_id): """Delete an event by ID.""" entry = self.get_entry_by_id(entry_id) if entry: self.entries.remove(entry) self.save_entries() return entry return None def get_entry_by_id(self, entry_id): """Get an event by its ID.""" return next((entry for entry in self.entries if entry.id == entry_id), None) def list_entries(self): """List all calendar entries.""" return self.entries def correct_dates(self, list_of_events): for entry in self.entries: entry.corrected_start_date = None entry.corrected_end_date = None for start_date, end_date, original_id in list_of_events: entry = self.get_entry_by_id(original_id) entry.corrected_start_date = start_date entry.corrected_end_date = end_date