mirror of
https://github.com/Noettore/fattureCCSR.git
synced 2025-10-15 11:46:39 +02:00
fattureCCSR: added filepicker and filedialog for input and output files. Started working on exceptions handling.
Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
17
converter.py
17
converter.py
@@ -1,10 +1,13 @@
|
|||||||
"""This script ask for an input file and an output file and generates the TRAF2000 records from a .csv or .xml"""
|
"""ask for an input file and an output file and generates the TRAF2000 records from a .csv or .xml"""
|
||||||
|
|
||||||
|
import os
|
||||||
import datetime
|
import datetime
|
||||||
import csv
|
import csv
|
||||||
import xml.etree.ElementTree
|
import xml.etree.ElementTree
|
||||||
import unidecode
|
import unidecode
|
||||||
|
|
||||||
|
import exc
|
||||||
|
|
||||||
def import_csv(csv_file_path):
|
def import_csv(csv_file_path):
|
||||||
"""Return a dict containing the invoices info"""
|
"""Return a dict containing the invoices info"""
|
||||||
fatture = dict()
|
fatture = dict()
|
||||||
@@ -89,8 +92,18 @@ def import_xml(xml_file_path):
|
|||||||
return fatture
|
return fatture
|
||||||
|
|
||||||
|
|
||||||
def convert(fatture, out_file_path):
|
def convert(input_file_path, out_file_path):
|
||||||
"""Output to a file the TRAF2000 records"""
|
"""Output to a file the TRAF2000 records"""
|
||||||
|
input_file_ext = os.path.splitext(input_file_path)[1]
|
||||||
|
if input_file_ext == ".csv":
|
||||||
|
fatture = import_csv(input_file_path)
|
||||||
|
|
||||||
|
elif input_file_ext == ".xml":
|
||||||
|
fatture = import_xml(input_file_path)
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise exc.WrongFileExtension("Expected .csv or .xml but received " + input_file_ext)
|
||||||
|
|
||||||
with open(out_file_path, "w") as traf2000_file:
|
with open(out_file_path, "w") as traf2000_file:
|
||||||
print("Note di credito:\n")
|
print("Note di credito:\n")
|
||||||
|
|
||||||
|
7
exc.py
Normal file
7
exc.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
"""Define Python user-defined exceptions"""
|
||||||
|
|
||||||
|
class Error(Exception):
|
||||||
|
"""Base class for other exceptions"""
|
||||||
|
|
||||||
|
class WrongFileExtension(Error):
|
||||||
|
"""Raised when a file extension is not accepted"""
|
@@ -1,23 +1,27 @@
|
|||||||
"""This script prompts for downloading or converting to TRAF2000 from a .csv or .xml report file"""
|
"""This utility is used for downloading or converting to TRAF2000 invoices from a .csv or .xml report file"""
|
||||||
|
|
||||||
import sys
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import wx
|
import wx
|
||||||
|
|
||||||
import converter
|
import converter
|
||||||
import utilities
|
import exc
|
||||||
|
|
||||||
class FattureCCSRFrame(wx.Frame):
|
class FattureCCSRFrame(wx.Frame):
|
||||||
|
"""main application frame"""
|
||||||
def __init__(self, parent, title):
|
def __init__(self, parent, title):
|
||||||
|
self.input_file_path = None
|
||||||
|
self.input_file_ext = None
|
||||||
|
self.output_file_path = None
|
||||||
|
|
||||||
super(FattureCCSRFrame, self).__init__(parent, title=title, size=(500, 150))
|
super(FattureCCSRFrame, self).__init__(parent, title=title, size=(500, 150))
|
||||||
panel = wx.Panel(self)
|
panel = wx.Panel(self)
|
||||||
vbox = wx.BoxSizer(wx.VERTICAL)
|
vbox = wx.BoxSizer(wx.VERTICAL)
|
||||||
hbox1 = wx.BoxSizer(wx.HORIZONTAL)
|
hbox1 = wx.BoxSizer(wx.HORIZONTAL)
|
||||||
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
|
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
|
||||||
|
|
||||||
self.input_file_picker = wx.FilePickerCtrl(panel, 101, "", "Seleziona il .csv o .xml", "*.csv;*.xml")
|
self.input_file_picker = wx.FilePickerCtrl(panel, 101, "", "Seleziona il .csv o .xml", "*.csv;*.xml", style=wx.FLP_DEFAULT_STYLE|wx.FLP_USE_TEXTCTRL)
|
||||||
hbox1.Add(self.input_file_picker, 1, wx.EXPAND, 0)
|
hbox1.Add(self.input_file_picker, 1, wx.EXPAND, 0)
|
||||||
|
self.input_file_picker.Bind(wx.EVT_FILEPICKER_CHANGED, self.file_picker_changed)
|
||||||
|
|
||||||
self.download_btn = wx.Button(panel, 201, "Scarica Fatture")
|
self.download_btn = wx.Button(panel, 201, "Scarica Fatture")
|
||||||
hbox2.Add(self.download_btn, 0, wx.ALIGN_CENTER)
|
hbox2.Add(self.download_btn, 0, wx.ALIGN_CENTER)
|
||||||
@@ -26,6 +30,9 @@ class FattureCCSRFrame(wx.Frame):
|
|||||||
self.traf2000_btn = wx.Button(panel, 202, "Genera TRAF2000")
|
self.traf2000_btn = wx.Button(panel, 202, "Genera TRAF2000")
|
||||||
hbox2.Add(self.traf2000_btn, 0, wx.ALIGN_CENTER)
|
hbox2.Add(self.traf2000_btn, 0, wx.ALIGN_CENTER)
|
||||||
self.traf2000_btn.Bind(wx.EVT_BUTTON, self.btn_onclick)
|
self.traf2000_btn.Bind(wx.EVT_BUTTON, self.btn_onclick)
|
||||||
|
self.traf2000_btn.Disable()
|
||||||
|
|
||||||
|
self.output_file_dialog = wx.FileDialog(panel, "Scegli dove salvare il file TRAF2000", defaultFile="TRAF2000", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
|
||||||
|
|
||||||
vbox.Add(hbox1, 0, wx.EXPAND)
|
vbox.Add(hbox1, 0, wx.EXPAND)
|
||||||
vbox.Add(hbox2, 0, wx.ALIGN_CENTER_HORIZONTAL)
|
vbox.Add(hbox2, 0, wx.ALIGN_CENTER_HORIZONTAL)
|
||||||
@@ -35,30 +42,28 @@ class FattureCCSRFrame(wx.Frame):
|
|||||||
self.Centre()
|
self.Centre()
|
||||||
self.Show()
|
self.Show()
|
||||||
|
|
||||||
|
def file_picker_changed(self, event):
|
||||||
|
"""event raised when the input file path is changed"""
|
||||||
|
self.input_file_path = event.GetEventObject().GetPath()
|
||||||
|
if self.input_file_path in (None, ""):
|
||||||
|
print("ERROR: No input file selected!")
|
||||||
|
return
|
||||||
|
self.input_file_ext = os.path.splitext(self.input_file_path)[1]
|
||||||
|
if self.input_file_ext not in (".csv", ".xml"):
|
||||||
|
print("ERROR: wrong file extension: " + self.input_file_ext)
|
||||||
|
return
|
||||||
|
self.traf2000_btn.Enable()
|
||||||
|
|
||||||
def btn_onclick(self, event):
|
def btn_onclick(self, event):
|
||||||
|
"""event raised when a button is clicked"""
|
||||||
btn_id = event.GetEventObject().GetId()
|
btn_id = event.GetEventObject().GetId()
|
||||||
if btn_id == 202:
|
if btn_id == 202:
|
||||||
input_file_path = utilities.get_input_file("*.csv;*.xml")
|
if self.output_file_dialog.ShowModal() == wx.ID_OK:
|
||||||
if input_file_path is None:
|
self.output_file_path = self.output_file_dialog.GetPath()
|
||||||
sys.exit("ERROR: No input file selected!")
|
try:
|
||||||
|
converter.convert(self.input_file_path, self.output_file_path)
|
||||||
fatture_file_extension = os.path.splitext(input_file_path)[1]
|
except exc.WrongFileExtension as e:
|
||||||
|
print(e.args[0])
|
||||||
output_file_path = utilities.get_output_file("TRAF2000")
|
|
||||||
if output_file_path is None:
|
|
||||||
sys.exit("ERROR: No output file selected!")
|
|
||||||
|
|
||||||
if fatture_file_extension == ".csv":
|
|
||||||
fatture = converter.import_csv(input_file_path)
|
|
||||||
|
|
||||||
elif fatture_file_extension == ".xml":
|
|
||||||
fatture = converter.import_xml(input_file_path)
|
|
||||||
|
|
||||||
else:
|
|
||||||
sys.exit("ERROR: file extension not supported")
|
|
||||||
|
|
||||||
converter.convert(fatture, output_file_path)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app = wx.App()
|
app = wx.App()
|
||||||
|
@@ -1,29 +0,0 @@
|
|||||||
# fattureSanRossore Downloader
|
|
||||||
|
|
||||||
[](../LICENSE.md) [](#) [](https://github.com/Noettore/fattureSanRossore/commit/master)
|
|
||||||
|
|
||||||
## Getting Started
|
|
||||||
|
|
||||||
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
|
|
||||||
|
|
||||||
### Prerequisites
|
|
||||||
|
|
||||||
- [Python3](https://www.python.org/)
|
|
||||||
|
|
||||||
### Run
|
|
||||||
|
|
||||||
To run you can simply execute python script.
|
|
||||||
It will ask for an input(.csv) and an output(TRAF2000) file.
|
|
||||||
|
|
||||||
## Dependencies
|
|
||||||
|
|
||||||
- [unidecode](https://pypi.org/project/Unidecode/)
|
|
||||||
- [easygui](https://pypi.org/project/easygui/)
|
|
||||||
|
|
||||||
## Author
|
|
||||||
|
|
||||||
- [**Ettore Dreucci**](https://ettore.dreucci.it)
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
This project is licensed under the MIT License - see the [LICENSE.md](../LICENSE.md) file for details
|
|
25
utilities.py
25
utilities.py
@@ -1,25 +0,0 @@
|
|||||||
import wx
|
|
||||||
|
|
||||||
def get_input_file(wildcard):
|
|
||||||
"""Return the input file path"""
|
|
||||||
_ = wx.App(None)
|
|
||||||
style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
|
|
||||||
dialog = wx.FileDialog(None, "Scegli il .csv o .xml contenente le informazioni sulle fatture da importare", wildcard=wildcard, style=style)
|
|
||||||
if dialog.ShowModal() == wx.ID_OK:
|
|
||||||
path = dialog.GetPath()
|
|
||||||
else:
|
|
||||||
path = None
|
|
||||||
dialog.Destroy()
|
|
||||||
return path
|
|
||||||
|
|
||||||
def get_output_file(default_output_filename):
|
|
||||||
"""Return the output file path"""
|
|
||||||
_ = wx.App(None)
|
|
||||||
style = wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT
|
|
||||||
dialog = wx.FileDialog(None, "Scegli dove salvare il file TRAF2000", defaultFile=default_output_filename, style=style)
|
|
||||||
if dialog.ShowModal() == wx.ID_OK:
|
|
||||||
path = dialog.GetPath()
|
|
||||||
else:
|
|
||||||
path = None
|
|
||||||
dialog.Destroy()
|
|
||||||
return path
|
|
Reference in New Issue
Block a user