From 0deafd5ce31463ece09799fbac642908b34058a4 Mon Sep 17 00:00:00 2001 From: John Vouvakis Manousakis Date: Sat, 18 May 2024 12:26:26 -0700 Subject: [PATCH] Add warnings in a separate file too. This commit configures the logger so that when a log file is requested, another file with the suffix `_warnings` is added, containing only the warnings. The warnings are still being placed in the log file as well. The file name for the warnings file takes care of any file extensions, even when there are periods in the file name itself before the file extension. Extra spaces and color formatting is removed from the warnings so that the log file doesn't contain escape sequences used for coloring the terminal output. --- pelicun/base.py | 33 ++++++++++++++++++++++++++++++--- pelicun/tests/test_base.py | 12 ++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/pelicun/base.py b/pelicun/base.py index 8c95c2b7c..0de718307 100644 --- a/pelicun/base.py +++ b/pelicun/base.py @@ -289,11 +289,18 @@ def __init__(self, verbose, log_show_ms, log_file, print_log): if log_file is None: self.log_file = None + self.warning_file = None else: try: - filepath = Path(log_file).resolve() - self.log_file = str(filepath) - with open(filepath, 'w', encoding='utf-8') as f: + path = Path(log_file) + self.log_file = str(path.resolve()) + name, extension = split_file_name(self.log_file) + self.warning_file = ( + path.parent / (name + '_warnings' + extension) + ).resolve() + with open(self.log_file, 'w', encoding='utf-8') as f: + f.write('') + with open(self.warning_file, 'w', encoding='utf-8') as f: f.write('') except BaseException as err: print( @@ -396,6 +403,14 @@ def emit_warnings(self): for message in self.warning_stack: if message not in self.emitted: warnings.warn(message, PelicunWarning) + if self.warning_file is not None: + with open(self.warning_file, 'a', encoding='utf-8') as f: + f.write( + message.replace(Fore.RED, '') + .replace(Style.RESET_ALL, '') + .replace(self.spaces, '') + ) + self.emitted = self.emitted.union(set(self.warning_stack)) self.warning_stack = [] @@ -445,6 +460,18 @@ def print_system_info(self): pelicun_path = Path(os.path.dirname(os.path.abspath(__file__))) +def split_file_name(file_path: str): + """ + Separates a file name from the extension accounting for the case + where the file name itself contains periods. + + """ + path = Path(file_path) + name = path.stem + extension = path.suffix + return name, extension + + def control_warnings(): """ Convenience function to turn warnings on/off diff --git a/pelicun/tests/test_base.py b/pelicun/tests/test_base.py index de38e84d6..31c96d3f0 100644 --- a/pelicun/tests/test_base.py +++ b/pelicun/tests/test_base.py @@ -206,6 +206,18 @@ def test_logger_div(): os.remove('log.txt') +def test_split_file_name(): + file_path = "example.file.name.txt" + name, extension = base.split_file_name(file_path) + assert name == 'example.file.name' + assert extension == '.txt' + + file_path = "example" + name, extension = base.split_file_name(file_path) + assert name == 'example' + assert extension == '' + + def test_print_system_info(): # create a logger object log_config = {