Init commit
This commit is contained in:
0
generators/utility/__init__.py
Normal file
0
generators/utility/__init__.py
Normal file
62
generators/utility/mib_csv_writer.py
Normal file
62
generators/utility/mib_csv_writer.py
Normal file
@ -0,0 +1,62 @@
|
||||
#! /usr/bin/python3.7
|
||||
"""
|
||||
@file
|
||||
mib_packet_content_parser.py
|
||||
@brief
|
||||
CSV Writer
|
||||
@details
|
||||
This class writes tables to a csv.
|
||||
@author
|
||||
R. Mueller
|
||||
@date
|
||||
14.11.2019
|
||||
"""
|
||||
from utility import mib_globals as g
|
||||
from utility.mib_file_management import copy_file, move_file
|
||||
|
||||
|
||||
# TODO: Export to SQL
|
||||
class CsvWriter:
|
||||
def __init__(self, filename, table_to_print=None, header_array=None):
|
||||
if header_array is None:
|
||||
header_array = []
|
||||
if table_to_print is None:
|
||||
table_to_print = dict()
|
||||
self.filename = filename
|
||||
self.tableToPrint = table_to_print
|
||||
self.headerArray = header_array
|
||||
if self.headerArray != 0:
|
||||
self.columnNumbers = len(self.headerArray)
|
||||
self.fileSeparator = g.fileSeparator
|
||||
|
||||
def write_to_csv(self):
|
||||
file = open(self.filename, "w")
|
||||
file.write("Index" + self.fileSeparator)
|
||||
for index in range(self.columnNumbers):
|
||||
# noinspection PyTypeChecker
|
||||
if index < len(self.headerArray)-1:
|
||||
file.write(self.headerArray[index] + self.fileSeparator)
|
||||
else:
|
||||
file.write(self.headerArray[index] + "\n")
|
||||
for index, entry in self.tableToPrint.items():
|
||||
file.write(str(index) + self.fileSeparator)
|
||||
for columnIndex in range(self.columnNumbers):
|
||||
# noinspection PyTypeChecker
|
||||
if columnIndex < len(self.headerArray) - 1:
|
||||
file.write(str(entry[columnIndex]) + self.fileSeparator)
|
||||
else:
|
||||
file.write(str(entry[columnIndex]) + "\n")
|
||||
file.close()
|
||||
|
||||
def copy_csv(self, copy_destination: str = g.copyDestination):
|
||||
copy_file(self.filename, copy_destination)
|
||||
print("CSV file was copied to " + copy_destination)
|
||||
|
||||
def move_csv(self, move_destination):
|
||||
move_file(self.filename, move_destination)
|
||||
if move_destination == ".." or move_destination == "../":
|
||||
print("CSV Writer: CSV file was moved to parser root directory")
|
||||
else:
|
||||
print("CSV Writer: CSV file was moved to " + move_destination)
|
||||
|
||||
|
22
generators/utility/mib_file_management.py
Normal file
22
generators/utility/mib_file_management.py
Normal file
@ -0,0 +1,22 @@
|
||||
#! /usr/bin/python3.8
|
||||
# -*- coding: utf-8 -*-
|
||||
import shutil
|
||||
import os
|
||||
|
||||
def copy_file(filename:str, destination: str= ""):
|
||||
if os.path.exists(filename):
|
||||
try:
|
||||
shutil.copy2(filename, destination)
|
||||
except FileNotFoundError as error:
|
||||
print("File not found!")
|
||||
print(error)
|
||||
|
||||
|
||||
def move_file(file_name: str, destination: str= ""):
|
||||
if os.path.exists(file_name):
|
||||
try:
|
||||
shutil.copy2(file_name, destination)
|
||||
os.remove(file_name)
|
||||
except FileNotFoundError as error:
|
||||
print("File not found!")
|
||||
print(error)
|
18
generators/utility/mib_globals.py
Normal file
18
generators/utility/mib_globals.py
Normal file
@ -0,0 +1,18 @@
|
||||
"""
|
||||
@file
|
||||
mib_globals.py
|
||||
@date
|
||||
16.11.2019
|
||||
@brief
|
||||
Global settings for MIB exporter
|
||||
"""
|
||||
import pprint
|
||||
|
||||
|
||||
doExportMiB = True
|
||||
executeSQLcommands = False
|
||||
printToConsole = True
|
||||
exportToCSV = True
|
||||
doCopyFile = False
|
||||
copyDestination = "."
|
||||
fileSeparator = ';'
|
15
generators/utility/mib_printer.py
Normal file
15
generators/utility/mib_printer.py
Normal file
@ -0,0 +1,15 @@
|
||||
import pprint
|
||||
|
||||
PrettyPrinter = pprint.PrettyPrinter(indent=0, width=250)
|
||||
|
||||
class Printer:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def print_content(dictionary, leading_string: str = ""):
|
||||
if leading_string != "":
|
||||
print(leading_string)
|
||||
PrettyPrinter.pprint(dictionary)
|
||||
print("\r\n", end="")
|
||||
|
37
generators/utility/mib_sql_writer.py
Normal file
37
generators/utility/mib_sql_writer.py
Normal file
@ -0,0 +1,37 @@
|
||||
import sqlite3
|
||||
SQL_DATABASE_NAME = "obsw_mib.db"
|
||||
|
||||
|
||||
class SqlWriter:
|
||||
def __init__(self, filename: str = SQL_DATABASE_NAME):
|
||||
self.filename = filename
|
||||
self.conn = sqlite3.connect(self.filename)
|
||||
|
||||
def open(self, sql_creation_command: str):
|
||||
print("SQL Writer: Opening " + self.filename)
|
||||
self.conn.execute(sql_creation_command)
|
||||
|
||||
def delete(self, sql_deletion_command):
|
||||
print("SQL Writer: Deleting SQL table")
|
||||
self.conn.execute(sql_deletion_command)
|
||||
|
||||
def write_entries(self, sql_insertion_command, current_entry):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(sql_insertion_command, current_entry)
|
||||
return cur.lastrowid
|
||||
|
||||
def commit(self):
|
||||
print("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=""):
|
||||
if deletion_cmd != "":
|
||||
self.delete(deletion_cmd)
|
||||
self.open(creation_cmd)
|
||||
for i in mib_table:
|
||||
self.write_entries(insertion_cmd, mib_table[i])
|
||||
self.commit()
|
||||
self.close()
|
Reference in New Issue
Block a user