Preparing to support generic p7m files.

This commit is contained in:
Franco Masotti 2019-10-10 20:04:16 +02:00
parent 383d5c3a3a
commit 4d35c145f4
Signed by: frnmst
GPG Key ID: 24116ED85666780A
2 changed files with 48 additions and 35 deletions

View File

@ -519,7 +519,8 @@ def pipeline(metadata_file: str,
invoice_xslt_type: str = 'ordinaria',
ignore_attachment_extension_whitelist: bool = False,
ignore_attachment_filetype_whitelist: bool = False,
write_default_configuration_file: bool = False):
write_default_configuration_file: bool = False,
generic_p7m_file = False):
r"""Run the pipeline."""
project_name = 'fattura_elettronica_reader'
create_appdirs(project_name)
@ -549,8 +550,8 @@ def pipeline(metadata_file: str,
metadata_root = parse_xml_file(metadata_file)
if invoice_filename is None:
invoice_filename = get_invoice_filename(
metadata_root, config['metadata file']['XML invoice filename tag'],
dict(default = config['metadata file']['XML namespace']))
metadata_root, config['metadata file']['XML invoice filename tag'],
dict(default = config['metadata file']['XML namespace']))
if invoice_filename is None:
raise MissingTagInMetadataFile
@ -588,7 +589,7 @@ def pipeline(metadata_file: str,
ignore_signers_certificate_check):
raise InvoiceFileNotAuthentic
if not no_invoice_xml_validation:
if not generic_p7m_file or not no_invoice_xml_validation:
# This W3C file should not change any time soon so we can avoid the force download option.
if not pathlib.Path(w3c_schema_file_for_xml_signatures).exists():
@ -600,47 +601,48 @@ def pipeline(metadata_file: str,
patch_invoice_schema_file(invoice_schema_file, Patch['invoice file']['XSD']['line'][0]['offending'],Patch['invoice file']['XSD']['line'][0]['fix'])
# Create a temporary directory to store the original XML invoice file.
with tempfile.TemporaryDirectory() as tmpdirname:
invoice_original_file = invoice_filename + '.xml'
if not generic_p7m_file:
# Create a temporary directory to store the original XML invoice file.
with tempfile.TemporaryDirectory() as tmpdirname:
invoice_original_file = invoice_filename + '.xml'
if not remove_signature_from_invoice_file(invoice_filename,
if not remove_signature_from_invoice_file(invoice_filename,
str(pathlib.Path(tmpdirname, invoice_original_file))):
raise CannotExtractOriginalInvoiceFile
raise CannotExtractOriginalInvoiceFile
if not no_invoice_xml_validation:
if not is_xml_file_conforming_to_schema(str(pathlib.Path(tmpdirname, invoice_original_file)), invoice_schema_file):
raise XMLFileNotConformingToSchema
if not no_invoice_xml_validation:
if not is_xml_file_conforming_to_schema(str(pathlib.Path(tmpdirname, invoice_original_file)), invoice_schema_file):
raise XMLFileNotConformingToSchema
invoice_root = parse_xml_file(str(pathlib.Path(tmpdirname, invoice_original_file)))
invoice_root = parse_xml_file(str(pathlib.Path(tmpdirname, invoice_original_file)))
if extract_attachments:
extract_attachments_from_invoice_file(
invoice_root, config['invoice file']['XML attachment XPath'],
config['invoice file']['XML attachment tag'],
config['invoice file']['XML attachment filename tag'],
config['invoice file']['text encoding'],
ignore_attachment_extension_whitelist,
ignore_attachment_filetype_whitelist,
config['invoice file']['attachment extension whitelist'],
config['invoice file']['attachment filetype whitelist'])
if extract_attachments:
extract_attachments_from_invoice_file(
invoice_root, config['invoice file']['XML attachment XPath'],
config['invoice file']['XML attachment tag'],
config['invoice file']['XML attachment filename tag'],
config['invoice file']['text encoding'],
ignore_attachment_extension_whitelist,
ignore_attachment_filetype_whitelist,
config['invoice file']['attachment extension whitelist'],
config['invoice file']['attachment filetype whitelist'])
if generate_html_output:
if generate_html_output:
if force_invoice_xml_stylesheet_file_download or not pathlib.Path(
invoice_xslt_file).exists():
get_remote_file(
invoice_xslt_file, config['invoice file']['XSLT ' + invoice_xslt_type + ' download'])
if force_invoice_xml_stylesheet_file_download or not pathlib.Path(
invoice_xslt_file).exists():
get_remote_file(
invoice_xslt_file, config['invoice file']['XSLT ' + invoice_xslt_type + ' download'])
invoice_xslt_root = parse_xml_file(invoice_xslt_file)
invoice_xslt_root = parse_xml_file(invoice_xslt_file)
html_output = invoice_filename + '.html'
html_output = invoice_filename + '.html'
get_invoice_as_html(invoice_root, invoice_xslt_root, html_output,
config['invoice file']['text encoding'])
get_invoice_as_html(invoice_root, invoice_xslt_root, html_output,
config['invoice file']['text encoding'])
if keep_original_invoice:
shutil.move(str(pathlib.Path(tmpdirname, invoice_original_file)), invoice_original_file)
if keep_original_invoice:
shutil.move(str(pathlib.Path(tmpdirname, invoice_original_file)), invoice_original_file)
if __name__ == '__main__':

View File

@ -48,6 +48,9 @@ class CliToApi():
args.extract_attachments = True
args.generate_html_output = True
args.keep_original_invoice = True
if args.generic_p7m_file:
# The generic p7m file might not be an XML file.
args.no_invoice_xml_validation = True
for metadata_file in args.metadata_file:
pipeline(
metadata_file=metadata_file,
@ -66,7 +69,8 @@ class CliToApi():
invoice_xslt_type=args.invoice_xslt_type,
ignore_attachment_extension_whitelist=args.ignore_attachment_extension_whitelist,
ignore_attachment_filetype_whitelist=args.ignore_attachment_filetype_whitelist,
write_default_configuration_file=args.write_default_configuration_file)
write_default_configuration_file=args.write_default_configuration_file,
generic_p7m_file=args.generic_p7m_file)
class CliInterface():
"""The interface exposed to the final user."""
@ -98,6 +102,13 @@ class CliInterface():
action='store_true',
help='write the default configuration file')
parser.add_argument(
'-g',
'--generic-p7m-file',
action='store_true',
help='the input file is a generic p7m file, not a p7m invoice file. \
You need to provide a dummy metadata file')
parser.add_argument(
'-i',
'--invoice-filename',