fix UnicodeDecodeError raise when detect encoding

This commit is contained in:
Rossen Georgiev 2015-07-18 07:54:54 +01:00
parent 6285051c39
commit 347410db37
1 changed files with 20 additions and 9 deletions

View File

@ -65,6 +65,25 @@ MTYPE_TABLE_CUSTOM = {
} }
def _unicode_packet(packet):
# attempt utf-8
try:
return packet.decode('utf-8')
except UnicodeDecodeError:
pass
# attempt to detect encoding
res = chardet.detect(packet.split(b':', 1)[-1])
if res['confidence'] > 0.7:
try:
return packet.decode(res['encoding'])
except UnicodeDecodeError:
pass
# if everything fails
return packet.decode('latin-1')
def parse(packet): def parse(packet):
""" """
Parses an APRS packet and returns a dict with decoded data Parses an APRS packet and returns a dict with decoded data
@ -83,15 +102,7 @@ def parse(packet):
# attempt to detect encoding # attempt to detect encoding
if isinstance(packet, bytes): if isinstance(packet, bytes):
try: packet = _unicode_packet(packet)
packet = packet.decode('utf-8')
except UnicodeDecodeError:
res = chardet.detect(packet.split(b':', 1)[-1])
if res['confidence'] > 0.7:
packet = packet.decode(res['encoding'])
else:
packet = packet.decode('latin-1')
packet = packet.rstrip("\r\n") packet = packet.rstrip("\r\n")
logger.debug("Parsing: %s", packet) logger.debug("Parsing: %s", packet)