Skip to content

Commit

Permalink
PEP8
Browse files Browse the repository at this point in the history
  • Loading branch information
lmignon committed Jul 10, 2014
1 parent 69ec9d8 commit d47c6e5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 30 deletions.
68 changes: 46 additions & 22 deletions coda/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ def parse(self, value):
if not line:
pass
elif line[0] == '0':
self.__fixes_not_closed_globalisation(statement, globalisation_stack)
self.__fixes_not_closed_globalisation(
statement, globalisation_stack)
# Begin of a new Bank statement
statement = Statement()
globalisation_stack = []
Expand Down Expand Up @@ -101,8 +102,10 @@ def __fixes_not_closed_globalisation(self, statement, globalisation_stack):
def _parseHeader(self, line, statement):
statement.version = version = line[127]
if version not in ['1', '2']:
raise CodaParserException(' R001', 'CODA V%s statements are not supported, please contact your bank' % statement['version'])
statement.creation_date = time.strftime(self.date_format, time.strptime(rmspaces(line[5:11]), '%d%m%y'))
raise CodaParserException(
' R001', 'CODA V%s statements are not supported, please contact your bank' % statement['version'])
statement.creation_date = time.strftime(
self.date_format, time.strptime(rmspaces(line[5:11]), '%d%m%y'))
statement.separate_application = rmspaces(line[83:88])

def _parseHeaderDetails(self, line, statement):
Expand All @@ -114,19 +117,23 @@ def _parseHeaderDetails(self, line, statement):
statement.acc_number = rmspaces(line[5:17])
statement.currency = rmspaces(line[18:21])
elif line[1] == '1': # foreign bank account BBAN structure
raise CodaParserException(' R1001', 'Foreign bank accounts with BBAN structure are not supported ')
raise CodaParserException(
' R1001', 'Foreign bank accounts with BBAN structure are not supported ')
elif line[1] == '2': # Belgian bank account IBAN structure
statement.acc_number = rmspaces(line[5:21])
statement.currency = rmspaces(line[39:42])
elif line[1] == '3': # foreign bank account IBAN structure
raise CodaParserException(' R1002', 'Foreign bank accounts with IBAN structure are not supported ')
raise CodaParserException(
' R1002', 'Foreign bank accounts with IBAN structure are not supported ')
else: # Something else, not supported
raise CodaParserException(' R1003', 'Unsupported bank account structure ')
raise CodaParserException(
' R1003', 'Unsupported bank account structure ')

statement.description = rmspaces(line[90:125])
statement.old_balance = float(rmspaces(line[43:58])) / 1000
statement.old_balance_amount_sign = line[42]
statement.old_balance_date = time.strftime(self.date_format, time.strptime(rmspaces(line[58:64]), '%d%m%y'))
statement.old_balance_date = time.strftime(
self.date_format, time.strptime(rmspaces(line[58:64]), '%d%m%y'))
statement.account_holder_name = rmspaces(line[64:90])
statement.paper_seq_number = rmspaces(line[2:5])
statement.coda_seq_number = rmspaces(line[125:128])
Expand All @@ -142,25 +149,30 @@ def _parseMovementRecord(self, line, statement, globalisation_stack):
record.transaction_amount_sign = line[31] # 0 = Credit, 1 = Debit
record.transaction_amount = float(rmspaces(line[32:47])) / 1000
record.transaction_type = line[53]
record.transaction_date = time.strftime(self.date_format, time.strptime(rmspaces(line[47:53]), '%d%m%y'))
record.transaction_date = time.strftime(
self.date_format, time.strptime(rmspaces(line[47:53]), '%d%m%y'))
record.transaction_family = rmspaces(line[54:56])
record.transaction_code = rmspaces(line[56:58])
record.transaction_category = rmspaces(line[58:61])
record.communication_is_structured = line[61] == '1'
if record.communication_is_structured:
# Structured communication
record.communication_type = line[62:65]
record.communication = '+++' + line[65:68] + '/' + line[68:72] + '/' + line[72:77] + '+++'
record.communication = '+++' + \
line[65:68] + '/' + line[68:72] + '/' + line[72:77] + '+++'
else:
# Non-structured communication
record.communication = rmspaces(line[62:115])
record.entry_date = time.strftime(self.date_format, time.strptime(rmspaces(line[115:121]), '%d%m%y'))
record.entry_date = time.strftime(
self.date_format, time.strptime(rmspaces(line[115:121]), '%d%m%y'))
record.type = MovementRecordType.NORMAL
record.globalisation_code = int(line[124])
if len(globalisation_stack) > 0 and record.communication != '':
record.communication = "\n".join(filter(None, [globalisation_stack[-1].communication, record.communication]))
record.communication = "\n".join(
filter(None, [globalisation_stack[-1].communication, record.communication]))
if record.globalisation_code > 0:
if len(globalisation_stack) > 0 and globalisation_stack[-1].globalisation_code == record.globalisation_code:
if len(globalisation_stack) > 0 and \
globalisation_stack[-1].globalisation_code == record.globalisation_code:
# Destack
globalisation_stack.pop()
else:
Expand All @@ -171,14 +183,19 @@ def _parseMovementRecord(self, line, statement, globalisation_stack):
elif line[1] == '2':
record = statement.movements[-1]
if record.ref[0:4] != line[2:6]:
raise CodaParserException('R2004', 'CODA parsing error on movement data record 2.2, seq nr %s! Please report this issue via your OpenERP support channel.' % line[2:10])
record.communication = join_communications(record.communication, rmspaces(line[10:63]))
raise CodaParserException(
'R2004', 'CODA parsing error on movement data record 2.2, seq nr %s!'
' Please report this issue via your OpenERP support channel.' % line[2:10])
record.communication = join_communications(
record.communication, rmspaces(line[10:63]))
record.payment_reference = rmspaces(line[63:98])
record.counterparty_bic = rmspaces(line[98:109])
elif line[1] == '3':
record = statement.movements[-1]
if record.ref[0:4] != line[2:6]:
raise CodaParserException('R2005', 'CODA parsing error on movement data record 2.3, seq nr %s! Please report this issue via your OpenERP support channel.' % line[2:10])
raise CodaParserException(
'R2005', 'CODA parsing error on movement data record 2.3, seq nr %s!'
' Please report this issue via your OpenERP support channel.' % line[2:10])
if statement.version == '1':
record.counterparty_number = rmspaces(line[10:22])
record.counterparty_name = rmspaces(line[47:73])
Expand All @@ -192,10 +209,12 @@ def _parseMovementRecord(self, line, statement, globalisation_stack):
record.counterparty_number = rmspaces(line[10:44])
record.counterparty_currency = rmspaces(line[44:47])
record.counterparty_name = rmspaces(line[47:82])
record.communication = join_communications(record.communication, rmspaces(line[82:125]))
record.communication = join_communications(
record.communication, rmspaces(line[82:125]))
else:
# movement data record 2.x (x != 1,2,3)
raise CodaParserException('R2006', '\nMovement data records of type 2.%s are not supported ' % line[1])
raise CodaParserException(
'R2006', '\nMovement data records of type 2.%s are not supported ' % line[1])

def _parseInformationRecord(self, line, statement):
if line[1] == '1':
Expand All @@ -211,19 +230,24 @@ def _parseInformationRecord(self, line, statement):
elif line[1] == '2':
infoLine = statement.informations[-1]
if infoLine.ref != rmspaces(line[2:10]):
raise CodaParserException('R3004', 'CODA parsing error on information data record 3.2, seq nr %s! Please report this issue via your OpenERP support channel.' % line[2:10])
raise CodaParserException(
'R3004', 'CODA parsing error on information data record 3.2, seq nr %s!'
' Please report this issue via your OpenERP support channel.' % line[2:10])
infoLine.communication += rmspaces(line[10:100])
elif line[1] == '3':
infoLine = statement.informations[-1]
if infoLine['ref'] != rmspaces(line[2:10]):
raise CodaParserException('R3005', 'CODA parsing error on information data record 3.3, seq nr %s! Please report this issue via your OpenERP support channel.' % line[2:10])
raise CodaParserException(
'R3005', 'CODA parsing error on information data record 3.3, seq nr %s!'
' Please report this issue via your OpenERP support channel.' % line[2:10])
infoLine.communication += rmspaces(line[10:100])

def _parseNewBalanceRecord(self, line, statement):
statement.new_balance_amount_sign = line[41]
statement.new_balance_paper_seq_number = rmspaces(line[1:4])
statement.new_balance = float(rmspaces(line[42:57])) / 1000
statement.new_balance_date = time.strftime(self.date_format, time.strptime(rmspaces(line[57:63]), '%d%m%y'))
statement.new_balance_date = time.strftime(
self.date_format, time.strptime(rmspaces(line[57:63]), '%d%m%y'))

def parseFreeCommunication(self, line, statement):
comm_line = FreeCommunication()
Expand All @@ -236,10 +260,10 @@ def join_communications(c1, c2):
if not c1:
return c2
if not c2:
return c1
return c1
if not c2.startswith(" "):
return " ".join([c1, c2])
return c1 + c2
return c1 + c2


def rmspaces(s):
Expand Down
7 changes: 4 additions & 3 deletions coda/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ class MovementRecordType(object):
class MovementRecord(object):
"""A movement record
"""

def __init__(self):
self.ref = None

# Continuous sequence number Starts at 0001 and is increased by 1 for
# each movement record referring to
# another movement on the daily statement
# of account.
self.ref_move = None

# Detail number for
# each movement record for the same
# continuous sequence number.
Expand Down Expand Up @@ -78,7 +79,7 @@ def __init__(self):
class InformationRecord(object):
""" Information record
"""

def __init__(self):
self.ref = None
self.transaction_ref = None
Expand Down
7 changes: 4 additions & 3 deletions coda/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

BASEPATH = os.path.dirname(__file__)


class TestParser(object):

def _test_first_statement(self, statement):
Expand Down Expand Up @@ -107,12 +108,12 @@ def _checkInformation(self, information):
eq_(information.transaction_code, "01")
eq_(information.transaction_category, "000")
eq_(information.communication, "001AMERICAN EXPRESS")

def test_wrong_globalisation(self):
"""Test wrong globalisation
Check that a globalisation line without the corresponding 'end globalisation'
is considered as Normal
is considered as Normal
"""
parser = Parser()
with open(os.path.join(BASEPATH, "Coda_v2_3_faulty_globalisation.txt")) as f:
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
zip_safe=False,
include_package_data=True,
install_requires=requires,
setup_requires = ['nose'],
setup_requires=['nose'],
tests_require=requires + ['nose', 'coverage'],
test_suite = 'nose.collector',
test_suite='nose.collector',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
Expand Down

0 comments on commit d47c6e5

Please sign in to comment.