Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testEmptyBalance(self):
''' The test file contains empty or blank strings in the balance
fields. Fail nicely on those.
'''
with open_file('fail_nice/empty_balance.ofx') as f:
ofx = OfxParser.parse(f, False)
self.assertEqual(len(ofx.account.statement.transactions), 1)
self.assertEqual(len(ofx.account.statement.discarded_entries), 0)
self.assertFalse(hasattr(ofx.account.statement, 'balance'))
self.assertFalse(hasattr(ofx.account.statement, 'available_balance'))
# Test that it raises an error otherwise.
with open_file('fail_nice/empty_balance.ofx') as f:
self.assertRaises(OfxParserException, OfxParser.parse, f)
raise OfxParserException(
six.u("Invalid Transaction Amount: '%s'") % amt_tag.contents[0])
except TypeError:
raise OfxParserException(
six.u("No Transaction Amount (a required field)"))
else:
raise OfxParserException(
six.u("Missing Transaction Amount (a required field)"))
date_tag = txn_ofx.find('dtposted')
if hasattr(date_tag, "contents"):
try:
transaction.date = cls.parseOfxDateTime(
date_tag.contents[0].strip())
except IndexError:
raise OfxParserException("Invalid Transaction Date")
except ValueError:
ve = sys.exc_info()[1]
raise OfxParserException(str(ve))
except TypeError:
raise OfxParserException(
six.u("No Transaction Date (a required field)"))
else:
raise OfxParserException(
six.u("Missing Transaction Date (a required field)"))
user_date_tag = txn_ofx.find('dtuser')
if hasattr(user_date_tag, "contents"):
try:
transaction.user_date = cls.parseOfxDateTime(
user_date_tag.contents[0].strip())
except IndexError:
def validate_ofx(self, file):
try:
ofx = OfxParser.parse(file)
except (TypeError, AttributeError, OfxParserException, ValueError):
raise UserError("No se pudo interpretar el archivo dado!")
return ofx
type_tag = txn_ofx.find('trntype')
if hasattr(type_tag, 'contents'):
try:
transaction.type = type_tag.contents[0].lower().strip()
except IndexError:
raise OfxParserException(six.u("Empty transaction type"))
except TypeError:
raise OfxParserException(
six.u("No Transaction type (a required field)"))
name_tag = txn_ofx.find('name')
if hasattr(name_tag, "contents"):
try:
transaction.payee = name_tag.contents[0].strip()
except IndexError:
raise OfxParserException(six.u("Empty transaction name"))
except TypeError:
raise OfxParserException(
six.u("No Transaction name (a required field)"))
memo_tag = txn_ofx.find('memo')
if hasattr(memo_tag, "contents"):
try:
transaction.memo = memo_tag.contents[0].strip()
except IndexError:
# Memo can be empty.
pass
except TypeError:
pass
amt_tag = txn_ofx.find('trnamt')
if hasattr(amt_tag, "contents"):
ve = sys.exc_info()[1]
raise OfxParserException(str(ve))
except TypeError:
raise OfxParserException(
six.u("No Transaction Date (a required field)"))
else:
raise OfxParserException(
six.u("Missing Transaction Date (a required field)"))
user_date_tag = txn_ofx.find('dtuser')
if hasattr(user_date_tag, "contents"):
try:
transaction.user_date = cls.parseOfxDateTime(
user_date_tag.contents[0].strip())
except IndexError:
raise OfxParserException("Invalid Transaction User Date")
except ValueError:
ve = sys.exc_info()[1]
raise OfxParserException(str(ve))
except TypeError:
pass
id_tag = txn_ofx.find('fitid')
if hasattr(id_tag, "contents"):
try:
transaction.id = id_tag.contents[0].strip()
except IndexError:
raise OfxParserException(six.u("Empty FIT id (a required \
field)"))
except TypeError:
raise OfxParserException(six.u("No FIT id (a required field)"))
else:
statement.warnings.append(
six.u("Currency definition was empty for %s") % stmt_ofx)
if cls.fail_fast:
raise
cls.parseBalance(statement, stmt_ofx, 'ledgerbal',
'balance', 'balance_date', 'ledger')
cls.parseBalance(statement, stmt_ofx, 'availbal', 'available_balance',
'available_balance_date', 'ledger')
for transaction_ofx in stmt_ofx.findAll('stmttrn'):
try:
statement.transactions.append(
cls.parseTransaction(transaction_ofx))
except OfxParserException:
ofxError = sys.exc_info()[1]
statement.discarded_entries.append(
{'error': str(ofxError), 'content': transaction_ofx})
if cls.fail_fast:
raise
return statement
def parseBalance(cls, statement, stmt_ofx, bal_tag_name, bal_attr,
bal_date_attr, bal_type_string):
bal_tag = stmt_ofx.find(bal_tag_name)
if hasattr(bal_tag, "contents"):
balamt_tag = bal_tag.find('balamt')
dtasof_tag = bal_tag.find('dtasof')
if hasattr(balamt_tag, "contents"):
try:
setattr(statement, bal_attr, cls.toDecimal(balamt_tag))
except (IndexError, decimal.InvalidOperation):
statement.warnings.append(
six.u("%s balance amount was empty for \
%s") % (bal_type_string, stmt_ofx))
if cls.fail_fast:
raise OfxParserException("Empty %s balance\
" % bal_type_string)
if hasattr(dtasof_tag, "contents"):
try:
setattr(statement, bal_date_attr, cls.parseOfxDateTime(
dtasof_tag.contents[0].strip()))
except IndexError:
statement.warnings.append(
six.u("%s balance date was empty for %s\
") % (bal_type_string, stmt_ofx))
if cls.fail_fast:
raise
except ValueError:
statement.warnings.append(
six.u("%s balance date was not allowed for \
%s") % (bal_type_string, stmt_ofx))
if cls.fail_fast:
if not hasattr(file_handle, 'seek'):
raise TypeError(six.u('parse() accepts a seek-able file handle\
, not %s' % type(file_handle).__name__))
ofx_obj = Ofx()
# Store the headers
ofx_file = OfxPreprocessedFile(file_handle)
ofx_obj.headers = ofx_file.headers
ofx_obj.accounts = []
ofx_obj.signon = None
ofx = soup_maker(ofx_file.fh)
if ofx.find('ofx') is None:
raise OfxParserException('The ofx file is empty!')
sonrs_ofx = ofx.find('sonrs')
if sonrs_ofx:
ofx_obj.signon = cls.parseSonrs(sonrs_ofx)
stmttrnrs = ofx.find('stmttrnrs')
if stmttrnrs:
stmttrnrs_trnuid = stmttrnrs.find('trnuid')
if stmttrnrs_trnuid:
ofx_obj.trnuid = stmttrnrs_trnuid.contents[0].strip()
stmttrnrs_status = stmttrnrs.find('status')
if stmttrnrs_status:
ofx_obj.status = {}
ofx_obj.status['code'] = int(
stmttrnrs_status.find('code').contents[0].strip()
raise OfxParserException(
six.u("No Transaction Amount (a required field)"))
else:
raise OfxParserException(
six.u("Missing Transaction Amount (a required field)"))
date_tag = txn_ofx.find('dtposted')
if hasattr(date_tag, "contents"):
try:
transaction.date = cls.parseOfxDateTime(
date_tag.contents[0].strip())
except IndexError:
raise OfxParserException("Invalid Transaction Date")
except ValueError:
ve = sys.exc_info()[1]
raise OfxParserException(str(ve))
except TypeError:
raise OfxParserException(
six.u("No Transaction Date (a required field)"))
else:
raise OfxParserException(
six.u("Missing Transaction Date (a required field)"))
user_date_tag = txn_ofx.find('dtuser')
if hasattr(user_date_tag, "contents"):
try:
transaction.user_date = cls.parseOfxDateTime(
user_date_tag.contents[0].strip())
except IndexError:
raise OfxParserException("Invalid Transaction User Date")
except ValueError:
ve = sys.exc_info()[1]
except TypeError:
pass
amt_tag = txn_ofx.find('trnamt')
if hasattr(amt_tag, "contents"):
try:
transaction.amount = cls.toDecimal(amt_tag)
except IndexError:
raise OfxParserException("Invalid Transaction Date")
except decimal.InvalidOperation:
# Some banks use a null transaction for including interest
# rate changes on your statement.
if amt_tag.contents[0].strip() in ('null', '-null'):
transaction.amount = 0
else:
raise OfxParserException(
six.u("Invalid Transaction Amount: '%s'") % amt_tag.contents[0])
except TypeError:
raise OfxParserException(
six.u("No Transaction Amount (a required field)"))
else:
raise OfxParserException(
six.u("Missing Transaction Amount (a required field)"))
date_tag = txn_ofx.find('dtposted')
if hasattr(date_tag, "contents"):
try:
transaction.date = cls.parseOfxDateTime(
date_tag.contents[0].strip())
except IndexError:
raise OfxParserException("Invalid Transaction Date")
except ValueError: