Update logging and add file detection for calendar files

- Updated setup_logging function to accept a logfile argument with default value "log"
- Changed basicConfig to use the provided logfile argument
- Updated parse_args to set default logfile value to "log" in current directory
- Added while loop to detect calendar files in sync location before proceeding with main logic
- Updated main function to call setup_logging with args.logfile and added file detection logic
This commit is contained in:
mrsu 2024-02-16 00:48:38 +00:00
parent d892cdbd4b
commit f604dcbae3
1 changed files with 14 additions and 11 deletions

View File

@ -14,15 +14,15 @@ from alert_processor import AlertProcessor
logger = logging.getLogger()
def setup_logging():
def setup_logging(logfile = "log"):
log_format='[%(levelname)s] %(asctime)s %(message)s'
logging.basicConfig(format=log_format, level=logging.INFO)
logging.basicConfig(filename = logfile, 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. Default logfile', default = "none")
parser.add_argument('--logfile', type=str, help='Path to logfile file. Defaults to "log" in current directory.', default = "log")
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()
@ -340,12 +340,12 @@ def process_alert(current_time, next_alert, next_event, event, config):
return
def main():
setup_logging()
logger = logging.getLogger() # Assign a default value to logger
# 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
@ -370,12 +370,15 @@ def main():
sys.exit(1)
# Parse calendar events
calendar_parser = CalendarParser()
files = list(cal_dir.glob('*.ics'))
if len(files) == 0:
logger.error("No calendar files in destination location. Did you sync with the caldav server?")
sys.exit(1)
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: