Browse Source

Updated archive_invoice_files.py to use fattura-elettronica-reader 3.0.0.

master
Franco Masotti 7 months ago
parent
commit
c2330d03e5
Signed by: frnmst
GPG Key ID: 24116ED85666780A
  1. 2
      .pre-commit-config.yaml
  2. 116
      automated_tasks/archiving/archive_invoice_files.py
  3. 2
      docs/scripts.rst

2
.pre-commit-config.yaml

@ -24,7 +24,7 @@ repos:
args: ['--ignore=E501,W503,W504']
- repo: https://github.com/PyCQA/bandit
rev: '1.7.1' # Use the sha / tag you want to point at
rev: '1.7.2' # Use the sha / tag you want to point at
hooks:
- id: bandit
args: ['--skip', 'B404,B506,B410,B603', '--level', 'LOW']

116
automated_tasks/archiving/archive_invoice_files.py

@ -2,7 +2,7 @@
#
# archive_invoice_files.py
#
# Copyright (C) 2019-2021 Franco Masotti (franco \D\o\T masotti {-A-T-} tutanota \D\o\T com)
# Copyright (C) 2019-2022 Franco Masotti (franco \D\o\T masotti {-A-T-} tutanota \D\o\T com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -167,33 +167,33 @@ def decode_invoice_file(metadata_file: str, invoice_file: str, extract_attachmen
file_type = 'p7m'
data = {
'patched': True,
'configuration file': str(),
'write default configuration file': False,
'extract attachments': extract_attachments,
'invoice xslt type': 'ordinaria',
'no invoice xml validation': False,
'force invoice schema file download': False,
'generate html output': True,
'invoice filename': invoice_file,
'no checksum check': False,
'force invoice xml stylesheet file download': False,
'ignore attachment extension whitelist': False,
'ignore attachment filetype whitelist': False,
'metadata file': metadata_file,
'ignore signature check': False,
'ignore signers certificate check': False,
'force trusted list file download': False,
'keep original file': True,
'ignore assets checksum': False,
'destination directory': str(pathlib.Path(invoice_file).parents[0])
'configuration_file': str(),
'write_default_configuration_file': False,
'extract_attachments': extract_attachments,
'invoice_xslt_type': 'ordinaria',
'no_invoice_xml_validation': False,
'force_invoice_schema_file_download': False,
'generate_html_output': True,
'invoice_filename': invoice_file,
'no_checksum_check': False,
'force_invoice_xml_stylesheet_file_download': False,
'ignore_attachment_extension_whitelist': False,
'ignore_attachment_filetype_whitelist': False,
'metadata_file': metadata_file,
'ignore_signature_check': False,
'ignore_signers_certificate_check': False,
'force_trusted_list_file_download': False,
'keep_original_file': True,
'ignore_assets_checksum': False,
'destination_directory': str(pathlib.Path(invoice_file).parents[0])
}
status = {
'invoice file': invoice_file,
'valid checksum': True,
'valid signature and signers certificate': True,
'valid assets checksum': True,
'file type': 'p7m',
'invoice_file': invoice_file,
'valid_checksum': True,
'valid_signature_and_signers_certificate': True,
'valid_assets_checksum': True,
'file_type': file_type,
}
# Most probably a metadata file or a non-signed invoice file.
@ -204,7 +204,7 @@ def decode_invoice_file(metadata_file: str, invoice_file: str, extract_attachmen
else:
done = True
# Unprocessed.
status['invoice file'] = str()
status['invoice_file'] = str()
while not done:
try:
@ -213,19 +213,19 @@ def decode_invoice_file(metadata_file: str, invoice_file: str, extract_attachmen
data=data)
done = True
except fattura_elettronica_reader.exceptions.InvoiceFileChecksumFailed:
if status['valid checksum']:
status['valid checksum'] = False
if status['valid_checksum']:
status['valid_checksum'] = False
# Ignore checksum at the next iteration but mark the checksum
# as invalid.
data['no checksum check'] = True
except fattura_elettronica_reader.exceptions.P7MFileNotAuthentic:
if status['valid signature and signers certificate']:
status['valid signature and signers certificate'] = False
data['ignore signature check'] = True
data['ignore signers certificate check'] = True
if status['valid_signature_and_signers_certificate']:
status['valid_signature_and_signers_certificate'] = False
data['ignore_signature_check'] = True
data['ignore_signers_certificate_check'] = True
except fattura_elettronica_reader.exceptions.P7MFileDoesNotHaveACoherentCryptographicalSignature:
if status['file type'] == 'p7m':
status['file type'] = 'plain'
if status['file_type'] == 'p7m':
status['file_type'] = 'plain'
file_type = 'plain'
except lxml.etree.LxmlError:
# The selected metadata file is the real invoice file.
@ -233,9 +233,9 @@ def decode_invoice_file(metadata_file: str, invoice_file: str, extract_attachmen
done = True
traceback.print_exc()
except fattura_elettronica_reader.exceptions.AssetsChecksumDoesNotMatch:
if status['valid assets checksum']:
status['valid assets checksum'] = False
data['ignore assets checksum'] = True
if status['valid_assets_checksum']:
status['valid_assets_checksum'] = False
data['ignore_assets_checksum'] = True
except fattura_elettronica_reader.exceptions.CannotExtractOriginalP7MFile:
# Fatal error.
done = True
@ -250,27 +250,27 @@ def validate_decoded_invoice_files_struct(struct: list):
for e in struct:
if not isinstance(e, dict):
raise TypeError
if 'invoice file' not in e:
if 'invoice_file' not in e:
raise ValueError
if 'valid checksum' not in e:
if 'valid_checksum' not in e:
raise ValueError
if 'valid signature and signers certificate' not in e:
if 'valid_signature_and_signers_certificate' not in e:
raise ValueError
if 'valid assets checksum' not in e:
if 'valid_assets_checksum' not in e:
raise ValueError
if 'file type' not in e:
if 'file_type' not in e:
raise ValueError
if not isinstance(e['invoice file'], str):
if not isinstance(e['invoice_file'], str):
raise TypeError
if not isinstance(e['valid checksum'], bool):
if not isinstance(e['valid_checksum'], bool):
raise TypeError
if not isinstance(e['valid signature and signers certificate'], bool):
if not isinstance(e['valid_signature_and_signers_certificate'], bool):
raise TypeError
if not isinstance(e['valid assets checksum'], bool):
if not isinstance(e['valid_assets_checksum'], bool):
raise TypeError
if not isinstance(e['file type'], str):
if not isinstance(e['file_type'], str):
raise TypeError
if e['file type'] not in ['p7m', 'plain']:
if e['file_type'] not in ['p7m', 'plain']:
raise ValueError
@ -507,7 +507,7 @@ def decode_invoice_files(file_group: dict, extract_attachments: bool = False) ->
metadata_file = files_perm[j][0]
invoice_file = files_perm[j][1]
status = decode_invoice_file(metadata_file, invoice_file, extract_attachments)
if status['invoice file'] != str():
if status['invoice_file'] != str():
# Ignore unprocessed files.
invoice_files.append(status)
@ -529,7 +529,7 @@ def print_invoice(file: dict, data: dict):
r"""Print the invoice file."""
validate_config_struct(data)
html_file = file['invoice file'] + '.html'
html_file = file['invoice_file'] + '.html'
with tempfile.NamedTemporaryFile() as g:
css = CSS(string=data['print']['css string'])
html = HTML(html_file)
@ -542,7 +542,7 @@ def get_status_page(file: dict, data: dict):
r"""Save and print the status page."""
validate_config_struct(data)
html_file = file['invoice file'] + '.html'
html_file = file['invoice_file'] + '.html'
content = '<h1>' + pathlib.Path(html_file).stem + '</h1>'
if data['status page']['show']['info']['enabled']:
@ -552,22 +552,22 @@ def get_status_page(file: dict, data: dict):
shlex.split('openssl version'),
capture_output=True, shell=False).stdout.decode('UTF-8').rstrip() + '</h2> '
if data['status page']['status']['crypto']['enabled']:
if file['valid signature and signers certificate']:
if file['valid_signature_and_signers_certificate']:
content += '<h1>' + data['status page']['status']['crypto']['message'] + ' ' + data['status page']['status']['crypto']['valid value'] + '</h1>'
else:
content += '<h1>' + data['status page']['status']['crypto']['message'] + ' ' + data['status page']['status']['crypto']['invalid value'] + '</h1>'
if data['status page']['status']['checksum']['enabled']:
if file['valid checksum']:
if file['valid_checksum']:
content += '<h1>' + data['status page']['status']['checksum']['message'] + ' ' + data['status page']['status']['checksum']['valid value'] + '</h1>'
else:
content += '<h1>' + data['status page']['status']['checksum']['message'] + ' ' + data['status page']['status']['checksum']['invalid value'] + '</h1>'
if data['status page']['status']['p7m']['enabled']:
if file['file type'] == 'p7m':
if file['file_type'] == 'p7m':
content += '<h1>' + data['status page']['status']['p7m']['message'] + ' ' + data['status page']['status']['p7m']['valid value'] + '</h1>'
else:
content += '<h1>' + data['status page']['status']['p7m']['message'] + ' ' + data['status page']['status']['p7m']['invalid value'] + '</h1>'
if data['status page']['status']['assets']['enabled']:
if file['valid assets checksum']:
if file['valid_assets_checksum']:
content += '<h1>' + data['status page']['status']['assets']['message'] + ' ' + data['status page']['status']['assets']['valid value'] + '</h1>'
else:
content += '<h1>' + data['status page']['status']['assets']['message'] + ' ' + data['status page']['status']['assets']['invalid value'] + '</h1>'
@ -582,12 +582,12 @@ def get_status_page(file: dict, data: dict):
print_file(data['print']['printer'], status_page_tmp_path, 'status page',
{'media': 'a4'})
if data['status page']['file']['store']:
dir = pathlib.Path(file['invoice file']).parent
dir = pathlib.Path(file['invoice_file']).parent
shutil.move(
status_page_tmp_path,
str(
pathlib.Path(dir,
file['invoice file'] + '_status_page.pdf')))
file['invoice_file'] + '_status_page.pdf')))
if __name__ == '__main__':
@ -609,7 +609,7 @@ if __name__ == '__main__':
get_status_page(f, config)
if config['notify']['gotify']['enabled']:
message = 'processed invoice = ' + pathlib.Path(
f['invoice file']).name
f['invoice_file']).name
fpyutils.send_gotify_message(config['notify']['gotify']['url'],
config['notify']['gotify']['token'],
message,

2
docs/scripts.rst

@ -318,7 +318,7 @@ Steps
::
pip3 install wheel
pip3 install requests==2.26 fpyutils==2.1.0 python-dateutil fattura-elettronica-reader==2.1.0 WeasyPrint==52.1 pycups==2.0.1 lxm
pip3 install requests==2.26 fpyutils==2.1.0 python-dateutil fattura-elettronica-reader==3.0.0 WeasyPrint==52.1 pycups==2.0.1 lxm
3. optionally run common command 1

Loading…
Cancel
Save