fsfwgen/core.py

84 lines
2.3 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"
)
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())
logger.addHandler(console_info_handler)
logger.addHandler(console_debug_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} MOD Generator --")
def return_generic_args_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser("Arguments for FSFW MOD generation")
choices = ("events", "objects", "returnvalues", "subservices")
parser.add_argument(
"type",
metavar="type",
choices=choices,
help=f"Type of MOD data to generate. Choices: {choices}",
)
return parser