Update logging and file handling

* Added setup_log_location function to handle log directory creation and validation.
* Updated setup_logging function to accept a log location instead of a log file name.
* Updated parse_args function to handle log directory argument and validate the presence of a config file.
* Updated read_file function to raise an error if the specified file does not exist.
* Updated parse_toml function to raise an error if the parsed TOML content is None.
* Added get_calendar_dir function to validate the existence of the calendar directory.
* Added parse_calendar_files function to find and return a list of .ics files in the specified directory.
* Added construct_initial_event_dict function to parse each .ics file into an event dictionary and store them in a list.
* Updated FileChangeHandler class to log warnings instead of errors for invalid or unparseable files.
* Updated CalendarParser class to log warnings if no event dates are found for an event, or if the calendar component is missing.
* Added daemon function to monitor events and update status files at regular intervals.
* Updated main function to use setup_log_location, parse_args, get_calendar_dir, and construct_initial_event_dict functions.
This commit is contained in:
Sam 2024-02-16 11:57:51 +00:00
parent 1176ad9b67
commit 64516db02a
2 changed files with 107 additions and 83 deletions

View File

@ -1,6 +1,6 @@
install:
sudo apt-get update && sudo apt install python3-venv -y
sudo mkdir /opt/remindme_caldav /etc/remindme_caldav
sudo mkdir -p /opt/remindme_caldav/status /etc/remindme_caldav
python3 -m venv /opt/remindme_caldav/.venv
cp remindme_caldav.py alert_processor.py /opt/remindme_caldav/
. /opt/remindme_caldav/.venv/bin/activate && pip3 install -r requirements.txt

View File

@ -14,33 +14,80 @@ from alert_processor import AlertProcessor
logger = logging.getLogger()
def setup_logging(logfile = "log"):
def setup_log_location(logdir):
if not Path.is_dir(logdir):
raise FileNotFoundError(f"Log dir '{logdir}' does not exist. Be sure to create it first.")
log_location = os.path.join(logdir, "log")
status_location = os.path.join(logdir, "status")
alert_history_location = os.path.join(logdir, "alert_history")
return log_location, status_location, alert_history_location
def setup_logging(log_location):
log_format='[%(levelname)s] %(asctime)s %(message)s'
logging.basicConfig(filename = logfile, format=log_format, level=logging.INFO)
logging.basicConfig(filename = log_location, format=log_format, level=logging.INFO)
def parse_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(description="A simple calendar alerting daemon written in Python")
parser.add_argument('--config', type=str, help='Path to config file. Must be .toml')
parser.add_argument('--logfile', type=str, help='Path to logfile file. Defaults to "log" in current directory.', default = "log")
parser.add_argument('--logdir', type=str, help='Path to logfile directory', default = "logs")
parser.add_argument('--loglevel', help="Set the log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)",
type=str, choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'])
return parser.parse_args()
args = parser.parse_args()
if args.config is None:
logger.error("No config file provided. Please use --config path_to_config.toml")
sys.exit(1)
return args
def read_file(filename):
try:
return Path(filename).read_text()
except FileNotFoundError:
logger.error("Error: The specified file does not exist.")
raise FileNotFoundError("Error: The specified file does not exist.")
def parse_toml(content):
try:
return toml.loads(content)
config = toml.loads(content)
if config is None:
logging.error("Invalid config")
sys.exit(1)
return config
except Exception:
logger.error("Error: Failed to parse TOML file.")
raise RuntimeError("Error: Failed to parse TOML file.")
def get_calendar_dir(config):
cal_dir = Path(config["app"]["calendar_dir"])
if not cal_dir.is_dir():
logger.error(f"The provided path to .ics files does not exist: '{cal_dir}'")
sys.exit(1)
return cal_dir
def parse_calendar_files(cal_dir):
files = []
no_files_detected = True
logger.info(f"Looking for calendar files in {cal_dir}...")
while no_files_detected is True:
files = list(cal_dir.glob('*.ics'))
if len(files) != 0:
logger.info("Calendar files detected in sync location!")
no_files_detected = False
return files
def construct_initial_event_dict(cal_dir):
files = parse_calendar_files(cal_dir)
calendar_parser = CalendarParser()
event_list = []
for file in files:
with open(file, 'r') as f:
cal_str = f.read()
try:
event_dict = calendar_parser.parse_calendar(cal_str)
except Exception as e:
logger.warning(f"Error parsing event, skipping. {file}. Error message {e}")
continue
event_list.append(event_dict)
return event_list
def calculate_event_hash(event):
return hashlib.md5(json.dumps(event, sort_keys=True, cls=DateTimeEncoder).encode()).hexdigest()
@ -89,12 +136,12 @@ class FileChangeHandler(FileSystemEventHandler):
with open(event.src_path, 'r') as f:
cal_str = f.read()
except Exception as e:
logger.error(f"Not a valid file: {event.src_path}. Error: {e}")
logger.warning(f"Not a valid file: {event.src_path}. Error: {e}")
return
try:
event_dict = self.calendar_parser.parse_calendar(cal_str) # Use the instance to call parse_calendar method
except Exception as e:
logger.error(f"Failed to parse calendar event at: {event.src_path}. Error: {e}")
logger.warning(f"Failed to parse calendar event at: {event.src_path}. Error: {e}")
return
self.handle_modified(old_event=None, event_dict=event_dict)
@ -205,6 +252,8 @@ class CalendarParser:
generator = RecurringEventGenerator(dtstart, event_dict["rrule"])
recur_info = generator.generate()
event_dates = self.remove_exdates(event_dict["exdate"], recur_info["recur_dates"])
if len(event_dates) == 0:
logging.warning(f"No event dates for event: '{event['summary']}'")
valarms = self.process_valarm(event)
@ -258,7 +307,7 @@ class CalendarParser:
try:
event_info[info] = event[info]
except Exception:
pass
logging.info(f"CalDav componant '{info}' missing for event {event['summary']}")
return event_info
def dtstart_to_datetime(self, dtstart):
@ -299,6 +348,8 @@ class CalendarParser:
valarm = Event.from_ical(subcomponent.to_ical())
timedelta = valarm["trigger"].dt
valarms.append(timedelta)
if len(valarms) == 0:
logging.info(f"No reminders for event: {event['summary']}")
return valarms
def get_next_alert(event, current_time):
@ -317,7 +368,7 @@ def get_next_alert(event, current_time):
next_alert = min(next_alert_list)
return next_alert - dt.timedelta(seconds=5), next_event
def process_alert(current_time, next_alert, next_event, event, config):
def process_alert(current_time, next_alert, next_event, event, config, alert_history_location):
"""
Processes a given alert and passes it to a messaging client.
"""
@ -335,71 +386,12 @@ def process_alert(current_time, next_alert, next_event, event, config):
processor.send_email(event, next_alert, next_event)
except Exception as e:
raise RuntimeError(f"Error sending alert for event. {e}")
with open("alert_history", 'a') as f:
with open(alert_history_location, 'a') as f:
f.write(str(event))
return
def main():
# Parse args
args = parse_args()
setup_logging(args.logfile)
logger = logging.getLogger() # Assign a default value to logger
# Set log level
if args.loglevel is not None:
numeric_level = getattr(logging, args.loglevel.upper(), None) # Convert string to integer
if isinstance(numeric_level, int):
logger = logging.getLogger()
logger.setLevel(numeric_level) # Set the log level
if args.config is None:
logger.error("No config file provided. Please use --config path_to_config.toml")
sys.exit(1)
config_file = read_file(args.config)
config = parse_toml(config_file)
if config is None:
logging.error("Invalid config")
sys.exit(1)
# Get calendar dir
cal_dir = Path(config["app"]["calendar_dir"])
if not cal_dir.is_dir():
logger.error(f"The provided path to .ics files does not exist: '{cal_dir}'")
sys.exit(1)
# Parse calendar events
no_files_detected = True
logger.info(f"Looking for calendar files in {cal_dir}...")
while no_files_detected is True:
files = list(cal_dir.glob('*.ics'))
if len(files) != 0:
logger.info("Calendar files detected in sync location!")
no_files_detected = False
calendar_parser = CalendarParser()
event_list = []
for file in files:
with open(file, 'r') as f:
cal_str = f.read()
try:
event_dict = calendar_parser.parse_calendar(cal_str)
except Exception as e:
logger.warning(f"Error parsing event, skipping. {file}. Error message {e}")
continue
event_list.append(event_dict)
# Start file handler to detect changes to calendar dir
observer = Observer()
handler = FileChangeHandler(event_list)
observer.schedule(handler, cal_dir, recursive=True)
observer.start()
# Start main loop
try:
while True:
with open("status", 'w') as f:
def daemon(status_location, alert_history_location, config, event_list):
with open(status_location, 'w') as f:
f.write("") # Refresh the status file
current_time = dt.datetime.now().replace(tzinfo=pytz.UTC)
for event in event_list:
@ -422,13 +414,45 @@ def main():
Recur info: {event["recur_info"]}
Alert history: {event["alert_history"]}\n"""
monitor_status = textwrap.dedent(monitor_status)
with open("status", 'a') as f:
with open(status_location, 'a') as f:
f.write(monitor_status) # Write the output to the file
f.write("\n")
try:
process_alert(current_time, next_alert, next_event, event, config)
process_alert(current_time, next_alert, next_event, event, config, alert_history_location)
except RuntimeError as e:
logger.warning(f"Error processing alert for event {event['summary']}. Error message: {e}")
return
def main():
# Parse args and initiate logging
args = parse_args()
log_location, status_location, alert_history_location = setup_log_location(args.logdir)
setup_logging(log_location)
logger = logging.getLogger()
# Redefine log level if args passed
if args.loglevel is not None:
numeric_level = getattr(logging, args.loglevel.upper(), None) # Convert string to integer
if isinstance(numeric_level, int):
logger = logging.getLogger()
logger.setLevel(numeric_level) # Set the log level
# Setup initial event_list
config_file = read_file(args.config)
config = parse_toml(config_file)
cal_dir = get_calendar_dir(config)
event_list = construct_initial_event_dict(cal_dir)
# Start file handler to detect changes to calendar dir
observer = Observer()
handler = FileChangeHandler(event_list)
observer.schedule(handler, cal_dir, recursive=True)
observer.start()
# Start main loop
try:
while True:
daemon(status_location, alert_history_location, config, event_list)
time.sleep(1)
except KeyboardInterrupt:
observer.stop()