Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Commit

Permalink
Fix de43 field processor
Browse files Browse the repository at this point in the history
* use regex rather than just string handling\
* if does not match field pattern, don't fail. Just don't return sub fields
  • Loading branch information
adelosa committed Aug 6, 2016
1 parent cbb51c3 commit 5d789de
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
5 changes: 3 additions & 2 deletions HISTORY.rst
Expand Up @@ -2,10 +2,11 @@
History
=======
0.4.5 (2016-08-05)
0.4.5 (2016-08-06)
------------------
* check that all of message consumed by fields otherwise raise execption
* check that all of message consumed by fields otherwise raise exception
* get rid of a heap of debugging prints that were clogging the output
* allow freestyle de43 fields with the de43 processor enabled. Use regex rather than string splits

0.4.4-0.4.3 (2016-08-03)
------------------------
Expand Down
27 changes: 17 additions & 10 deletions mciutil/mciutil.py
Expand Up @@ -11,6 +11,7 @@
import datetime
import decimal
import codecs
import re
import binascii

import bitarray
Expand Down Expand Up @@ -559,16 +560,22 @@ def _get_de43_fields(de43_field):
:param de43_field: data of pds 43
:return: dictionary of pds 43 sub elements
"""

de43_elements = {}
de43_split = de43_field.split(b('\\'))
de43_elements["DE43_NAME"] = de43_split[0].rstrip()
de43_elements["DE43_ADDRESS"] = de43_split[1].rstrip()
de43_elements["DE43_SUBURB"] = de43_split[2].rstrip()
de43_elements["DE43_POSTCODE"] = de43_split[3][:4]
de43_elements["DE43_STATE"] = de43_split[3][len(de43_split[3])-6:len(de43_split[3])-3]
de43_elements["DE43_COUNTRY"] = de43_split[3][len(de43_split[3])-3:len(de43_split[3])]
return de43_elements
LOGGER.debug("de43_field=%s", de43_field)
de43_regex = (
r"(?P<DE43_NAME>.+?) *\\(?P<DE43_ADDRESS>.+?) *\\(?P<DE43_SUBURB>.+?) *\\"
r"(?P<DE43_POSTCODE>\d{4,10}) *(?P<DE43_STATE>.{3})(?P<DE43_COUNTRY>.{3})"
)

field_match = re.match(de43_regex, de43_field.decode())
if not field_match:
return dict()
# get the dict
field_dict = field_match.groupdict()
# set fields in dict to bytes (no effect for py2)
for field in field_dict:
field_dict[field] = b(field_dict[field])

return field_dict

if sys.version_info < (3,):
def b(string):
Expand Down
5 changes: 5 additions & 0 deletions tests/test_mciutil.py
Expand Up @@ -113,6 +113,11 @@ def test_get_message_elements_ebcdic(self):
self.assertEqual(message_elements["DE3"], b"111111")
self.assertEqual(message_elements["DE4"], b"000000009999")

def test_get_de43_elements_no_pattern_match(self):
de43_raw = b("MUMBAI EMV ATM - 2 MUMBAI IN")
de43_elements = _get_de43_fields(de43_raw)
self.assertEqual({}, de43_elements)

def test_get_de43_elements(self):
de43_raw = b("AAMI \\36 WICKHAM TERRACE "
" \\BRISBANE \\4000 QLDAUS")
Expand Down

0 comments on commit 5d789de

Please sign in to comment.