introduced logger

This commit is contained in:
Robin Müller 2021-07-31 20:49:38 +02:00
parent 2043b92cd4
commit 8e805d2408
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
6 changed files with 87 additions and 21 deletions

50
core.py Normal file
View File

@ -0,0 +1,50 @@
import enum
import colorlog
import logging
import argparse
CONSOLE_LOGGER_NAME = 'FSFW Generator Logger'
LOGGER_INSTANCE = 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():
handler = colorlog.StreamHandler()
handler.setFormatter(colorlog.ColoredFormatter(
'%(log_color)s%(levelname)s: %(message)s'))
logger = colorlog.getLogger(CONSOLE_LOGGER_NAME)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
return logger
class ParserTypes(enum.Enum):
EVENTS = 'events',
OBJECTS = 'objects',
RETVALS = 'returnvalues',
SUBSERVICES = 'subservices'
def init_printout():
global LOGGER_INSTANCE
LOGGER_INSTANCE = get_console_logger()
print('-- FSFW 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

View File

@ -2,6 +2,9 @@ import re
from typing import List
from fsfwgen.parserbase.parser import FileParser
from fsfwgen.core import get_console_logger
LOGGER = get_console_logger()
EVENT_ENTRY_NAME_IDX = 0
EVENT_ENTRY_SEVERITY_IDX = 1
@ -280,7 +283,6 @@ def handle_csv_export(file_name: str, event_list: list, file_separator: str):
Generates the CSV in the same directory as the .py file and copes the CSV to another
directory if specified.
"""
print("EventParser: Exporting to file: " + file_name)
export_to_file(filename=file_name, event_list=event_list, file_separator=file_separator)
@ -288,7 +290,6 @@ def handle_cpp_export(
event_list: list, date_string: str, file_name: str = "translateEvents.cpp",
generate_header: bool = True, header_file_name: str = "translateEvents.h"
):
print("EventParser: Generating translation cpp file.")
write_translation_source_file(event_list=event_list, date_string=date_string, filename=file_name)
if generate_header:
write_translation_header_file(filename=header_file_name)

View File

@ -1,9 +1,13 @@
import re
from fsfwgen.parserbase.parser import FileParser
from fsfwgen.core import get_console_logger
from fsfwgen.utility.sql_writer import SqlWriter
LOGGER = get_console_logger()
class ObjectDefinitionParser(FileParser):
def __init__(self, file_list: list):
@ -35,7 +39,7 @@ def export_object_file(filename, object_list, file_separator: str = ","):
def write_translation_file(filename: str, list_of_entries, date_string_full: str):
outputfile = open(filename, "w")
print('ObjectParser: Writing translation file ' + filename)
LOGGER.info('ObjectParser: Writing translation file ' + filename)
definitions = ""
function = "const char* translateObject(object_id_t object) " \
"{\n\tswitch( (object & 0xFFFFFFFF) ) {\n"

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
colorlog>=5.0.0

View File

@ -1,17 +1,23 @@
# -*- coding: utf-8 -*-
import shutil
import os
from fsfwgen.core import get_console_logger
LOGGER = get_console_logger()
def copy_file(filename: str, destination: str = "", delete_existing_file: bool = False):
if os.path.exists(filename):
if not os.path.exists(filename):
LOGGER.warning(f'File {filename} does not exist')
return
if not os.path.exists(destination):
LOGGER.warning(f'Destination directory {destination} does not exist')
return
try:
shutil.copy2(filename, destination)
except FileNotFoundError as error:
print("copy_file: File not found!")
print(error)
except FileNotFoundError:
LOGGER.exception('File not found!')
except shutil.SameFileError:
print("copy_file: Source and destination are the same!")
LOGGER.exception('Source and destination are the same!')
def move_file(file_name: str, destination: str = ""):
@ -25,9 +31,7 @@ def move_file(file_name: str, destination: str = ""):
shutil.copy2(file_name, destination)
os.remove(file_name)
return
except FileNotFoundError as error:
print(error)
print('move_file: File not found')
except shutil.SameFileError as error:
print(error)
print('move_file: Files are the same!')
except FileNotFoundError:
LOGGER.exception('File not found!')
except shutil.SameFileError:
LOGGER.exception('Source and destination are the same!')

View File

@ -1,5 +1,9 @@
import sqlite3
from fsfwgen.core import get_console_logger
LOGGER = get_console_logger()
class SqlWriter:
def __init__(self, db_filename: str):
@ -7,11 +11,11 @@ class SqlWriter:
self.conn = sqlite3.connect(self.filename)
def open(self, sql_creation_command: str):
print("SQL Writer: Opening " + self.filename)
LOGGER.info(f'SQL Writer: Opening {self.filename}')
self.conn.execute(sql_creation_command)
def delete(self, sql_deletion_command):
print("SQL Writer: Deleting SQL table")
LOGGER.info("SQL Writer: Deleting SQL table")
self.conn.execute(sql_deletion_command)
def write_entries(self, sql_insertion_command, current_entry):
@ -20,13 +24,15 @@ class SqlWriter:
return cur.lastrowid
def commit(self):
print("SQL Writer: Commiting SQL table")
LOGGER.info("SQL Writer: Commiting SQL table")
self.conn.commit()
def close(self):
self.conn.close()
def sql_writing_helper(self, creation_cmd, insertion_cmd, mib_table: dict, deletion_cmd: str=""):
def sql_writing_helper(
self, creation_cmd, insertion_cmd, mib_table: dict, deletion_cmd: str = ""
):
if deletion_cmd != "":
self.delete(deletion_cmd)
self.open(creation_cmd)