From c45e47d54e4990a7e0d26913b933cc218c9659e2 Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Wed, 2 Jul 2014 13:37:36 +0200 Subject: [PATCH] Improvement on the account_statement_base_import module to have profile data stored on a variable across parsers methods, in order to be used to customize, for example, parsing with a parameter introduced on profiles. Dependent modules have been changed accordingly. --- .../parser/parser.py | 20 ++++++++++--------- account_statement_base_import/statement.py | 2 +- .../parser/ofx_parser.py | 9 +-------- .../parser/transactionid_file_parser.py | 6 +++--- 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/account_statement_base_import/parser/parser.py b/account_statement_base_import/parser/parser.py index da01e6c8..929799c7 100644 --- a/account_statement_base_import/parser/parser.py +++ b/account_statement_base_import/parser/parser.py @@ -42,14 +42,16 @@ class BankStatementImportParser(object): from the FileParser instead. """ - def __init__(self, parser_name, *args, **kwargs): + def __init__(self, profile, *args, **kwargs): # The name of the parser as it will be called - self.parser_name = parser_name + self.parser_name = profile.import_type # The result as a list of row. One row per line of data in the file, but # not the commission one ! self.result_row_list = None # The file buffer on which to work on self.filebuffer = None + # The profile record to access its parameters in any parser method + self.profile = profile self.balance_start = None self.balance_end = None self.statement_name = None @@ -217,13 +219,13 @@ def itersubclasses(cls, _seen=None): yield sub -def new_bank_statement_parser(parser_name, *args, **kwargs): - """ - Return an instance of the good parser class base on the providen name - :param char: parser_name - :return: class instance of parser_name providen. +def new_bank_statement_parser(profile, *args, **kwargs): + """Return an instance of the good parser class based on the given profile. + + :param profile: browse_record of import profile. + :return: class instance for given profile import type. """ for cls in itersubclasses(BankStatementImportParser): - if cls.parser_for(parser_name): - return cls(parser_name, *args, **kwargs) + if cls.parser_for(profile.import_type): + return cls(profile, *args, **kwargs) raise ValueError diff --git a/account_statement_base_import/statement.py b/account_statement_base_import/statement.py index d3e0ea7e..b1497f48 100644 --- a/account_statement_base_import/statement.py +++ b/account_statement_base_import/statement.py @@ -153,7 +153,7 @@ class AccountStatementProfil(Model): _("You must provide a valid profile to import a bank statement!")) prof = prof_obj.browse(cr, uid, profile_id, context=context) - parser = new_bank_statement_parser(prof.import_type, ftype=ftype) + parser = new_bank_statement_parser(prof, ftype=ftype) res = [] for result_row_list in parser.parse(file_stream): statement_id = self._statement_import(cr, uid, ids, prof, parser, diff --git a/account_statement_ofx_import/parser/ofx_parser.py b/account_statement_ofx_import/parser/ofx_parser.py index 5b86035d..6dc7947d 100644 --- a/account_statement_ofx_import/parser/ofx_parser.py +++ b/account_statement_ofx_import/parser/ofx_parser.py @@ -31,14 +31,7 @@ except: raise Exception(_('Please install python lib ofxparse')) class OfxParser(BankStatementImportParser): - """ - Class for defining parser for OFX file format. - """ - - def __init__(self, parser_name, *args, **kwargs): - """ - """ - super(OfxParser, self).__init__(parser_name, *args, **kwargs) + """Class for defining parser for OFX file format.""" @classmethod def parser_for(cls, parser_name): diff --git a/account_statement_transactionid_import/parser/transactionid_file_parser.py b/account_statement_transactionid_import/parser/transactionid_file_parser.py index 0a54b1e5..091f8ada 100644 --- a/account_statement_transactionid_import/parser/transactionid_file_parser.py +++ b/account_statement_transactionid_import/parser/transactionid_file_parser.py @@ -27,17 +27,17 @@ class TransactionIDFileParser(FileParser): bank statement. """ - def __init__(self, parse_name, ftype='csv', extra_fields=None, header=None, **kwargs): + def __init__(self, profile, ftype='csv', extra_fields=None, header=None, **kwargs): """ Add transaction_id in header keys - :param char: parse_name: The name of the parser + :param char: profile: Reference to the profile :param char: ftype: extension of the file (could be csv or xls) :param dict: extra_fields: extra fields to add to the conversion dict. In the format {fieldname: fieldtype} :param list: header : specify header fields if the csv file has no header """ extra_fields = {'transaction_id': unicode} - super(TransactionIDFileParser, self).__init__(parse_name, extra_fields=extra_fields, + super(TransactionIDFileParser, self).__init__(profile, extra_fields=extra_fields, ftype=ftype, header=header, **kwargs) # ref is replaced by transaction_id thus we delete it from check self.keys_to_validate = [k for k in self.keys_to_validate if k != 'ref']