Added destination directory option.

This commit is contained in:
Franco Masotti 2021-12-16 18:33:17 +01:00
parent 6793cce43e
commit 5ed971874e
Signed by: frnmst
GPG Key ID: 24116ED85666780A
3 changed files with 34 additions and 13 deletions

View File

@ -277,7 +277,8 @@ def extract_attachments_from_invoice_file(
ignore_attachment_extension_whitelist: bool = False,
ignore_attachment_filetype_whitelist: bool = False,
attachment_extension_whitelist: list = list(),
attachment_filetype_whitelist: list = list()):
attachment_filetype_whitelist: list = list(),
destination_directory: str = '.'):
r"""Extract, decode and save possible attachments within the invoice file.
:param invoice_file_xml_root: the original invoice file.
@ -293,8 +294,9 @@ def extract_attachments_from_invoice_file(
Defaults to ``False``.
:param ignore_attachment_filetype_whitelist: avoid cheking file types.
Defaults to ``False``.
:param attachment_extension_whitelist: . Defaults to ``list()``.
:param attachment_filetype_whitelist: . Defaults to ``list()``.
:param attachment_extension_whitelist: allowed attachment extensions. Defaults to ``list()``.
:param attachment_filetype_whitelist: allowed attachment file types. Defaults to ``list()``.
:param destination_directory: the output directory for the attachments. Defaults to ``.``.
:type invoice_file_xml_root: str
:type invoice_file_xml_attachment_xpath: str
:type invoice_file_xml_attachment_tag: str
@ -304,14 +306,15 @@ def extract_attachments_from_invoice_file(
:type ignore_attachment_filetype_whitelist: bool
:type attachment_extension_whitelist: list
:type attachment_filetype_whitelist: list
:type destination_directory: str
:returns: None
:rtype: None
:raises: base64.binascii.Error, filetype, atomicwrites, or a built-in exception.
"""
for at in invoice_file_xml_root.findall(invoice_file_xml_attachment_xpath):
attachment = at.find(invoice_file_xml_attachment_tag).text
attachment_dest_path = at.find(
invoice_file_xml_attachment_filename_tag).text
attachment_dest_path = str(pathlib.Path(destination_directory, at.find(
invoice_file_xml_attachment_filename_tag).text))
if not ignore_attachment_extension_whitelist:
if not attachment_dest_path.endswith(
@ -340,7 +343,8 @@ def extract_attachments_from_invoice_file(
def get_invoice_as_html(invoice_file_xml_root,
invoice_file_xml_stylesheet_root,
html_output_file: str,
invoice_file_text_encoding: str):
invoice_file_text_encoding: str,
destination_directory: str = '.'):
r"""Transform the XML invoice file into a styled HTML file.
:param invoice_file_xml_root: the XML tree root of the invoice file
@ -348,16 +352,19 @@ def get_invoice_as_html(invoice_file_xml_root,
:param html_output_file: the destination file.
:param invoice_file_text_encoding: the text encoding used for the
invoice file.
:param destination_directory: the output directory for the html file. Defaults to ``.``.
:type invoice_file_xml_root: lxml.etree._Element
:type invoice_file_xml_stylesheet_root: lxml.etree._Element
:type html_output_file: str
:type invoice_file_text_encoding: str
:type destination_directory: str
:returns: None
:rtype: None
:raises: an lxml, atomicwrites, or a built-in exception.
"""
transform = ET.XSLT(invoice_file_xml_stylesheet_root)
newdom = transform(invoice_file_xml_root)
html_output_file = str(pathlib.Path(destination_directory, html_output_file))
with atomicwrites.atomic_write(html_output_file, mode='w',
overwrite=True) as f:
f.write(
@ -693,6 +700,7 @@ def pipeline(source: str, file_type: str, data: dict):
Choose between ``p7m`` and ``plain``, depending on the
source parameter.
:param data: a data structure containing all the fields.
See the cli.py file.
:type source: str
:type file_type: str
:type data: dict
@ -781,7 +789,7 @@ def pipeline(source: str, file_type: str, data: dict):
if not data['ignore assets checksum']:
if not asset_checksum_matches(trusted_list_file):
raise AssetsChecksumDoesNotMatch("Run the program with the '--ignore-assets-checksum' option, contact the developer or open a pull request. Have a look at https://frnmst.github.io/fattura-elettronica-reader/assets.html")
raise AssetsChecksumDoesNotMatch("Run the program with the '--ignore-assets-checksum' option, contact the developer or open a pull request. Have a look at " + const['Docs']['assets url'])
trusted_list_xml_root = parse_xml_file(trusted_list_file)
@ -880,7 +888,8 @@ def pipeline(source: str, file_type: str, data: dict):
data['ignore attachment extension whitelist'],
data['ignore attachment filetype whitelist'],
config['invoice file']['attachment extension whitelist'],
config['invoice file']['attachment filetype whitelist'])
config['invoice file']['attachment filetype whitelist'],
data['destination directory'])
if data['generate html output']:
if data['force invoice xml stylesheet file download'] or not pathlib.Path(
@ -897,9 +906,11 @@ def pipeline(source: str, file_type: str, data: dict):
invoice_xslt_root = parse_xml_file(invoice_xslt_file)
html_output = file_to_consider + '.html'
get_invoice_as_html(invoice_root, invoice_xslt_root,
html_output,
config['invoice file']['text encoding'])
config['invoice file']['text encoding'],
data['destination directory'])
if (source == 'invoice'
and file_type == 'p7m') or (source == 'generic'
@ -909,7 +920,7 @@ def pipeline(source: str, file_type: str, data: dict):
str(
pathlib.Path(tmpdirname,
file_to_consider_original_relative)),
file_to_consider_original)
str(pathlib.Path(data['destination directory'], file_to_consider_original)))
if __name__ == '__main__':

View File

@ -56,6 +56,8 @@ class CliToApi():
args.write_default_configuration_file,
'ignore assets checksum':
args.ignore_assets_checksum,
'destination directory':
args.destination_directory,
}
# Prepare the data structure.
@ -313,9 +315,9 @@ class CliInterface():
nargs='+',
help='the p7m file names')
###########
# Common #
###########
##########
# Common #
##########
parser.add_argument('-c',
'--configuration-file',
default=str(),
@ -326,6 +328,11 @@ class CliInterface():
action='store_true',
help='write the default configuration file')
parser.add_argument('-d',
'--destination-directory',
default='.',
help='the output directory for all files')
parser.add_argument('-k',
'--ignore-assets-checksum',
action='store_true',

View File

@ -141,5 +141,8 @@ Checksum[Paths['invoice file']['XSD']['default']] = 'a1b02818f81ac91f35358260dd1
Checksum[Paths['trusted list file']] = '18ba1d9fd695c95c2c445b0203b268404066386b9e433991ffc32699af08ab3cda4ec2a6d26ebb4d53ca830470f6aba2b35c8c954245e730fbed1ecaee30bd14'
Docs = dict()
Docs['assets url'] = 'https://docs.franco.net.eu.org/fattura-elettronica-reader/assets.html'
if __name__ == '__main__':
pass