Added some more error handling, reworked settings.json and file selection

master
Griffiths Lott 3 years ago
parent ef577a3203
commit 5742adef36
  1. 19
      ILE_MainWindow.py
  2. 23
      ILExtract.py
  3. 40
      main.py
  4. 2
      settings.json

@ -127,6 +127,7 @@ class Ui_MainWindow(object):
self.gridLayout.addWidget(self.inputFilePreview, 3, 0, 1, 1) self.gridLayout.addWidget(self.inputFilePreview, 3, 0, 1, 1)
self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1) self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget) MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 889, 24)) self.menubar.setGeometry(QtCore.QRect(0, 0, 889, 24))
self.menubar.setObjectName("menubar") self.menubar.setObjectName("menubar")
@ -136,20 +137,16 @@ class Ui_MainWindow(object):
self.menu_Settings.setIcon(icon5) self.menu_Settings.setIcon(icon5)
self.menu_Settings.setObjectName("menu_Settings") self.menu_Settings.setObjectName("menu_Settings")
self.menuStart_Maximized = QtWidgets.QMenu(self.menu_Settings) self.menuStart_Maximized = QtWidgets.QMenu(self.menu_Settings)
icon6 = QtGui.QIcon()
icon6.addPixmap(QtGui.QPixmap("assets/maximize.svg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.menuStart_Maximized.setIcon(icon6)
self.menuStart_Maximized.setObjectName("menuStart_Maximized")
MainWindow.setMenuBar(self.menubar) MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar") self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar) MainWindow.setStatusBar(self.statusbar)
self.action_Default_Locations = QtWidgets.QAction(MainWindow) # self.action_Debug_Mode = QtWidgets.QAction(MainWindow)
icon7 = QtGui.QIcon() # icon7 = QtGui.QIcon()
icon7.addPixmap(QtGui.QPixmap("assets/fileSearch.svg"), QtGui.QIcon.Normal, QtGui.QIcon.Off) # icon7.addPixmap(QtGui.QPixmap("assets/fileSearch.svg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.action_Default_Locations.setIcon(icon7) # self.action_Debug_Mode.setIcon(icon7)
self.action_Default_Locations.setObjectName("action_Default_Locations") # self.action_Debug_Mode.setObjectName("action_Debug_Mode")
self.menu_Settings.addAction(self.action_Default_Locations) #self.menu_Settings.addAction(self.action_Debug_Mode)
self.menubar.addAction(self.menu_Settings.menuAction()) self.menubar.addAction(self.menu_Settings.menuAction())
self.reportTypeL.setBuddy(self.reportTypeCB) self.reportTypeL.setBuddy(self.reportTypeCB)
@ -190,5 +187,5 @@ class Ui_MainWindow(object):
self.openFolderButton.setText(_translate("MainWindow", "&Open Folder")) self.openFolderButton.setText(_translate("MainWindow", "&Open Folder"))
self.openExcelButton.setText(_translate("MainWindow", "&Open File")) self.openExcelButton.setText(_translate("MainWindow", "&Open File"))
self.menu_Settings.setTitle(_translate("MainWindow", "&Settings")) self.menu_Settings.setTitle(_translate("MainWindow", "&Settings"))
self.action_Default_Locations.setText(_translate("MainWindow", "&Default Locations")) #self.action_Debug_Mode.setText(_translate("MainWindow", "&Debug Mode"))

@ -6,7 +6,7 @@ import re
from pathlib import Path from pathlib import Path
import numpy as np import numpy as np
from glob import glob from glob import glob
from logging import debug, DEBUG, basicConfig, warn from logging import debug, DEBUG, basicConfig, warn, error
# V3.1 | 01/19/23 # V3.1 | 01/19/23
@ -53,7 +53,7 @@ class ILReport:
dataframe: DataFrame = self.x_method(report, self.output_location) dataframe: DataFrame = self.x_method(report, self.output_location)
if dataframe.empty: if dataframe.empty:
warn(f"ILReport: resulting dataframe was empty! Exiting with None.") warn(f"ILReport: resulting dataframe was empty! Exiting with None.")
return None return dataframe
self._append_to_consolidated_report(dataframe, settings["consolidatedBasePath"]) self._append_to_consolidated_report(dataframe, settings["consolidatedBasePath"])
return dataframe return dataframe
@ -91,9 +91,13 @@ class ILReport:
debug(f"Consolidated Report | No monthly summary file!\n\tCreating: {save_path}") debug(f"Consolidated Report | No monthly summary file!\n\tCreating: {save_path}")
# No file exists yet # No file exists yet
# Create it and add the current month # Create it and add the current month
with pd.ExcelWriter(save_path) as writer: try:
debug(f"Consolidated Report | {sheet_name}: Saving data as: {report_name}") with pd.ExcelWriter(save_path) as writer:
dataframe_to_append.to_excel(writer, index=False, sheet_name=sheet_name) debug(f"Consolidated Report | {sheet_name}: Saving data as: {report_name}")
dataframe_to_append.to_excel(writer, index=False, sheet_name=sheet_name)
except Exception as e:
error(f"[E] Failed to create consolidated report! {sheet_name}:\n{e}")
else: else:
# We need to read the dataframe in the current monthly report # We need to read the dataframe in the current monthly report
# Check that we are not adding matching data # Check that we are not adding matching data
@ -106,9 +110,12 @@ class ILReport:
debug(f"Consolidated Report | Data is same as previous! Skipping!") debug(f"Consolidated Report | Data is same as previous! Skipping!")
return None return None
# We need to find the start cols (where the new data should go) # We need to find the start cols (where the new data should go)
with pd.ExcelWriter(save_path, engine='openpyxl', mode='a',if_sheet_exists="overlay") as writer: try:
debug(f"Consolidated Report | {sheet_name}: Saving data as: {report_name}") with pd.ExcelWriter(save_path, engine='openpyxl', mode='a',if_sheet_exists="overlay") as writer:
dataframe_to_append.to_excel(writer, index=False, sheet_name=sheet_name,startrow=len(current_data),header=False) debug(f"Consolidated Report | {sheet_name}: Saving data as: {report_name}")
dataframe_to_append.to_excel(writer, index=False, sheet_name=sheet_name,startrow=len(current_data),header=False)
except Exception as e:
error(f"[E] Failed to append to consolidated report! {sheet_name}:\n{e}")
def create_line_divider(breakage_list: list): def create_line_divider(breakage_list: list):

@ -14,6 +14,7 @@ with open("settings.json") as s:
if settings["debug"]: if settings["debug"]:
basicConfig(filename='debug.log', encoding='utf-8', level=DEBUG) basicConfig(filename='debug.log', encoding='utf-8', level=DEBUG)
debug("\n\n\n########################### VERSION = 3.10 ###########################\n\n\n")
debug("Running main.py...") debug("Running main.py...")
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
""" """
@ -104,7 +105,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
Launches the file selection dialog then inits the set_input function Launches the file selection dialog then inits the set_input function
""" """
debug("Launching getfile.") debug("Launching getfile.")
inFile = QtWidgets.QFileDialog.getOpenFileName(self, 'Open file') inFile = QtWidgets.QFileDialog.getOpenFileName(self, 'Open file',directory=self.settings["defaultLocations"][self.curReportType])
# If the user does not select a file we just exit and do not change anything # If the user does not select a file we just exit and do not change anything
if inFile[0] == '': if inFile[0] == '':
debug(f"User did not select an input file! {inFile}") debug(f"User did not select an input file! {inFile}")
@ -178,6 +179,9 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
debug(f"Process Selction | dataframe:\n{dataframe}") debug(f"Process Selction | dataframe:\n{dataframe}")
# The text preview box can have trouble loading the larger dataframes so # The text preview box can have trouble loading the larger dataframes so
# they are trimmed to 500 so that the users can see if anything got messed up # they are trimmed to 500 so that the users can see if anything got messed up
if dataframe.empty:
self.inputFilePreview.setText("Failed to create dataframe!")
return None
smallDF = dataframe.iloc[0:500,:] smallDF = dataframe.iloc[0:500,:]
self.inputFilePreview.setText(smallDF.to_html(index=False)) self.inputFilePreview.setText(smallDF.to_html(index=False))
# Enable the excel button so users can open the file # Enable the excel button so users can open the file
@ -213,9 +217,9 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
Checks if a report type already has a default location Checks if a report type already has a default location
If the default location is blank, the current inputFileLocation will be saved If the default location is blank, the current inputFileLocation will be saved
""" """
if self.settings["defaultLocations"][self.curReportType]["dir"] == '': if self.settings["defaultLocations"][self.curReportType] == '':
self.settings["defaultLocations"][self.curReportType]["dir"] = ('/').join(self.inputFile.split('/')[:-1]) self.settings["defaultLocations"][self.curReportType] = ('/').join(self.inputFile.split('/')[:-1])
debug(f"checked_for_saved: saved new deafult location | {self.curReportType} | {self.settings['defaultLocations'][self.curReportType]['dir']}") debug(f"checked_for_saved: saved new deafult location | {self.curReportType} | {self.settings['defaultLocations'][self.curReportType]}")
with open('settings.json', 'w') as s: with open('settings.json', 'w') as s:
# Save changes to the setting # Save changes to the setting
json.dump(self.settings, s) json.dump(self.settings, s)
@ -226,6 +230,10 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
# This will be used in settings the the extract function and determining file locations # This will be used in settings the the extract function and determining file locations
# Disable the process report button since the input and output fields are now blank # Disable the process report button since the input and output fields are now blank
self.processReportButton.setEnabled(False) self.processReportButton.setEnabled(False)
self.openExcelButton.setEnabled(False)
self.copyButton.setEnabled(False)
self.openFolderButton.setEnabled(False)
if self.reportTypeCB.currentText() == "ACH": if self.reportTypeCB.currentText() == "ACH":
self.curReportType = "ach" self.curReportType = "ach"
self.extract_function = ilx.ach self.extract_function = ilx.ach
@ -269,32 +277,16 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.curReportType = "pymt" self.curReportType = "pymt"
self.extract_function = ilx.payment_transactions self.extract_function = ilx.payment_transactions
# Replace the hard-coded dates in the default locations with the current date self.inputFile = ""
inputRoot = self.settings["defaultLocations"][self.curReportType]["dir"]\
.replace("{dd}",dt.now().strftime('%d'))\
.replace("{mm}",dt.now().strftime('%m'))\
.replace("{yyyy}",dt.now().strftime('%Y')).replace("{yy}",dt.now().strftime('%y'))
inputFile = self.settings["defaultLocations"][self.curReportType]["fn"]\
.replace("{dd}",dt.now().strftime('%d'))\
.replace("{mm}",dt.now().strftime('%m'))\
.replace("{yyyy}",dt.now().strftime('%Y')).replace("{yy}",dt.now().strftime('%y'))
# If inputfile is blank, just leave the input root from settings
self.inputFile = f"{inputRoot}/{inputFile}" if (inputFile != '') else inputRoot
self.inputFileLE.setText(self.inputFile) self.inputFileLE.setText(self.inputFile)
# Automatically sets output to be in the same file as input, with a naming scheme # Automatically sets output to be in the same file as input, with a naming scheme
# The report type selected in the combo box will dictate the naming # The report type selected in the combo box will dictate the naming
if self.inputFile == "":
outputroot = ('/').join(self.inputFileLE.text().split('/')[:-1])
else:
outputroot = inputRoot + '/'
if self.curReportType == "minv": if self.curReportType == "minv":
self.outputFile = f"{outputroot}{self.reportTypeCB.currentText()}_{dt.now().strftime('%Y%m%d_%H%M')}.txt" self.outputFile = f"{self.reportTypeCB.currentText()}_{dt.now().strftime('%Y%m%d_%H%M')}.txt"
else: else:
self.outputFile = f"{outputroot}{self.reportTypeCB.currentText().replace('/','')}_{dt.now().strftime('%Y%m%d_%H%M')}.xlsx" self.outputFile = f"{self.reportTypeCB.currentText().replace('/','')}_{dt.now().strftime('%Y%m%d_%H%M')}.xlsx"
self.outputFileLE.setText(self.outputFile) self.outputFileLE.setText(self.outputFile)
self.openExcelButton.setEnabled(False)
self.copyButton.setEnabled(False)
self.openFolderButton.setEnabled(True)
debug(f"report_type_change | inputFile: {self.inputFile}") debug(f"report_type_change | inputFile: {self.inputFile}")
debug(f"report_type_change | outputFile: {self.outputFile}") debug(f"report_type_change | outputFile: {self.outputFile}")
self.check_ready_to_process() self.check_ready_to_process()

@ -1 +1 @@
{"debug": false, "consolidatedBasePath": "", "defaultLocations": {"ach": {"dir": "", "fn": "", "custom": false}, "minv": {"dir": "", "fn": "", "custom": false}, "niv": {"dir": "", "fn": "", "custom": false}, "ren": {"dir": "", "fn": "", "custom": false}, "pymt": {"dir": "", "fn": "", "custom": false}, "uap": {"dir": "", "fn": "", "custom": false}, "pastdue": {"dir": "", "fn": "", "custom": false}}} {"debug": false, "consolidatedBasePath": "leafnow.com/shared/cashapps", "defaultLocations": {"ach": "", "disp": "", "gl": "", "lb": "", "minv": "", "niv": "", "ren": "", "pymt": "", "uap": "", "pastdue": ""}}
Loading…
Cancel
Save