Skip to content

Commit

Permalink
fix: implement suggested refactoring (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
UsamaSadiq committed Nov 15, 2023
1 parent ab87d6b commit 62147cf
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions barcalendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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)}")
Expand Down

0 comments on commit 62147cf

Please sign in to comment.