33import os
44from pathlib import Path
55import re
6- from typing import Any , Dict , List , Tuple , Union
6+ from typing import Any , Dict , Generator , List , Tuple , Union
77
88from icalendar import Calendar
99
1818)
1919from app .database .database import SessionLocal
2020from app .routers .event import create_event
21+ from loguru import logger
2122
2223
2324DATE_FORMAT = "%m-%d-%Y"
@@ -91,9 +92,10 @@ def is_file_valid_to_save_to_database(events: List[Dict[str, Union[str, Any]]],
9192 return same_date_counter <= max_event_start_date
9293
9394
94- def open_txt_file (txt_file : str ) -> List [str ]:
95+ def open_txt_file (txt_file : str ) -> Generator [str , None , None ]:
9596 with open (txt_file , "r" ) as text :
96- return text .readlines ()
97+ for row in text .readlines ():
98+ yield row
9799
98100
99101def save_calendar_content_txt (event : str , calendar_content : List ) -> bool :
@@ -114,8 +116,7 @@ def save_calendar_content_txt(event: str, calendar_content: List) -> bool:
114116
115117def import_txt_file (txt_file : str ) -> List [Dict [str , Union [str , Any ]]]:
116118 calendar_content = []
117- events = open_txt_file (txt_file )
118- for event in events :
119+ for event in open_txt_file (txt_file ):
119120 if (not is_event_text_valid (event ) or
120121 not save_calendar_content_txt (event , calendar_content )):
121122 return []
@@ -126,7 +127,8 @@ def open_ics(ics_file: str) -> Union[List, Calendar]:
126127 with open (ics_file , "r" ) as ics :
127128 try :
128129 calendar_read = Calendar .from_ical (ics .read ())
129- except (IndexError , ValueError ):
130+ except (IndexError , ValueError ) as e :
131+ logger .error (f"open_ics function failed error message: { e } " )
130132 return []
131133 return calendar_read
132134
0 commit comments