add changelog and removing logging module

This commit is contained in:
Robin Müller 2023-02-09 15:02:52 +01:00
parent b1e5a2d40a
commit 66e31885a7
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814
10 changed files with 47 additions and 136 deletions

11
CHANGELOG.md Normal file
View File

@ -0,0 +1,11 @@
Change Log
=======
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
# [unreleased]
- Remove `logging` module and move to more pythonic logging usage.

View File

@ -1,5 +1,5 @@
__version__ = "0.2.0"
VERSION_MAJOR = 0 VERSION_MAJOR = 0
VERSION_MINOR = 1 VERSION_MINOR = 1
VERSION_REVISION = 0 VERSION_REVISION = 0
__version__ = "0.1.0"

View File

@ -4,9 +4,8 @@ from pathlib import Path
from typing import List, Optional, Dict from typing import List, Optional, Dict
from fsfwgen.parserbase.parser import FileParser from fsfwgen.parserbase.parser import FileParser
from fsfwgen.logging import get_console_logger
LOGGER = get_console_logger() _LOGGER = logging.getLogger(__name__)
FSFW_EVENT_HEADER_INCLUDE = '#include "fsfw/events/Event.h"' FSFW_EVENT_HEADER_INCLUDE = '#include "fsfw/events/Event.h"'
@ -167,8 +166,8 @@ class EventParser(FileParser):
if self.obsw_root_path is not None: if self.obsw_root_path is not None:
file_name = file_name.relative_to(self.obsw_root_path) file_name = file_name.relative_to(self.obsw_root_path)
if self.mib_table.get(full_id) is not None: if self.mib_table.get(full_id) is not None:
LOGGER.warning(f"Duplicate event ID {full_id} detected") _LOGGER.warning(f"Duplicate event ID {full_id} detected")
LOGGER.info( _LOGGER.info(
f"Name: {self.mib_table.get(full_id).name}| " f"Name: {self.mib_table.get(full_id).name}| "
f"Description: {self.mib_table.get(full_id).description}" f"Description: {self.mib_table.get(full_id).description}"
) )

View File

@ -1,97 +0,0 @@
import logging
import sys
from typing import Optional
from colorlog import ColoredFormatter
FSFWGEN_LOGGER_NAME = "fsfwgen"
__CONSOLE_LOGGER_SET_UP: Optional[logging.Logger] = None
def get_console_logger() -> logging.Logger:
global __CONSOLE_LOGGER_SET_UP
"""Get the global console logger instance. Error logs will still be saved to an error file
"""
logger = logging.getLogger(FSFWGEN_LOGGER_NAME)
if not __CONSOLE_LOGGER_SET_UP:
__CONSOLE_LOGGER_SET_UP = True
__setup_tmtc_console_logger()
return logger
def init_console_logger(log_level: int = logging.INFO) -> logging.Logger:
global __CONSOLE_LOGGER_SET_UP
if not __CONSOLE_LOGGER_SET_UP:
__CONSOLE_LOGGER_SET_UP = True
return __setup_tmtc_console_logger(log_level=log_level)
return get_console_logger()
def __setup_tmtc_console_logger(log_level: int = logging.INFO) -> logging.Logger:
"""Sets the LOGGER object which will be used globally. This needs to be called before
using the logger.
:return: Returns the instance of the global logger
"""
logger = logging.getLogger(FSFWGEN_LOGGER_NAME)
# Use colorlog for now because it allows more flexibility and custom messages
# for different levels
set_up_colorlog_logger(logger=logger)
logger.setLevel(level=log_level)
# set_up_coloredlogs_logger(logger=logger)
return logger
# Custom formatter. Allows different strings for info, error and debug output
class CustomTmtccmdFormatter(ColoredFormatter):
def __init__(
self, info_fmt: str, dbg_fmt: str, err_fmt: str, warn_fmt: str, datefmt=None
):
self.err_fmt = err_fmt
self.info_fmt = info_fmt
self.dbg_fmt = dbg_fmt
self.warn_fmt = warn_fmt
super().__init__(fmt="%(levelno)d: %(msg)s", datefmt=datefmt, style="%")
def format(self, record):
# Save the original format configured by the user
# when the logger formatter was instantiated
format_orig = self._style._fmt
# Replace the original format with one customized by logging level
if record.levelno == logging.DEBUG:
self._style._fmt = self.dbg_fmt
elif record.levelno == logging.INFO:
self._style._fmt = self.info_fmt
elif record.levelno == logging.ERROR:
self._style._fmt = self.err_fmt
elif record.levelno == logging.WARNING:
self._style._fmt = self.warn_fmt
# Call the original formatter class to do the grunt work
result = logging.Formatter.format(self, record)
# Restore the original format configured by the user
self._style._fmt = format_orig
return result
def set_up_colorlog_logger(logger: logging.Logger):
from colorlog import StreamHandler
dbg_fmt = "%(log_color)s%(levelname)-8s %(cyan)s [%(filename)s:%(lineno)d] %(reset)s%(message)s"
custom_formatter = CustomTmtccmdFormatter(
info_fmt="%(log_color)s%(levelname)-8s %(cyan)s %(reset)s%(message)s",
dbg_fmt=dbg_fmt,
err_fmt=dbg_fmt,
warn_fmt=dbg_fmt,
)
console_handler = StreamHandler(stream=sys.stdout)
console_handler.setFormatter(custom_formatter)
logger.addHandler(console_handler)
logger.propagate = False

View File

@ -1,13 +1,13 @@
import logging
import re import re
from pathlib import Path from pathlib import Path
from typing import List from typing import List
from fsfwgen.parserbase.parser import FileParser from fsfwgen.parserbase.parser import FileParser
from fsfwgen.logging import get_console_logger
from fsfwgen.utility.sql_writer import SqlWriter from fsfwgen.utility.sql_writer import SqlWriter
LOGGER = get_console_logger() _LOGGER = logging.getLogger(__name__)
class ObjectDefinitionParser(FileParser): class ObjectDefinitionParser(FileParser):
@ -45,7 +45,7 @@ def export_object_file(filename, object_list, file_separator: str = ","):
def write_translation_file(filename: str, list_of_entries, date_string_full: str): def write_translation_file(filename: str, list_of_entries, date_string_full: str):
with open(filename, "w") as out: with open(filename, "w") as out:
LOGGER.info("ObjectParser: Writing translation file " + filename) _LOGGER.info("ObjectParser: Writing translation file " + filename)
definitions = "" definitions = ""
function = ( function = (
"const char *translateObject(object_id_t object) " "const char *translateObject(object_id_t object) "

View File

@ -1,13 +1,12 @@
"""Generic File Parser class """Generic File Parser class
Used by parse header files. Implemented as class in case header parser becomes more complex Used by parse header files. Implemented as class in case header parser becomes more complex
""" """
import logging
from pathlib import Path from pathlib import Path
from typing import Union, List from typing import Union, List
from fsfwgen.logging import get_console_logger
from logging import DEBUG
LOGGER = get_console_logger() _LOGGER = logging.getLogger(__name__)
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
@ -24,7 +23,7 @@ class FileListParser:
elif isinstance(directory_list_or_name, List): elif isinstance(directory_list_or_name, List):
self.directory_list.extend(directory_list_or_name) self.directory_list.extend(directory_list_or_name)
else: else:
LOGGER.warning( _LOGGER.warning(
"Header Parser: Passed directory list is not a header name or list of header names" "Header Parser: Passed directory list is not a header name or list of header names"
) )
self.header_files = [] self.header_files = []

View File

@ -16,7 +16,7 @@ import re
from abc import abstractmethod from abc import abstractmethod
from pathlib import Path from pathlib import Path
from typing import Dict, List from typing import Dict, List
from enum import Enum, auto from enum import Enum
class VerbosityLevels(enum.Enum): class VerbosityLevels(enum.Enum):

View File

@ -1,14 +1,14 @@
import logging
import re import re
import sys import sys
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from typing import List, Tuple, Optional, Dict, Union from typing import List, Optional, Dict
from fsfwgen.parserbase.parser import FileParser, VerbosityLevels from fsfwgen.parserbase.parser import FileParser, VerbosityLevels
from fsfwgen.utility.printer import PrettyPrinter from fsfwgen.utility.printer import PrettyPrinter
from fsfwgen.logging import get_console_logger
LOGGER = get_console_logger() _LOGGER = logging.getLogger(__name__)
# Intermediate solution # Intermediate solution
MAX_STRING_LEN = 80 MAX_STRING_LEN = 80
@ -342,10 +342,10 @@ class ReturnValueParser(FileParser):
if returnvalue_match: if returnvalue_match:
description = self.__search_for_descrip_string(moving_window=moving_window) description = self.__search_for_descrip_string(moving_window=moving_window)
if number_match == INVALID_IF_ID: if number_match == INVALID_IF_ID:
LOGGER.warning(f"Invalid number match detected for file {file_name}") _LOGGER.warning(f"Invalid number match detected for file {file_name}")
LOGGER.warning(f"Match groups:") _LOGGER.warning(f"Match groups:")
for group in returnvalue_match.groups(): for group in returnvalue_match.groups():
LOGGER.info(group) _LOGGER.info(group)
self.__handle_returnvalue_match( self.__handle_returnvalue_match(
name_match=returnvalue_match.group(1), name_match=returnvalue_match.group(1),
file_name=file_name, file_name=file_name,
@ -398,14 +398,14 @@ class ReturnValueParser(FileParser):
Returns whether the interface ID was found successfully in the IF ID header files Returns whether the interface ID was found successfully in the IF ID header files
""" """
if self.get_verbosity() == VerbosityLevels.DEBUG: if self.get_verbosity() == VerbosityLevels.DEBUG:
LOGGER.info( _LOGGER.info(
f"Interface ID {interface_id_match.group(1)} found in {file_name}" f"Interface ID {interface_id_match.group(1)} found in {file_name}"
) )
if_id_entry = self.interfaces.get(interface_id_match.group(1)) if_id_entry = self.interfaces.get(interface_id_match.group(1))
if if_id_entry is not None: if if_id_entry is not None:
self.current_interface_id_entries["ID"] = if_id_entry[0] self.current_interface_id_entries["ID"] = if_id_entry[0]
else: else:
LOGGER.warning( _LOGGER.warning(
f"Interface ID {interface_id_match.group(1)} not found in IF ID dictionary" f"Interface ID {interface_id_match.group(1)} not found in IF ID dictionary"
) )
return False return False
@ -415,7 +415,7 @@ class ReturnValueParser(FileParser):
self.current_interface_id_entries["FullName"] = interface_id_match.group(1) self.current_interface_id_entries["FullName"] = interface_id_match.group(1)
if self.get_verbosity() == VerbosityLevels.DEBUG: if self.get_verbosity() == VerbosityLevels.DEBUG:
current_id = self.current_interface_id_entries["ID"] current_id = self.current_interface_id_entries["ID"]
LOGGER.info(f"Current ID: {current_id}") _LOGGER.info(f"Current ID: {current_id}")
return True return True
def __handle_returnvalue_match( def __handle_returnvalue_match(
@ -479,7 +479,7 @@ class ReturnValueParser(FileParser):
my_str = first_part + "_" + self.convert(second_part) my_str = first_part + "_" + self.convert(second_part)
if len(my_str) > max_string_len: if len(my_str) > max_string_len:
if print_truncated_entries: if print_truncated_entries:
LOGGER.warning(f"Entry {my_str} too long. Will truncate.") _LOGGER.warning(f"Entry {my_str} too long. Will truncate.")
my_str = my_str[0:max_string_len] my_str = my_str[0:max_string_len]
else: else:
# print("Entry: " + myStr + " is all right.") # print("Entry: " + myStr + " is all right.")
@ -509,5 +509,5 @@ def get_number_from_dec_or_hex_str(a_string):
return int(a_string, 16) return int(a_string, 16)
if a_string.isdigit(): if a_string.isdigit():
return int(a_string) return int(a_string)
LOGGER.warning(f"Illegal number representation: {a_string}") _LOGGER.warning(f"Illegal number representation: {a_string}")
return 0 return 0

View File

@ -1,31 +1,31 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging
import shutil import shutil
import os import os
from pathlib import Path from pathlib import Path
from fsfwgen.logging import get_console_logger
LOGGER = get_console_logger() _LOGGER = logging.getLogger(__name__)
def copy_file( def copy_file(
filename: Path, destination: Path = "", delete_existing_file: bool = False filename: Path, destination: Path = "", delete_existing_file: bool = False
): ):
if not os.path.exists(filename): if not os.path.exists(filename):
LOGGER.warning(f"File {filename} does not exist") _LOGGER.warning(f"File {filename} does not exist")
return return
if not os.path.isdir(destination) and os.path.exists(destination): if not os.path.isdir(destination) and os.path.exists(destination):
if delete_existing_file: if delete_existing_file:
os.remove(destination) os.remove(destination)
else: else:
LOGGER.warning(f"Destination file {destination} already exists") _LOGGER.warning(f"Destination file {destination} already exists")
return return
try: try:
shutil.copy2(src=filename, dst=destination) shutil.copy2(src=filename, dst=destination)
except FileNotFoundError: except FileNotFoundError:
LOGGER.exception("File not found!") _LOGGER.exception("File not found!")
except shutil.SameFileError: except shutil.SameFileError:
LOGGER.exception("Source and destination are the same!") _LOGGER.exception("Source and destination are the same!")
def move_file(file_name: Path, destination: Path = ""): def move_file(file_name: Path, destination: Path = ""):
@ -40,6 +40,6 @@ def move_file(file_name: Path, destination: Path = ""):
os.remove(file_name) os.remove(file_name)
return return
except FileNotFoundError: except FileNotFoundError:
LOGGER.exception("File not found!") _LOGGER.exception("File not found!")
except shutil.SameFileError: except shutil.SameFileError:
LOGGER.exception("Source and destination are the same!") _LOGGER.exception("Source and destination are the same!")

View File

@ -1,8 +1,7 @@
import logging
import sqlite3 import sqlite3
from fsfwgen.logging import get_console_logger _LOGGER = logging.getLogger(__name__)
LOGGER = get_console_logger()
class SqlWriter: class SqlWriter:
@ -11,11 +10,11 @@ class SqlWriter:
self.conn = sqlite3.connect(self.filename) self.conn = sqlite3.connect(self.filename)
def open(self, sql_creation_command: str): def open(self, sql_creation_command: str):
LOGGER.info(f"SQL Writer: Opening {self.filename}") _LOGGER.info(f"SQL Writer: Opening {self.filename}")
self.conn.execute(sql_creation_command) self.conn.execute(sql_creation_command)
def delete(self, sql_deletion_command): def delete(self, sql_deletion_command):
LOGGER.info("SQL Writer: Deleting SQL table") _LOGGER.info("SQL Writer: Deleting SQL table")
self.conn.execute(sql_deletion_command) self.conn.execute(sql_deletion_command)
def write_entries(self, sql_insertion_command, current_entry): def write_entries(self, sql_insertion_command, current_entry):
@ -24,7 +23,7 @@ class SqlWriter:
return cur.lastrowid return cur.lastrowid
def commit(self): def commit(self):
LOGGER.info("SQL Writer: Commiting SQL table") _LOGGER.info("SQL Writer: Commiting SQL table")
self.conn.commit() self.conn.commit()
def close(self): def close(self):