use raw strings for regexp with escape sequences

This commit is contained in:
Alexandre Fayolle
2014-12-03 09:48:10 +01:00
parent fff650242c
commit 42f47cfdc8
10 changed files with 120 additions and 120 deletions

View File

@@ -397,7 +397,7 @@ class parser(object):
param being a company account is not enforced here either. param being a company account is not enforced here either.
""" """
def normalize(account_no): def normalize(account_no):
return re.sub('\s', '', account_no) return re.sub(r'\s', '', account_no)
account = normalize(account) account = normalize(account)
cr.execute( cr.execute(

View File

@@ -27,111 +27,111 @@ class PatuParser(object):
recparse = dict() recparse = dict()
recparse["00"] = ( recparse["00"] = (
"T(?P<recordid>00)(?P<record_len>\d{3})" r"T(?P<recordid>00)(?P<record_len>\d{3})"
"(?P<version>\d{3})(?P<accountnr>\d{14})" r"(?P<version>\d{3})(?P<accountnr>\d{14})"
"(?P<statementnr>\d{3})(?P<startdate>\d{6})" r"(?P<statementnr>\d{3})(?P<startdate>\d{6})"
"(?P<enddate>\d{6})" r"(?P<enddate>\d{6})"
"(?P<creationdate>\d{6})(?P<creationtime>\d{4})" r"(?P<creationdate>\d{6})(?P<creationtime>\d{4})"
"(?P<customerid>.{17})(?P<balancedate>\d{6})" r"(?P<customerid>.{17})(?P<balancedate>\d{6})"
"(?P<startingbalance>.{19})" r"(?P<startingbalance>.{19})"
"(?P<itemcount>\d{6})(?P<currency>.{3})" r"(?P<itemcount>\d{6})(?P<currency>.{3})"
"(?P<accountname>.{30})" r"(?P<accountname>.{30})"
"(?P<accountlimit>\d{18})(?P<accountowner>.{35})" r"(?P<accountlimit>\d{18})(?P<accountowner>.{35})"
"(?P<bankcontact1>.{40})(?P<bankcontact2>.{40})" r"(?P<bankcontact1>.{40})(?P<bankcontact2>.{40})"
"(?P<bankcontact3>.{30})(?P<ibanswift>.{30})" r"(?P<bankcontact3>.{30})(?P<ibanswift>.{30})"
) )
recparse["10"] = ( recparse["10"] = (
"T(?P<recordid>[18]0)(?P<record_len>\d{3})" r"T(?P<recordid>[18]0)(?P<record_len>\d{3})"
"(?P<eventid>\d{6})" r"(?P<eventid>\d{6})"
"(?P<archivalnr>.{18})(?P<recorddate>\d{6})" r"(?P<archivalnr>.{18})(?P<recorddate>\d{6})"
"(?P<valuedate>\d{6})" r"(?P<valuedate>\d{6})"
"(?P<paymentdate>\d{6})(?P<eventtype>\d)" r"(?P<paymentdate>\d{6})(?P<eventtype>\d)"
"(?P<eventcode>.{3})(?P<eventdesc>.{35})" r"(?P<eventcode>.{3})(?P<eventdesc>.{35})"
"(?P<amount>.{19})(?P<receiptcode>.)(?P<creationmethod>.)" r"(?P<amount>.{19})(?P<receiptcode>.)(?P<creationmethod>.)"
"(?P<recipientname>.{35})(?P<recipientsource>.)" r"(?P<recipientname>.{35})(?P<recipientsource>.)"
"(?P<recipientaccount>.{14})(?P<recipientaccountchanged>.)" r"(?P<recipientaccount>.{14})(?P<recipientaccountchanged>.)"
"(?P<refnr>.{20})" r"(?P<refnr>.{20})"
"(?P<formnr>.{8})(?P<eventlevel>.)" r"(?P<formnr>.{8})(?P<eventlevel>.)"
) )
recparse["11"] = ( recparse["11"] = (
"T(?P<recordid>[18]1)(?P<record_len>\d{3})" r"T(?P<recordid>[18]1)(?P<record_len>\d{3})"
"(?P<infotype>.{2})" r"(?P<infotype>.{2})"
"(?:(?# Match specific info)" r"(?:(?# Match specific info)"
"(?<=00)(?P<message>.{35})+" r"(?<=00)(?P<message>.{35})+"
"|" r"|"
"(?<=01)(?P<transactioncount>\d{8})" r"(?<=01)(?P<transactioncount>\d{8})"
"|" r"|"
"(?<=02)(?P<customerid>.{10})\s(?P<invoicenr>.{15})\s" r"(?<=02)(?P<customerid>.{10})\s(?P<invoicenr>.{15})\s"
"(?P<invoicedate>\d{6})" r"(?P<invoicedate>\d{6})"
"|" r"|"
"(?<=03)(?P<cardnumber>.{19})\s(?P<storereference>.{14})" r"(?<=03)(?P<cardnumber>.{19})\s(?P<storereference>.{14})"
"|" r"|"
"(?<=04)(?P<origarchiveid>.{18})" r"(?<=04)(?P<origarchiveid>.{18})"
"|" r"|"
"(?<=05)(?P<destinationamount>.{19})\s(?P<currency>.{3})\s" r"(?<=05)(?P<destinationamount>.{19})\s(?P<currency>.{3})\s"
"(?P<exchangerate>.{11})(?P<rateref>.{6})" r"(?P<exchangerate>.{11})(?P<rateref>.{6})"
"|" r"|"
"(?<=06)(?P<principalinfo1>.{35})(?P<principalinfo2>.{35})" r"(?<=06)(?P<principalinfo1>.{35})(?P<principalinfo2>.{35})"
"|" r"|"
"(?<=07)(?P<bankinfo1>.{35})" r"(?<=07)(?P<bankinfo1>.{35})"
"(?P<bankinfo2>.{35})?" r"(?P<bankinfo2>.{35})?"
"(?P<bankinfo3>.{35})?" r"(?P<bankinfo3>.{35})?"
"(?P<bankinfo4>.{35})?" r"(?P<bankinfo4>.{35})?"
"(?P<bankinfo5>.{35})?" r"(?P<bankinfo5>.{35})?"
"(?P<bankinfo6>.{35})?" r"(?P<bankinfo6>.{35})?"
"(?P<bankinfo7>.{35})?" r"(?P<bankinfo7>.{35})?"
"(?P<bankinfo8>.{35})?" r"(?P<bankinfo8>.{35})?"
"(?P<bankinfo9>.{35})?" r"(?P<bankinfo9>.{35})?"
"(?P<bankinfo10>.{35})?" r"(?P<bankinfo10>.{35})?"
"(?P<bankinfo11>.{35})?" r"(?P<bankinfo11>.{35})?"
"(?P<bankinfo12>.{35})?" r"(?P<bankinfo12>.{35})?"
"|" r"|"
"(?<=08)(?P<paymentcode>\d{3})\s(?P<paymentdesc>.{31})" r"(?<=08)(?P<paymentcode>\d{3})\s(?P<paymentdesc>.{31})"
"|" r"|"
"(?<=09)(?P<recipientname2>.{35})" r"(?<=09)(?P<recipientname2>.{35})"
"|" r"|"
"(?<=11)(?P<reference>.{35})(?P<recipientiban>.{35})" r"(?<=11)(?P<reference>.{35})(?P<recipientiban>.{35})"
"(?P<recipientbic>.{35})(?P<recipientnameiban>.{70})" r"(?P<recipientbic>.{35})(?P<recipientnameiban>.{70})"
"(?P<sendername>.{70})(?P<senderid>.{35})" r"(?P<sendername>.{70})(?P<senderid>.{35})"
"(?P<archivalid>.{70})" r"(?P<archivalid>.{70})"
")" r")"
) )
recparse["40"] = ( recparse["40"] = (
"T(?P<recordid>40)(?P<record_len>\d{3})" r"T(?P<recordid>40)(?P<record_len>\d{3})"
"(?P<recorddate>\d{6})(?P<balance>.{19})" r"(?P<recorddate>\d{6})(?P<balance>.{19})"
"(?P<availablefunds>.{19})" r"(?P<availablefunds>.{19})"
) )
recparse["50"] = ( recparse["50"] = (
"T(?P<recordid>50)(?P<record_len>\d{3})" r"T(?P<recordid>50)(?P<record_len>\d{3})"
"(?P<period>\d)(?P<perioddate>\d{6})" r"(?P<period>\d)(?P<perioddate>\d{6})"
"(?P<depositcount>\d{8})(?P<depositsum>.{19})" r"(?P<depositcount>\d{8})(?P<depositsum>.{19})"
"(?P<withdrawcount>\d{8})(?P<withdrawsum>.{19})" r"(?P<withdrawcount>\d{8})(?P<withdrawsum>.{19})"
) )
recparse["60"] = ( recparse["60"] = (
"T(?P<recordid>60)(?P<record_len>\d{3})" r"T(?P<recordid>60)(?P<record_len>\d{3})"
"(?P<bankid>.{3})(?P<specialid>01)" r"(?P<bankid>.{3})(?P<specialid>01)"
"(?P<interestperiodstart>\d{6})-" r"(?P<interestperiodstart>\d{6})-"
"(?P<interestperiodend>\d{6})" r"(?P<interestperiodend>\d{6})"
"(?P<avgbalanceinfo>.)(?P<avgbalance>.{19})" r"(?P<avgbalanceinfo>.)(?P<avgbalance>.{19})"
"(?P<interestinfo>.)(?P<interestrate>\d{7})" r"(?P<interestinfo>.)(?P<interestrate>\d{7})"
"(?P<limitbalanceinfo>.)(?P<avglimitbalance>.{19})" r"(?P<limitbalanceinfo>.)(?P<avglimitbalance>.{19})"
"(?P<limitinterestinfo>.)(?P<limitinterestrate>\d{7})" r"(?P<limitinterestinfo>.)(?P<limitinterestrate>\d{7})"
"(?P<limitusageinfo>.)(?P<limitusage>\d{7})" r"(?P<limitusageinfo>.)(?P<limitusage>\d{7})"
"(?P<permanentbalanceinfo>.)(?P<permanentbalance>.{19})" r"(?P<permanentbalanceinfo>.)(?P<permanentbalance>.{19})"
"(?P<refinterestinfo>.)(?P<refinterestname>.{35})" r"(?P<refinterestinfo>.)(?P<refinterestname>.{35})"
"(?P<refinterestrate>\d{7})" r"(?P<refinterestrate>\d{7})"
"(?P<refcreditinfo>.)(?P<refcreditname>.{35})" r"(?P<refcreditinfo>.)(?P<refcreditname>.{35})"
"(?P<refcreditrate>\d{7})" r"(?P<refcreditrate>\d{7})"
) )
recparse["70"] = ( recparse["70"] = (
"T(?P<recordid>70)(?P<record_len>\d{3})" r"T(?P<recordid>70)(?P<record_len>\d{3})"
"(?P<bankid>\d{3})" r"(?P<bankid>\d{3})"
"(?P<infoline1>.{80})" r"(?P<infoline1>.{80})"
"(?P<infoline2>.{80})?" r"(?P<infoline2>.{80})?"
"(?P<infoline3>.{80})?" r"(?P<infoline3>.{80})?"
"(?P<infoline4>.{80})?" r"(?P<infoline4>.{80})?"
"(?P<infoline5>.{80})?" r"(?P<infoline5>.{80})?"
"(?P<infoline6>.{80})?" r"(?P<infoline6>.{80})?"
) )
for record in recparse: for record in recparse:
recparse[record] = re.compile(recparse[record]) recparse[record] = re.compile(recparse[record])

View File

@@ -223,7 +223,7 @@ class transaction(models.mem_bank_transaction):
transfer_type = 'GIRO' transfer_type = 'GIRO'
# field has markup 'GIRO ACCOUNT OWNER' # field has markup 'GIRO ACCOUNT OWNER'
# separated by clusters of space of varying size # separated by clusters of space of varying size
account_match = re.match('\s*([0-9]+)\s(.*)$', field[5:]) account_match = re.match(r'\s*([0-9]+)\s(.*)$', field[5:])
if account_match: if account_match:
remote_account = account_match.group(1).zfill(10) remote_account = account_match.group(1).zfill(10)
remote_owner = account_match.group(2).strip() or '' remote_owner = account_match.group(2).strip() or ''
@@ -245,7 +245,7 @@ class transaction(models.mem_bank_transaction):
# format # format
elif field.startswith('MAANDBIJDRAGE ABNAMRO'): elif field.startswith('MAANDBIJDRAGE ABNAMRO'):
transfer_type = 'COSTS' transfer_type = 'COSTS'
elif re.match("^\s([0-9]+\.){3}[0-9]+\s", field): elif re.match(r"^\s([0-9]+\.){3}[0-9]+\s", field):
transfer_type = 'BANK' transfer_type = 'BANK'
remote_account = field[1:13].strip().replace('.', '').zfill(10) remote_account = field[1:13].strip().replace('.', '').zfill(10)
# column 14 to 31 is either empty or contains the remote owner # column 14 to 31 is either empty or contains the remote owner
@@ -325,8 +325,8 @@ class transaction(models.mem_bank_transaction):
# but can be any numeric line really # but can be any numeric line really
for field in fields[1:]: for field in fields[1:]:
m = re.match( m = re.match(
"^\s*((BETALINGSKENM\.)|(ACCEPTGIRO))?\s*([0-9]+" r"^\s*((BETALINGSKENM\.)|(ACCEPTGIRO))?\s*([0-9]+"
"([ /][0-9]+)*)\s*$", r"([ /][0-9]+)*)\s*$",
field) field)
if m: if m:
self.reference = m.group(4) self.reference = m.group(4)

View File

@@ -117,7 +117,7 @@ class transaction_message(object):
# payment batch done via clieop # payment batch done via clieop
if (self.transfer_type == 'VZ' if (self.transfer_type == 'VZ'
and (not self.remote_account or self.remote_account == '0') and (not self.remote_account or self.remote_account == '0')
and (not self.message or re.match('^\s*$', self.message)) and (not self.message or re.match(r'^\s*$', self.message))
and self.remote_owner.startswith('TOTAAL ')): and self.remote_owner.startswith('TOTAAL ')):
self.transfer_type = 'PB' self.transfer_type = 'PB'
self.message = self.remote_owner self.message = self.remote_owner
@@ -127,7 +127,7 @@ class transaction_message(object):
and not self.remote_account\ and not self.remote_account\
and not self.remote_owner\ and not self.remote_owner\
and re.match( and re.match(
'^Verzamel Eurobetaling .* TOTAAL \d+ POSTEN\s*$', r'^Verzamel Eurobetaling .* TOTAAL \d+ POSTEN\s*$',
self.message): self.message):
self.transfer_type = 'PB' self.transfer_type = 'PB'
else: else:

View File

@@ -116,7 +116,7 @@ class transaction(models.mem_bank_transaction):
} }
# global expression for matching storno references # global expression for matching storno references
ref_expr = re.compile('REF[\*:]([0-9A-Z-z_-]+)') ref_expr = re.compile(r'REF[\*:]([0-9A-Z-z_-]+)')
# match references for Acceptgiro's through Internet banking # match references for Acceptgiro's through Internet banking
kn_expr = re.compile('KN: ([^ ]+)') kn_expr = re.compile('KN: ([^ ]+)')
@@ -178,8 +178,8 @@ class transaction(models.mem_bank_transaction):
before = self.message[:index] before = self.message[:index]
self.message = self.message[index:] self.message = self.message[index:]
expression = ( expression = (
"^\s*(KN:\s*(?P<kn>[^\s]+))?(\s*)" r"^\s*(KN:\s*(?P<kn>[^\s]+))?(\s*)"
"(?P<navr>NAVR:\s*[^\s]+)?(\s*)(?P<after>.*?)$") r"(?P<navr>NAVR:\s*[^\s]+)?(\s*)(?P<after>.*?)$")
msg_match = re.match(expression, self.message) msg_match = re.match(expression, self.message)
after = msg_match.group('after') after = msg_match.group('after')
kn = msg_match.group('kn') kn = msg_match.group('kn')
@@ -196,7 +196,7 @@ class transaction(models.mem_bank_transaction):
if parts: if parts:
zip_city = parts.pop(0).strip() zip_city = parts.pop(0).strip()
zip_match = re.match( zip_match = re.match(
"^(?P<zipcode>[^ ]{6})\s+(?P<city>.*?)$", zip_city) r"^(?P<zipcode>[^ ]{6})\s+(?P<city>.*?)$", zip_city)
if zip_match: if zip_match:
self.remote_owner_postalcode = zip_match.group('zipcode') self.remote_owner_postalcode = zip_match.group('zipcode')
self.remote_owner_city = zip_match.group('city') self.remote_owner_city = zip_match.group('city')

View File

@@ -39,8 +39,8 @@ class IngMT940Parser(MT940, parser):
code = 'INT_MT940_STRUC' code = 'INT_MT940_STRUC'
tag_61_regex = re.compile( tag_61_regex = re.compile(
'^(?P<date>\d{6})(?P<sign>[CD])(?P<amount>\d+,\d{2})N(?P<type>.{3})' r'^(?P<date>\d{6})(?P<sign>[CD])(?P<amount>\d+,\d{2})N(?P<type>.{3})'
'(?P<reference>\w{1,16})') r'(?P<reference>\w{1,16})')
def create_transaction(self, cr): def create_transaction(self, cr):
return transaction() return transaction()

View File

@@ -38,8 +38,8 @@ class RaboMT940Parser(MT940, parser):
header_lines = 1 header_lines = 1
tag_61_regex = re.compile( tag_61_regex = re.compile(
'^(?P<date>\d{6})(?P<sign>[CD])(?P<amount>\d+,\d{2})N(?P<type>.{3})' r'^(?P<date>\d{6})(?P<sign>[CD])(?P<amount>\d+,\d{2})N(?P<type>.{3})'
'(?P<reference>\w{1,16})') r'(?P<reference>\w{1,16})')
def parse(self, cr, data): def parse(self, cr, data):
'implements account_banking.parsers.models.parser.parse()' 'implements account_banking.parsers.models.parser.parse()'

View File

@@ -61,7 +61,7 @@ class transaction_message(object):
self.__dict__.update(dict(zip(self.attrnames, values))) self.__dict__.update(dict(zip(self.attrnames, values)))
# for lack of a standardized locale function to parse amounts # for lack of a standardized locale function to parse amounts
self.transferred_amount = float( self.transferred_amount = float(
re.sub(',', '.', re.sub('\.', '', self.transferred_amount))) re.sub(',', '.', re.sub(r'\.', '', self.transferred_amount)))
if self.debcred == 'Debet': if self.debcred == 'Debet':
self.transferred_amount = -self.transferred_amount self.transferred_amount = -self.transferred_amount
self.execution_date = str2date(self.date, '%d-%m-%Y') self.execution_date = str2date(self.date, '%d-%m-%Y')

View File

@@ -178,10 +178,10 @@ class parser_hsbc_mt940(models.parser):
result = [] result = []
parser = HSBCParser() parser = HSBCParser()
# Split into statements # Split into statements
statements = [st for st in re.split('[\r\n]*(?=:20:)', data)] statements = [st for st in re.split(r'[\r\n]*(?=:20:)', data)]
# Split by records # Split by records
statement_list = [ statement_list = [
re.split('[\r\n ]*(?=:\d\d[\w]?:)', st) re.split(r'[\r\n ]*(?=:\d\d[\w]?:)', st)
for st in statements for st in statements
] ]

View File

@@ -31,7 +31,7 @@ class HSBCParser(object):
def __init__(self): def __init__(self):
recparse = dict() recparse = dict()
patterns = {'ebcdic': "\w/\?:\(\).,'+{} -"} patterns = {'ebcdic': r"\w/\?:\(\).,'+{} -"}
# MT940 header # MT940 header
recparse["20"] = r":(?P<recordid>20):(?P<transref>.{1,16})" recparse["20"] = r":(?P<recordid>20):(?P<transref>.{1,16})"
@@ -60,18 +60,18 @@ class HSBCParser(object):
# Further info # Further info
recparse["86"] = (r":(?P<recordid>86):" recparse["86"] = (r":(?P<recordid>86):"
"(?P<infoline1>.{1,80})?" r"(?P<infoline1>.{1,80})?"
"(?:\n(?P<infoline2>.{1,80}))?" r"(?:\n(?P<infoline2>.{1,80}))?"
"(?:\n(?P<infoline3>.{1,80}))?" r"(?:\n(?P<infoline3>.{1,80}))?"
"(?:\n(?P<infoline4>.{1,80}))?" r"(?:\n(?P<infoline4>.{1,80}))?"
"(?:\n(?P<infoline5>.{1,80}))?") r"(?:\n(?P<infoline5>.{1,80}))?")
# Forward available balance (64) / Closing balance (62F) # Forward available balance (64) / Closing balance (62F)
# / Interim balance (62M) # / Interim balance (62M)
recparse["64"] = (r":(?P<recordid>64|62[FM]):" recparse["64"] = (r":(?P<recordid>64|62[FM]):"
"(?P<creditmarker>[CD])" r"(?P<creditmarker>[CD])"
"(?P<bookingdate>\d{6})(?P<currencycode>.{3})" r"(?P<bookingdate>\d{6})(?P<currencycode>.{3})"
"(?P<endingbalance>[\d,]{1,15})") r"(?P<endingbalance>[\d,]{1,15})")
for record in recparse: for record in recparse:
recparse[record] = re.compile(recparse[record]) recparse[record] = re.compile(recparse[record])