init repository
This commit is contained in:
0
utility/__init__.py
Normal file
0
utility/__init__.py
Normal file
61
utility/csv_writer.py
Normal file
61
utility/csv_writer.py
Normal file
@ -0,0 +1,61 @@
|
||||
#! /usr/bin/python3
|
||||
"""
|
||||
@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 modgen.utility.file_management import copy_file, move_file
|
||||
|
||||
|
||||
# TODO: Export to SQL
|
||||
class CsvWriter:
|
||||
def __init__(
|
||||
self, filename: str, table_to_print=None, header_array=None, file_separator: str = ","
|
||||
):
|
||||
if header_array is None:
|
||||
header_array = []
|
||||
if table_to_print is None:
|
||||
table_to_print = dict()
|
||||
self.filename = filename
|
||||
self.table_to_print = table_to_print
|
||||
self.header_array = header_array
|
||||
if self.header_array != 0:
|
||||
self.column_numbers = len(self.header_array)
|
||||
self.file_separator = file_separator
|
||||
|
||||
def write_to_csv(self):
|
||||
file = open(self.filename, "w")
|
||||
file.write("Index" + self.file_separator)
|
||||
for index in range(self.column_numbers):
|
||||
# noinspection PyTypeChecker
|
||||
if index < len(self.header_array)-1:
|
||||
file.write(self.header_array[index] + self.file_separator)
|
||||
else:
|
||||
file.write(self.header_array[index] + "\n")
|
||||
for index, entry in self.table_to_print.items():
|
||||
file.write(str(index) + self.file_separator)
|
||||
for columnIndex in range(self.column_numbers):
|
||||
# noinspection PyTypeChecker
|
||||
if columnIndex < len(self.header_array) - 1:
|
||||
file.write(str(entry[columnIndex]) + self.file_separator)
|
||||
else:
|
||||
file.write(str(entry[columnIndex]) + "\n")
|
||||
file.close()
|
||||
|
||||
def copy_csv(self, copy_destination: str = "."):
|
||||
copy_file(self.filename, copy_destination)
|
||||
print("CSV file was copied to " + copy_destination)
|
||||
|
||||
def move_csv(self, move_destination: str):
|
||||
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(f"CSV Writer: CSV file was moved to {move_destination}")
|
25
utility/file_management.py
Normal file
25
utility/file_management.py
Normal file
@ -0,0 +1,25 @@
|
||||
#! /usr/bin/python3.8
|
||||
# -*- coding: utf-8 -*-
|
||||
import shutil
|
||||
import os
|
||||
|
||||
|
||||
def copy_file(filename: str, destination: str = "", delete_existing_file: bool = False):
|
||||
if os.path.exists(filename):
|
||||
try:
|
||||
shutil.copy2(filename, destination)
|
||||
except FileNotFoundError as error:
|
||||
print("copy_file: File not found!")
|
||||
print(error)
|
||||
except shutil.SameFileError:
|
||||
print("copy_file: Source and destination are the same!")
|
||||
|
||||
|
||||
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)
|
16
utility/printer.py
Normal file
16
utility/printer.py
Normal file
@ -0,0 +1,16 @@
|
||||
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="")
|
||||
|
36
utility/sql_writer.py
Normal file
36
utility/sql_writer.py
Normal file
@ -0,0 +1,36 @@
|
||||
import sqlite3
|
||||
|
||||
|
||||
class SqlWriter:
|
||||
def __init__(self, db_filename: str):
|
||||
self.filename = db_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