def process_transaction(date, json_formatted_data, line, transaction_regex, date_change_regex, desc_regex, ignorable_regexes): transaction_pattern = re.compile(transaction_regex) date_change_pattern = re.compile(date_change_regex) desc_pattern = re.compile(desc_regex) ignorable_patterns = [ re.compile(ignorable_regex) for ignorable_regex in ignorable_regexes ] m = transaction_pattern.match(line) if date_change_pattern.match(line): m = date_change_pattern.match(line) date = m.group(constants.DATE_STR) if transaction_pattern.match(line): m = transaction_pattern.match(line) opening_balance = bsr_utils.get_opening_balance( json_formatted_data) transaction_type = bsr_utils.get_transaction_type( opening_balance, bsr_utils.pretty_format(m.group( constants.CLOSING_BALANCE_STR))) json_formatted_data[constants.TRANSACTIONS_STR].append({ constants.DATE_STR: bsr_utils.pretty_format(date), constants.DESCRIPTION_STR: bsr_utils.pretty_format(m.group(constants.DESCRIPTION_STR)), constants.TYPE_STR: transaction_type, constants.AMOUNT_STR: bsr_utils.pretty_format(m.group(constants.AMOUNT_STR)), constants.CLOSING_BALANCE_STR: bsr_utils.pretty_format(m.group(constants.CLOSING_BALANCE_STR)) }) elif constants.OPENING_BALANCE_STR not in json_formatted_data: json_formatted_data[ constants.OPENING_BALANCE_STR] = bsr_utils.pretty_format( m.group(constants.AMOUNT_STR)) elif transaction_pattern.match(line): opening_balance = bsr_utils.get_opening_balance(json_formatted_data) transaction_type = bsr_utils.get_transaction_type( opening_balance, bsr_utils.pretty_format(m.group(constants.CLOSING_BALANCE_STR))) json_formatted_data[constants.TRANSACTIONS_STR].append({ constants.DATE_STR: bsr_utils.pretty_format(date), constants.DESCRIPTION_STR: bsr_utils.pretty_format(m.group(constants.DESCRIPTION_STR)), constants.TYPE_STR: transaction_type, constants.AMOUNT_STR: bsr_utils.pretty_format(m.group(constants.AMOUNT_STR)), constants.CLOSING_BALANCE_STR: bsr_utils.pretty_format(m.group(constants.CLOSING_BALANCE_STR)) }) elif bsr_utils.is_ignorable(ignorable_patterns, line): pass elif desc_pattern.match(line): process_desc(json_formatted_data, desc_pattern, line) # print json_formatted_data return date
def process_transaction(json_formatted_data, line, transaction_regex, desc_regex): transaction_pattern = re.compile(transaction_regex) desc_pattern = re.compile(desc_regex) m = transaction_pattern.match(line) if transaction_pattern.match(line): opening_balance = bsr_utils.get_opening_balance(json_formatted_data) transaction_type = bsr_utils.get_transaction_type( opening_balance, bsr_utils.pretty_format(m.group(constants.CLOSING_BALANCE_STR))) json_formatted_data[constants.TRANSACTIONS_STR].append({ constants.DATE_STR: bsr_utils.pretty_format(m.group(constants.DATE_STR)), constants.DESCRIPTION_STR: bsr_utils.pretty_format(m.group(constants.DESCRIPTION_STR)), constants.TYPE_STR: transaction_type, constants.AMOUNT_STR: bsr_utils.pretty_format(m.group(constants.AMOUNT_STR)), constants.CLOSING_BALANCE_STR: bsr_utils.pretty_format(m.group(constants.CLOSING_BALANCE_STR)) }) elif desc_pattern.match(line): process_desc(json_formatted_data, desc_pattern, line)
def extract(_file, password): header_pattern = re.compile(constants.BANDHAN_BANK_HEADER_REGEX) file_content = bsr_utils.get_file_content(_file, password) json_formatted_data = { constants.TRANSACTIONS_STR: [] } is_transaction_started = False acc_details = '' i = 0 existing_desc = '' while i < len(file_content): line = file_content[i] if is_transaction_started: existing_desc = process_transaction(json_formatted_data, line, existing_desc, constants.BANDHAN_BANK_TRANSACTION_REGEX_TWO, constants.BANDHAN_BANK_DESC_REGEX) elif header_pattern.match(line): is_transaction_started = True bsr_utils.put_acc_details(json_formatted_data, acc_details, constants.BANDHAN_BANK_ACCOUNT_DETAILS_REGEX_TWO) else: acc_details += line + '\n' i = i + 1 j = len(json_formatted_data[constants.TRANSACTIONS_STR]) - 1 while j >= 0: opening_balance = bsr_utils.get_rev_opening_balance(j, json_formatted_data) transaction_type = bsr_utils.get_transaction_type(opening_balance, json_formatted_data[constants.TRANSACTIONS_STR][j][ constants.CLOSING_BALANCE_STR]) json_formatted_data[constants.TRANSACTIONS_STR][j][constants.TRANSACTION_TYPE_STR] = transaction_type j -= 1 return json_formatted_data
def process_transaction(json_formatted_data, line, existing_desc, transaction_regex, desc_regex, ignorable_regexes): transaction_pattern = re.compile(transaction_regex) desc_pattern = re.compile(desc_regex) ignorable_patterns = [ re.compile(ignorable_regex) for ignorable_regex in ignorable_regexes ] m = transaction_pattern.match(line) if transaction_pattern.match(line): opening_balance = bsr_utils.get_opening_balance(json_formatted_data) transaction_type = bsr_utils.get_transaction_type( opening_balance, bsr_utils.pretty_format(m.group(constants.CLOSING_BALANCE_STR))) json_formatted_data[constants.TRANSACTIONS_STR].append({ constants.DATE_STR: bsr_utils.pretty_format(m.group(constants.DATE_STR)), constants.DESCRIPTION_STR: existing_desc + bsr_utils.pretty_format(m.group(constants.DESCRIPTION_STR)), constants.TYPE_STR: transaction_type, constants.AMOUNT_STR: bsr_utils.pretty_format(m.group(constants.AMOUNT_STR)), constants.CLOSING_BALANCE_STR: bsr_utils.pretty_format(m.group(constants.CLOSING_BALANCE_STR)) }) existing_desc = '' elif bsr_utils.is_ignorable(ignorable_patterns, line): pass elif desc_pattern.match(line): existing_desc = process_desc(desc_pattern, line, existing_desc) return existing_desc
def process_transaction(json_formatted_data, line, existing_desc, transaction_regex, desc_regex, ignorable_regexes): transaction_pattern = re.compile(transaction_regex) desc_pattern = re.compile(desc_regex) ignorable_patterns = [re.compile(ignorable_regex) for ignorable_regex in ignorable_regexes] m = transaction_pattern.match(line) if transaction_pattern.match(line): opening_balance = bsr_utils.get_opening_balance(json_formatted_data) withdraw = float(str(bsr_utils.pretty_format(m.group(constants.WITHDRAW_AMOUNT_STR))).replace(',', '')) deposit = float(str(bsr_utils.pretty_format(m.group(constants.DEPOSIT_AMOUNT_STR)).replace(',', ''))) amount = withdraw + deposit transaction_type = bsr_utils.get_transaction_type(opening_balance, bsr_utils.pretty_format( m.group(constants.CLOSING_BALANCE_STR))) json_formatted_data[constants.TRANSACTIONS_STR].append({ constants.DATE_STR: bsr_utils.pretty_format(m.group(constants.DATE_STR)), constants.DESCRIPTION_STR: existing_desc + bsr_utils.pretty_format(m.group(constants.DESCRIPTION_STR)), constants.TYPE_STR: transaction_type, constants.AMOUNT_STR: amount, constants.CLOSING_BALANCE_STR: bsr_utils.pretty_format(m.group(constants.CLOSING_BALANCE_STR)) }) existing_desc = '' elif bsr_utils.is_ignorable(ignorable_patterns, line): pass elif desc_pattern.match(line): existing_desc = process_desc_custum(desc_pattern, line, existing_desc) return existing_desc
def extract(_file, password): header_pattern = re.compile(constants.CANARA_BANK_HEADER_REGEX) file_content = bsr_utils.get_file_content(_file, password) json_formatted_data = {constants.TRANSACTIONS_STR: []} is_transaction_started = False acc_details = '' if file_content == 'wrongpassword': return 'wrongpassword' elif file_content == 'pdfnotreadable': return 'pdfnotreadable' for line in file_content: if is_transaction_started: process_transaction(json_formatted_data, line, constants.CANARA_BANK_TRANSACTION_REGEX, constants.CANARA_BANK_DESC_REGEX, constants.CANARA_BANK_IGNORABLE_REGEXS) elif header_pattern.match(line): is_transaction_started = True bsr_utils.put_acc_details( json_formatted_data, acc_details, constants.CANARA_BANK_ACCOUNT_DETAILS_REGEX) else: acc_details += line + '\n' j = len(json_formatted_data[constants.TRANSACTIONS_STR]) - 1 while j >= 0: opening_balance = bsr_utils.get_rev_opening_balance( j, json_formatted_data) transaction_type = bsr_utils.get_transaction_type( opening_balance, json_formatted_data[constants.TRANSACTIONS_STR][j] [constants.CLOSING_BALANCE_STR]) json_formatted_data[constants.TRANSACTIONS_STR][j][ constants.TRANSACTION_TYPE_STR] = transaction_type j -= 1 return json_formatted_data
def extract(_file, password): header_pattern = re.compile(constants.KOTAK_BANK_HEADER_REGEX_TWO) file_end_pattern = re.compile(constants.KOTAK_BANK_STATEMENT_END_REGEX_TWO) file_content = bsr_utils.get_file_content(_file, password) json_formatted_data = {constants.TRANSACTIONS_STR: []} is_transaction_started = False acc_details = '' line_index = len(file_content) - 1 while line_index > 0: line = file_content[line_index] if file_end_pattern.match(line): line = file_content[line_index] json_formatted_data.update( {constants.OPENING_BALANCE_STR: line.split()[-1]}) break line_index -= 1 line_index = 0 if file_content == 'wrongpassword': return 'wrongpassword' elif file_content == 'pdfnotreadable': return 'pdfnotreadable' while line_index < len(file_content): line = file_content[line_index] if file_end_pattern.match(line): break elif is_transaction_started: process_transaction(json_formatted_data, line, constants.KOTAK_BANK_TRANSACTION_REGEX_TWO, constants.KOTAK_BANK_DESC_REGEX, constants.KOTAK_BANK_IGNORABLE_REGEXS_TWO) elif header_pattern.match(line): is_transaction_started = True bsr_utils.put_acc_details( json_formatted_data, acc_details, constants.KOTAK_BANK_ACCOUNT_DETAILS_REGEX_TWO) line_index -= 1 else: acc_details += line + '\n' line_index += 1 j = len(json_formatted_data[constants.TRANSACTIONS_STR]) - 1 while j >= 0: opening_balance = bsr_utils.get_rev_opening_balance( j, json_formatted_data) transaction_type = bsr_utils.get_transaction_type( opening_balance, json_formatted_data[constants.TRANSACTIONS_STR][j] [constants.CLOSING_BALANCE_STR]) json_formatted_data[constants.TRANSACTIONS_STR][j][ constants.TRANSACTION_TYPE_STR] = transaction_type j -= 1 return json_formatted_data
def extract(_file, password): header_pattern = re.compile(constants.RBL_BANK_HEADER_REGEX) file_end_pattern = re.compile(constants.RBL_BANK_STATEMENT_END_REGEX) file_content = bsr_utils.get_file_content(_file, password) json_formatted_data = {constants.TRANSACTIONS_STR: []} is_transaction_started = False acc_details = '' i = len(file_content) - 1 while i > 0: if 'Opening Balance:' in file_content[i]: json_formatted_data[ constants.OPENING_BALANCE_STR] = file_content[i].split()[3] break i -= 1 i = 0 if file_content == 'wrongpassword': return 'wrongpassword' elif file_content == 'pdfnotreadable': return 'pdfnotreadable' while i < len(file_content): line = file_content[i] if file_end_pattern.match(line): break elif is_transaction_started: process_transaction(json_formatted_data, i, file_content, constants.RBL_BANK_TRANSACTION_REGEX_1, constants.RBL_BANK_TRANSACTION_REGEX_2, constants.RBL_BANK_DESC_REGEX, constants.RBL_BANK_IGNORABLE_REGEXS) elif header_pattern.match(line): is_transaction_started = True bsr_utils.put_acc_details(json_formatted_data, acc_details, constants.RBL_BANK_ACCOUNT_DETAILS_REGEX) if "name" not in json_formatted_data: bsr_utils.put_acc_details( json_formatted_data, acc_details, constants.RBL_BANK_ACCOUNT_DETAILS_REGEX_TWO) else: acc_details += line + '\n' i += 1 j = len(json_formatted_data[constants.TRANSACTIONS_STR]) - 1 while j >= 0: opening_balance = bsr_utils.get_rev_opening_balance( j, json_formatted_data) transaction_type = bsr_utils.get_transaction_type( opening_balance, json_formatted_data[constants.TRANSACTIONS_STR][j] [constants.CLOSING_BALANCE_STR]) json_formatted_data[constants.TRANSACTIONS_STR][j][ constants.TRANSACTION_TYPE_STR] = transaction_type j -= 1 return json_formatted_data
def process_transaction(json_formatted_data, i, existing_desc, transaction_regex, file_content, desc_regex, ignorable_regexes): line = file_content[i] return_statement = False Description_value = '' transaction_pattern = re.compile(transaction_regex) desc_pattern = re.compile(desc_regex) ignorable_patterns = [ re.compile(ignorable_regex) for ignorable_regex in ignorable_regexes ] m = transaction_pattern.match(line) if transaction_pattern.match(line): if (len(bsr_utils.pretty_format(m.group(constants.DATE_STR))) > 0) and len( bsr_utils.pretty_format(m.group( constants.DESCRIPTION_STR))) > 0: Description_value = bsr_utils.pretty_format( m.group(constants.DESCRIPTION_STR)) else: Description_value = '' return_statement = True opening_balance = bsr_utils.get_opening_balance(json_formatted_data) transaction_type = bsr_utils.get_transaction_type( opening_balance, bsr_utils.pretty_format(m.group(constants.CLOSING_BALANCE_STR))) json_formatted_data[constants.TRANSACTIONS_STR].append({ constants.DATE_STR: bsr_utils.pretty_format(m.group(constants.DATE_STR)), # constants.DESCRIPTION_STR: bsr_utils.pretty_format(m.group(constants.DESCRIPTION_STR)), constants.DESCRIPTION_STR: Description_value, constants.TYPE_STR: transaction_type, constants.AMOUNT_STR: bsr_utils.pretty_format(m.group(constants.AMOUNT_STR)), constants.CLOSING_BALANCE_STR: bsr_utils.pretty_format(m.group(constants.CLOSING_BALANCE_STR)) }) if return_statement: return existing_desc elif bsr_utils.is_ignorable(ignorable_patterns, line): pass elif desc_pattern.match(line): if existing_desc is None or existing_desc == '': existing_desc = existing_desc + line else: process_desc_custum(json_formatted_data, desc_pattern, line, existing_desc) existing_desc = '' return existing_desc
def extract(_file, password): header_pattern = re.compile(constants.PNB_BANK_HEADER_REGEX) file_end_pattern = re.compile(constants.PNB_BANK_STATEMENT_END_REGEX) file_content = bsr_utils.get_file_content(_file, password) json_formatted_data = { constants.TRANSACTIONS_STR: [] } is_transaction_started = False acc_details = '' i = 0 if file_content == 'wrongpassword': return 'wrongpassword' elif file_content == 'pdfnotreadable': return 'pdfnotreadable' while i < len(file_content): line = file_content[i] if file_end_pattern.match(line): break elif is_transaction_started: process_transaction(json_formatted_data, i, constants.PNB_BANK_TRANSACTION_REGEX, file_content) elif header_pattern.match(line): is_transaction_started = True bsr_utils.put_acc_details(json_formatted_data, acc_details, constants.PNB_BANK_ACCOUNT_DETAILS_REGEX) else: acc_details += line + '\n' i += 1 j = len(json_formatted_data[constants.TRANSACTIONS_STR]) - 2 while j >= 0: opening_balance = bsr_utils.get_rev_opening_balance(j, json_formatted_data) transaction_type = bsr_utils.get_transaction_type(opening_balance, json_formatted_data[constants.TRANSACTIONS_STR][j][ constants.CLOSING_BALANCE_STR]) json_formatted_data[constants.TRANSACTIONS_STR][j][constants.TRANSACTION_TYPE_STR] = transaction_type j -= 1 json_formatted_data[constants.NAME_STR] = json_formatted_data[constants.NAME_STR].split(' SO ')[0] return json_formatted_data