From d005ec45b0207a88172e480ac8dc0dc7823332e9 Mon Sep 17 00:00:00 2001 From: Graeme Gellatly Date: Sun, 2 Feb 2014 22:27:27 +1300 Subject: [PATCH] ofxparse update --- .../parser/ofx_parser.py | 60 ++++++++++++------- account_statement_ofx_import/statement.py | 10 ++-- 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/account_statement_ofx_import/parser/ofx_parser.py b/account_statement_ofx_import/parser/ofx_parser.py index 5b86035d..f2a75551 100644 --- a/account_statement_ofx_import/parser/ofx_parser.py +++ b/account_statement_ofx_import/parser/ofx_parser.py @@ -34,7 +34,7 @@ class OfxParser(BankStatementImportParser): """ Class for defining parser for OFX file format. """ - + def __init__(self, parser_name, *args, **kwargs): """ """ @@ -60,25 +60,23 @@ class OfxParser(BankStatementImportParser): """ return True + def _prepare_lines(self, transactions): + return [{'date': transaction.date, + 'amount': transaction.amount, + 'ref': transaction.type, + 'label': transaction.payee} for transaction in transactions] + def _parse(self, *args, **kwargs): """ Launch the parsing itself. """ - ofx_file = tempfile.NamedTemporaryFile() - ofx_file.seek(0) - ofx_file.write(self.filebuffer) - ofx_file.flush() - ofx = ofxparse.OfxParser.parse(file(ofx_file.name)) - ofx_file.close() - res = [] - for transaction in ofx.account.statement.transactions: - res.append({ - 'date': transaction.date, - 'amount': transaction.amount, - 'ref': transaction.type, - 'label': transaction.payee, - }) - self.result_row_list = res + with tempfile.NamedTemporaryFile() as ofx_file: + ofx_file.seek(0) + ofx_file.write(self.filebuffer) + ofx_file.flush() + ofx = ofxparse.OfxParser.parse(file(ofx_file.name)) + ofx_file.close() + self.result_row_list = self._prepare_lines(ofx.account.statement.transactions) return True def _validate(self, *args, **kwargs): @@ -93,12 +91,6 @@ class OfxParser(BankStatementImportParser): """ return True - def _post(self, *args, **kwargs): - """ - Nothing to do. - """ - return True - def get_st_line_vals(self, line, *args, **kwargs): """ This method must return a dict of vals that can be passed to create @@ -118,3 +110,27 @@ class OfxParser(BankStatementImportParser): 'label': line.get('label', ''), } +class OfxParser2(OfxParser): + """ + Class for defining parser for OFX file format. Only differs from above + by setting ref to be the memo rather than type field + """ + + def __init__(self, parser_name, *args, **kwargs): + """ + """ + super(OfxParser2, self).__init__(parser_name, *args, **kwargs) + + @classmethod + def parser_for(cls, parser_name): + """ + Used by the new_bank_statement_parser class factory. Return true if + the providen name is 'ofx_alt'. + """ + return parser_name == 'ofx_alt' + + def _prepare_lines(self, transactions): + return [{'date': transaction.date, + 'amount': transaction.amount, + 'ref': transaction.memo, + 'label': transaction.payee} for transaction in transactions] diff --git a/account_statement_ofx_import/statement.py b/account_statement_ofx_import/statement.py index c5b06fe8..b182882a 100644 --- a/account_statement_ofx_import/statement.py +++ b/account_statement_ofx_import/statement.py @@ -21,17 +21,17 @@ from openerp.tools.translate import _ from openerp.osv import fields, orm -class AccountStatementProfil(orm.Model): +class AccountStatementProfile(orm.Model): _inherit = "account.statement.profile" def get_import_type_selection(self, cr, uid, context=None): """ Inherited from parent to add parser. """ - selection = super(AccountStatementProfil, self - ).get_import_type_selection(cr, uid, - context=context) - selection.append(('ofx_so', _('OFX - Open Financial Exchange'))) + selection = super(AccountStatementProfile, self + ).get_import_type_selection(cr, uid, context=context) + selection.extend([('ofx_so', _('OFX - Open Financial Exchange')), + ('ofx_alt', _('OFX - (memo rather than type in ref field)'))]) return selection _columns = {