1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# 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, time_period=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.time_period = time_period
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
entry.time_period = None
for start_date, end_date, original_id in list_of_events:
entry = self.get_entry_by_id(original_id)
if not entry:
continue
entry.corrected_start_date = start_date
entry.corrected_end_date = end_date
|