Skip to content

Commit

Permalink
Merge pull request #6 from starkbank/fix/python2-encoding
Browse files Browse the repository at this point in the history
Fix circular import for Python2
  • Loading branch information
daltonfm-stark authored Feb 1, 2021
2 parents 49a42dc + 9c80c1d commit d13f68a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
7 changes: 6 additions & 1 deletion starkbank/iso8583/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
encoding = "cp500"

from starkbank.iso8583.message import parse, unparse

def getEncoding():
return encoding


from starkbank.iso8583.message import parse, unparse
8 changes: 4 additions & 4 deletions starkbank/iso8583/message.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from bisect import bisect_left as bisectLeft
from starkbank import iso8583
from .mastercard import mastercard
from .utils.binary import Binary
from . import getEncoding


def getVersion(MTI):
Expand All @@ -12,7 +12,7 @@ def getVersion(MTI):
try:
return versionMap[MTI[0]]
except KeyError:
raise ValueError("Expected '0' or '1' in MTI[0] (Actual {MTI}), please check message iso8583.encoding".format(MTI=MTI))
raise ValueError("Expected '0' or '1' in MTI[0] (Actual: {MTI}), please check iso8583.encoding".format(MTI=MTI))


def parse(message, template=mastercard):
Expand Down Expand Up @@ -46,7 +46,7 @@ def parseElement(message, elementId, template):
rule = template[elementId]
size = rule["limit"]
if rule["type"]:
parseSize = int(message[:rule["type"]].decode(iso8583.encoding) or 0)
parseSize = int(message[:rule["type"]].decode(getEncoding()) or 0)
size = min(size, parseSize)
if parseSize > size:
raise ValueError(
Expand Down Expand Up @@ -105,7 +105,7 @@ def unparseElement(json, elementId, template):
size = rule["limit"]
unparsed = rule["unparser"](data)[:size]
if rule["type"]:
return str(len(unparsed)).zfill(rule["type"]).encode(iso8583.encoding) + unparsed
return str(len(unparsed)).zfill(rule["type"]).encode(getEncoding()) + unparsed
if len(unparsed) != size:
raise ValueError("{elementId}: Expected length {lenExpected}, got {lenActual}".format(
elementId=elementId,
Expand Down
22 changes: 11 additions & 11 deletions starkbank/iso8583/utils/parser.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from base64 import b64encode, b64decode
from copy import deepcopy
from binascii import hexlify, unhexlify
from starkbank import iso8583
from .. import getEncoding


def parseString(text):
return text.decode(iso8583.encoding)
return text.decode(getEncoding())


def unparseString(text):
return text.encode(iso8583.encoding)
return text.encode(getEncoding())


def parseBin(text):
Expand All @@ -34,31 +34,31 @@ def unparseBytesToBin(text, length=64):

def parseSubelements(text):
json = {
"SE00": text[0:1].decode(iso8583.encoding)
"SE00": text[0:1].decode(getEncoding())
}
text = text[1:]
while text:
key, length, text = text[0:2].decode(iso8583.encoding), int(text[2:4].decode(iso8583.encoding)), text[4:]
value, text = text[0:length].decode(iso8583.encoding), text[length:]
key, length, text = text[0:2].decode(getEncoding()), int(text[2:4].decode(getEncoding())), text[4:]
value, text = text[0:length].decode(getEncoding()), text[length:]
json["SE" + key.zfill(2)] = value
return json


def unparseSubelements(text):
json = deepcopy(text)
string = json.pop("SE00").encode(iso8583.encoding)
string = json.pop("SE00").encode(getEncoding())
for key, value in sorted(json.items()):
key = key.replace("SE", "")
length = len(value)
string += key.encode(iso8583.encoding) + str(length).zfill(2).encode(iso8583.encoding) + value.encode(iso8583.encoding)
string += key.encode(getEncoding()) + str(length).zfill(2).encode(getEncoding()) + value.encode(getEncoding())
return string


def parsePds(text):
json = {}
while text:
key, length, text = text[0:4].decode(iso8583.encoding), int(text[4:7].decode(iso8583.encoding)), text[7:]
value, text = text[0:length].decode(iso8583.encoding), text[length:]
key, length, text = text[0:4].decode(getEncoding()), int(text[4:7].decode(getEncoding())), text[7:]
value, text = text[0:length].decode(getEncoding()), text[length:]
json["PDS" + key.zfill(4)] = value
return json

Expand All @@ -69,5 +69,5 @@ def unparsePds(text):
for key, value in sorted(json.items()):
key = key.replace("PDS", "")
length = str(len(value)).zfill(3)
byteString += key.encode(iso8583.encoding) + length.encode(iso8583.encoding) + value.encode(iso8583.encoding)
byteString += key.encode(getEncoding()) + length.encode(getEncoding()) + value.encode(getEncoding())
return byteString

0 comments on commit d13f68a

Please sign in to comment.