diff --git a/downloader.py b/downloader.py index 16bb0e4..db2b51d 100644 --- a/downloader.py +++ b/downloader.py @@ -18,7 +18,6 @@ def get_invoices_info(input_file_path: str) -> tuple: invoices = dict() owner_name = '_'.join(sheet["B1"].value.split()[2:]) - print(owner_name) for i in range(1, sheet.max_row+1): invoice_id = sheet["I"+str(i)].value diff --git a/exc.py b/exc.py index e6ee437..dc843b2 100644 --- a/exc.py +++ b/exc.py @@ -3,7 +3,6 @@ class FattureSanRossoreError(Exception): """Base class for other exceptions""" - class FileError(FattureSanRossoreError): """Basic exception for errors raised by files""" def __init__(self, file_path, msg=None): diff --git a/fatture_ccsr.py b/fatture_ccsr.py index 90c3982..f041643 100644 --- a/fatture_ccsr.py +++ b/fatture_ccsr.py @@ -1,33 +1,45 @@ """This utility is used for downloading or converting to TRAF2000 invoices from a .csv or .xml report file""" +import os import wx import downloader import traf2000_converter import exc -import utils DOWNLOAD_ACTION = 1 CONVERT_ACTION = 2 +def file_extension(file_path: str, allowed_ext: set = None) -> str: + """Return the file extension if that's in the allowed extension set""" + if file_path in (None, ""): + raise exc.NoFileError() + file_ext = os.path.splitext(file_path)[1] + if file_ext in (None, ""): + raise exc.NoFileExtensionError + if allowed_ext is not None and file_ext not in allowed_ext: + raise exc.WrongFileExtensionError + return file_ext + class LogDialog(wx.Dialog): """logging panel""" def __init__(self, parent, title, action): - super(LogDialog, self).__init__(parent, wx.ID_ANY, title) + super(LogDialog, self).__init__(parent, wx.ID_ANY, title, style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER) main_sizer = wx.BoxSizer(wx.VERTICAL) - self.log_text = wx.TextCtrl(self, wx.ID_ANY, size=(300, 200), style=wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL) + self.log_text = wx.TextCtrl(self, wx.ID_ANY, size=(300, 200), style=wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL|wx.EXPAND) log_sizer = wx.BoxSizer(wx.HORIZONTAL) - log_sizer.Add(self.log_text, 0, wx.ALL, 2) + log_sizer.Add(self.log_text, 1, wx.ALL|wx.EXPAND, 2) self.log_text.Bind(wx.EVT_TEXT, self.on_text_update) if action == CONVERT_ACTION: self.nc_text = wx.TextCtrl(self, wx.ID_ANY, size=(300, 200), style=wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL) - log_sizer.Add(self.nc_text, 0, wx.ALL, 2) + self.nc_text.Bind(wx.EVT_TEXT, self.on_text_update) + log_sizer.Add(self.nc_text, 1, wx.ALL|wx.EXPAND, 2) - main_sizer.Add(log_sizer, 0, wx.ALL, 2) + main_sizer.Add(log_sizer, 1, wx.ALL|wx.EXPAND, 2) self.btn = wx.Button(self, wx.ID_OK, "Chiudi") self.btn.Disable() main_sizer.Add(self.btn, 0, wx.ALL|wx.CENTER, 2) @@ -43,29 +55,29 @@ class LoginDialog(wx.Dialog): """login dialog for basic auth download""" def __init__(self, parent, title): """constructor""" - super(LoginDialog, self).__init__(parent, wx.ID_ANY, title) + super(LoginDialog, self).__init__(parent, wx.ID_ANY, title, style=wx.DEFAULT_DIALOG_STYLE) self.logged_id = False user_sizer = wx.BoxSizer(wx.HORIZONTAL) user_lbl = wx.StaticText(self, wx.ID_ANY, "Username:") - user_sizer.Add(user_lbl, 0, wx.ALL|wx.CENTER, 2) + user_sizer.Add(user_lbl, 0, wx.ALL|wx.CENTER|wx.EXPAND, 2) self.username = wx.TextCtrl(self, size=(265, -1)) - user_sizer.Add(self.username, 0, wx.ALL, 2) + user_sizer.Add(self.username, 0, wx.ALL|wx.EXPAND, 2) pass_sizer = wx.BoxSizer(wx.HORIZONTAL) pass_lbl = wx.StaticText(self, wx.ID_ANY, "Password:") - pass_sizer.Add(pass_lbl, 0, wx.ALL|wx.CENTER, 2) + pass_sizer.Add(pass_lbl, 0, wx.ALL|wx.CENTER|wx.EXPAND, 2) self.password = wx.TextCtrl(self, size=(265, -1), style=wx.TE_PASSWORD|wx.TE_PROCESS_ENTER) - pass_sizer.Add(self.password, 0, wx.ALL, 2) + pass_sizer.Add(self.password, 0, wx.ALL|wx.EXPAND, 2) login_btn = wx.Button(self, label="Login") login_btn.SetDefault() login_btn.Bind(wx.EVT_BUTTON, self.on_login) main_sizer = wx.BoxSizer(wx.VERTICAL) - main_sizer.Add(user_sizer, 0, wx.ALL, 2) - main_sizer.Add(pass_sizer, 0, wx.ALL, 2) + main_sizer.Add(user_sizer, 1, wx.ALL|wx.EXPAND, 2) + main_sizer.Add(pass_sizer, 1, wx.ALL|wx.EXPAND, 2) main_sizer.Add(login_btn, 0, wx.ALL|wx.CENTER, 2) self.SetSizerAndFit(main_sizer) @@ -129,7 +141,7 @@ class FattureCCSRFrame(wx.Frame): self.input_file_path = event.GetEventObject().GetPath() #TODO: error frame try: - self.input_file_ext = utils.file_extension(self.input_file_path, (".xml", ".csv", ".xlsx")) + self.input_file_ext = file_extension(self.input_file_path, (".xml", ".csv", ".xlsx")) except exc.NoFileError as handled_exception: print(handled_exception.args[0]) except exc.NoFileExtensionError as handled_exception: @@ -157,16 +169,11 @@ class FattureCCSRFrame(wx.Frame): downloader.download_invoices(self) self.log_dialog.btn.Enable() elif btn_id == CONVERT_ACTION: - if self.output_traf2000_dialog.ShowModal() == wx.ID_OK: - self.output_file_path = self.output_traf2000_dialog.GetPath() - else: - #TODO: avviso errore file output - return self.log_dialog = LogDialog(self, "Conversione delle fatture in TRAF2000", CONVERT_ACTION) self.log_dialog.Show() #TODO: error frame try: - traf2000_converter.convert(self.input_file_path, self.output_file_path, self) + traf2000_converter.convert(self) except exc.NoFileError as handled_exception: print(handled_exception.args[0]) except exc.NoFileExtensionError as handled_exception: diff --git a/logger.py b/logger.py deleted file mode 100644 index 6d3d5bd..0000000 --- a/logger.py +++ /dev/null @@ -1,19 +0,0 @@ -"""Create logging handler""" -import logging - -#_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') - -downloader_logger = logging.getLogger("downloader") -downloader_logger.setLevel(logging.DEBUG) - -converter_logger = logging.getLogger("converter") -converter_logger.setLevel(logging.DEBUG) -#_converter_ch = logging.StreamHandler() -#_converter_ch.setFormatter(_formatter) -#converter_logger.addHandler(_converter_ch) - -note_credito_logger = logging.getLogger("note_credito") -note_credito_logger.setLevel(logging.INFO) -#_note_credito_ch = logging.StreamHandler() -#_note_credito_ch.setFormatter(_formatter) -#note_credito_logger.addHandler(_note_credito_ch) diff --git a/traf2000_converter.py b/traf2000_converter.py index 756d515..b1e3afd 100755 --- a/traf2000_converter.py +++ b/traf2000_converter.py @@ -6,12 +6,10 @@ import xml.etree.ElementTree import unidecode import wx -import utils - -def import_csv(csv_file_path: str, parent) -> dict: +def import_csv(parent) -> dict: """Return a dict containing the invoices info""" fatture = dict() - with open(csv_file_path, newline="") as csv_file: + with open(parent.input_file_path, newline="") as csv_file: lettore = csv.reader(csv_file, delimiter=",") for _ in range(4): @@ -52,11 +50,11 @@ def import_csv(csv_file_path: str, parent) -> dict: wx.Yield() return fatture -def import_xml(xml_file_path: str, parent) -> dict: +def import_xml(parent) -> dict: """Return a dict containing the invoices info""" fatture = dict() - tree = xml.etree.ElementTree.parse(xml_file_path) + tree = xml.etree.ElementTree.parse(parent.input_file_path) root = tree.getroot() for fattura in root.iter('{STAT_FATTURATO_CTERZI}Dettagli'): @@ -96,17 +94,22 @@ def import_xml(xml_file_path: str, parent) -> dict: return fatture -def convert(input_file_path: str, out_file_path: str, parent): +def convert(parent): """Output to a file the TRAF2000 records""" - input_file_ext = utils.file_extension(input_file_path, (".xml", ".csv")) - if input_file_ext == ".csv": - fatture = import_csv(input_file_path, parent) + if parent.input_file_ext == ".csv": + fatture = import_csv(parent) - elif input_file_ext == ".xml": - fatture = import_xml(input_file_path, parent) + elif parent.input_file_ext == ".xml": + fatture = import_xml(parent) - with open(out_file_path, "w") as traf2000_file: + if parent.output_traf2000_dialog.ShowModal() == wx.ID_OK: + parent.output_file_path = parent.output_traf2000_dialog.GetPath() + else: + #TODO: avviso errore file output + return + + with open(parent.output_file_path, "w") as traf2000_file: parent.log_dialog.nc_text.AppendText("Note di credito:\n") wx.Yield() diff --git a/utils.py b/utils.py deleted file mode 100644 index bddd732..0000000 --- a/utils.py +++ /dev/null @@ -1,16 +0,0 @@ -"""Some useful utilities""" - -import os - -import exc - -def file_extension(file_path: str, allowed_ext: set = None) -> str: - """Return the file extension if that's in the allowed extension set""" - if file_path in (None, ""): - raise exc.NoFileError() - file_ext = os.path.splitext(file_path)[1] - if file_ext in (None, ""): - raise exc.NoFileExtensionError - if allowed_ext is not None and file_ext not in allowed_ext: - raise exc.WrongFileExtensionError - return file_ext