fsfwgen/core.py
2022-03-04 10:24:08 +01:00

94 lines
2.7 KiB
Python

import enum
import sys
import colorlog
import logging
import argparse
CONSOLE_LOGGER_NAME = "FSFW Generator Logger"
LOGGER_INSTANCE = None
class InfoFilter(logging.Filter):
"""Filter object, which is used so that only INFO and DEBUG messages are printed to stdout."""
def filter(self, rec):
if rec.levelno == logging.INFO:
return rec.levelno
return None
class DebugFilter(logging.Filter):
"""Filter object, which is used so that only DEBUG messages are printed to stdout."""
def filter(self, rec):
if rec.levelno == logging.DEBUG:
return rec.levelno
return None
def get_console_logger():
global LOGGER_INSTANCE
if LOGGER_INSTANCE is None:
LOGGER_INSTANCE = init_console_logger()
return LOGGER_INSTANCE
def init_console_logger():
logger = colorlog.getLogger(CONSOLE_LOGGER_NAME)
generic_format = colorlog.ColoredFormatter(
"%(log_color)s%(levelname)-8s | %(reset)s%(message)s%(reset)s"
)
fault_format = colorlog.ColoredFormatter(
fmt="%(log_color)s%(levelname)-8s %(cyan)s%(asctime)s.%(msecs)03d "
"[%(filename)s:%(lineno)d] %(reset)s%(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
console_info_handler = colorlog.StreamHandler(stream=sys.stdout)
console_info_handler.setLevel(logging.INFO)
console_info_handler.addFilter(InfoFilter())
console_debug_handler = logging.StreamHandler(stream=sys.stdout)
console_debug_handler.setLevel(logging.DEBUG)
console_debug_handler.addFilter(DebugFilter())
console_info_handler.setFormatter(generic_format)
console_info_handler.addFilter(InfoFilter())
console_debug_handler.addFilter(DebugFilter())
console_error_handler = logging.StreamHandler(stream=sys.stderr)
console_error_handler.setLevel(logging.WARNING)
console_error_handler.setFormatter(fault_format)
logger.addHandler(console_info_handler)
logger.addHandler(console_debug_handler)
logger.addHandler(console_error_handler)
logger.setLevel(logging.DEBUG)
return logger
class ParserTypes(enum.Enum):
EVENTS = "events"
OBJECTS = "objects"
RETVALS = "returnvalues"
SUBSERVICES = "subservices"
def init_printout(project_string: str):
global LOGGER_INSTANCE
LOGGER_INSTANCE = get_console_logger()
print(f"-- {project_string} MIB Generator --")
def return_generic_args_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser("Arguments for FSFW MIB generation")
choices = ("events", "objects", "returnvalues", "subservices")
parser.add_argument(
"type",
metavar="type",
choices=choices,
help=f"Type of MIB data to generate. Choices: {choices}",
)
return parser