From 62147cf5e78a2f4ebc060439bfd566997b80a96e Mon Sep 17 00:00:00 2001 From: Usama Sadiq Date: Wed, 15 Nov 2023 21:58:01 +0500 Subject: [PATCH] fix: implement suggested refactoring (#455) --- barcalendar.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/barcalendar.py b/barcalendar.py index 64abcc4f..182c2160 100644 --- a/barcalendar.py +++ b/barcalendar.py @@ -16,33 +16,34 @@ """ +import atexit import colorsys import datetime import itertools +import logging import re import time -import atexit -import logging import requests import yaml + def setup_custom_logging(): """ - Creating a custom logger to flush all Warning logs at the end of the script + Create a custom logger to flush all Warning logs at the end of the script to avoid breaking the console script being generated by the script """ class DelayedLogHandler(logging.Handler): def __init__(self): super().__init__() - self.log_messages = [] + self.log_records = [] def emit(self, record): - self.log_messages.append(record) + self.log_records.append(f"// {record}") - def _flush_log_messages(handler): - for message in handler.log_messages: - print(message) + def _flush_log_records(handler): + for record in handler.log_records: + print(record) # Create a custom log handler custom_log_handler = DelayedLogHandler() @@ -52,8 +53,8 @@ def _flush_log_messages(handler): eol_logger = logging.getLogger("eol_logger") eol_logger.addHandler(custom_log_handler) - # Register a function to flush log messages at exit - atexit.register(_flush_log_messages, custom_log_handler) + # Register a function to flush log records at exit + atexit.register(_flush_log_records, custom_log_handler) # Return custom logger to use in the script return eol_logger @@ -97,7 +98,7 @@ def validate_version_date(project, version, year, month, check_start=False): if (year, month) == (int(eol_year), int(eol_month)): return (year, month) else: - eol_logger.warning(f"Invalid EOL: Update {project} {version} {date_type} to {eol_year}/{eol_month} instead") + eol_logger.warning(f"Invalid EOL: Update {project} {version} {date_type} to {eol_month}/{eol_year} instead") return (int(eol_year), int(eol_month)) except requests.exceptions.RequestException as e: eol_logger.error(f"API request failed with an exception: {str(e)}")