You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
4.6 KiB
115 lines
4.6 KiB
from ui import Ui_MainWindow
|
|
import ILParser
|
|
from PyQt5 import QtWidgets
|
|
from logging import debug, DEBUG, basicConfig, warn
|
|
from sys import argv
|
|
from typing import Literal, Optional
|
|
from pandas import DataFrame, ExcelWriter
|
|
from datetime import datetime as dt
|
|
|
|
|
|
logConfig = basicConfig(filename='ILFormatter.log', encoding='utf-8', level=DEBUG, filemode='w')
|
|
|
|
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
|
|
def __init__(self, *args, obj=None, **kwargs):
|
|
debug("MainWindow class init..")
|
|
super(MainWindow, self).__init__(*args, **kwargs)
|
|
self.setupUi(self)
|
|
self.processButton.setEnabled(False)
|
|
|
|
# File Locations
|
|
self.assetFile = None
|
|
self.custFile = None
|
|
self.dobFile = None
|
|
self.finFile = None
|
|
self.outputLocation = None
|
|
|
|
# Button Hooks
|
|
self.assetButton.clicked.connect(lambda: self._set_file(lineEdit= self.assetLE, selfFile="ASSET"))
|
|
self.custButton.clicked.connect(lambda: self._set_file(lineEdit= self.custLe, selfFile="CUST"))
|
|
self.dobButton.clicked.connect(lambda: self._set_file(lineEdit= self.dobLE, selfFile="DOB"))
|
|
self.finButton.clicked.connect(lambda: self._set_file(lineEdit= self.finLE, selfFile="FIN"))
|
|
self.outputButton.clicked.connect(lambda: self._set_output())
|
|
self.processButton.clicked.connect(lambda: self._process())
|
|
|
|
def _check_files(self):
|
|
debug(self.assetFile)
|
|
debug(self.custFile)
|
|
debug(self.dobFile)
|
|
debug(self.finFile)
|
|
ready = (
|
|
self.assetFile != None and
|
|
self.custFile != None and
|
|
self.dobFile != None and
|
|
self.finFile != None and
|
|
self.outputLocation != None
|
|
)
|
|
self.processButton.setEnabled(ready)
|
|
|
|
def _set_file(self, lineEdit: QtWidgets.QLineEdit, selfFile: Literal["ASSET", "CUST", "DOB", "FIN"]) -> str :
|
|
selectedFile: list[str] = QtWidgets.QFileDialog.getOpenFileName(self, "OpenFile")
|
|
debug(f"Selected file: {selectedFile}")
|
|
lineEdit.setText(selectedFile[0])
|
|
file = selectedFile[0] if selectedFile[0] != '' else None
|
|
if file != None and self.outputLocation == None:
|
|
self._auto_output_set(fileRoot='/'.join(file.split('/')[:-1]))
|
|
if selfFile == "ASSET":
|
|
self.assetFile = file
|
|
elif selfFile == "CUST":
|
|
self.custFile = file
|
|
elif selfFile == "DOB":
|
|
self.dobFile = file
|
|
elif selfFile == "FIN":
|
|
self.finFile = file
|
|
self._check_files()
|
|
|
|
def _set_output(self):
|
|
self.outputLocation = QtWidgets.QFileDialog.getSaveFileName(self, "Output file name") if QtWidgets.QFileDialog.getSaveFileName(self, "Output file name") != '' else None
|
|
self.outputLE.setText(self.outputLocation if self.outputLocation != None else '')
|
|
debug(f"Output Location: {self.outputLocation}")
|
|
|
|
def _auto_output_set(self, fileRoot):
|
|
self.outputLocation = fileRoot + f"/Portfolio Contracts - {dt.now().strftime('%Y-%m-%d')}.xlsx"
|
|
self.outputLE.setText(self.outputLocation if self.outputLocation != None else '')
|
|
debug(f"Auto set output: {self.outputLocation}")
|
|
|
|
def _setAssetFile(self):
|
|
self.assetFile = self._set_file(self.assetLE)
|
|
|
|
def _parse_file(self, filePath: str, parseColumns: list[ILParser.Column]) -> Optional[DataFrame]:
|
|
with open(filePath) as file:
|
|
report = file.read()
|
|
debug(f"Report: {report}")
|
|
data: DataFrame = ILParser.parse(report, parseColumns)
|
|
debug(f"Data: {data}")
|
|
if data.empty:
|
|
return None
|
|
else: return data
|
|
|
|
def _process(self):
|
|
assetDf: DataFrame = self._parse_file(self.assetFile, ILParser.ASSET_COLS)
|
|
debug(assetDf)
|
|
custDf: DataFrame = self._parse_file(self.custFile, ILParser.CUST_COLS)
|
|
debug(custDf)
|
|
dobDf: DataFrame = self._parse_file(self.dobFile, ILParser.DOB_COL)
|
|
debug(dobDf)
|
|
finDf: DataFrame = self._parse_file(self.finFile, ILParser.FIN_COLUMNS)
|
|
debug(finDf)
|
|
with ExcelWriter(self.outputLocation) as writer:
|
|
assetDf.to_excel(writer, sheet_name="ASSET")
|
|
custDf.to_excel(writer, sheet_name="CUST")
|
|
dobDf.to_excel(writer, sheet_name="DOB")
|
|
finDf.to_excel(writer, sheet_name="FIN")
|
|
debug("Finished writing to excel.")
|
|
|
|
|
|
# Defines the app
|
|
app = QtWidgets.QApplication(argv)
|
|
# Sets the style
|
|
app.setStyle("Fusion")
|
|
# Builds the main window
|
|
window = MainWindow()
|
|
window.setWindowTitle("IL Extract")
|
|
window.show()
|
|
# Starts the app
|
|
app.exec() |