You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.5 KiB

  1. # Copyright 2013-2016 Therp BV <https://therp.nl>
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  3. import logging
  4. from io import BytesIO
  5. import zipfile
  6. from odoo import api, models
  7. _logger = logging.getLogger(__name__)
  8. class AccountBankStatementImport(models.TransientModel):
  9. _inherit = 'account.bank.statement.import'
  10. @api.model
  11. def _parse_file(self, data_file):
  12. """Parse a CAMT053 XML file."""
  13. try:
  14. parser = self.env['account.bank.statement.import.camt.parser']
  15. _logger.debug("Try parsing with camt.")
  16. return parser.parse(data_file)
  17. except ValueError:
  18. try:
  19. with zipfile.ZipFile(BytesIO(data_file)) as data:
  20. currency = None
  21. account_number = None
  22. transactions = []
  23. for member in data.namelist():
  24. currency, account_number, new = self._parse_file(
  25. data.open(member).read()
  26. )
  27. transactions.extend(new)
  28. return currency, account_number, transactions
  29. except (zipfile.BadZipFile, ValueError):
  30. pass
  31. # Not a camt file, returning super will call next candidate:
  32. _logger.debug("Statement file was not a camt file.",
  33. exc_info=True)
  34. return super(AccountBankStatementImport, self)._parse_file(data_file)