1
0
Fork 0

Fix demod data display for utf-8 decodable non-CW observations (W1612)

Previously for unicode decode-able frames in observations where the transmitter
type was not CW, the hex dump was malformed and contained the unicode decoded
text instead.

Fixes the unicode-builtin (W1612) pylint errors as well.

Signed-off-by: Fabian P. Schmidt <kerel@mailbox.org>
merge-requests/847/head
Fabian P. Schmidt 2019-12-30 20:38:18 +01:00
parent 65b379f6af
commit 3609e46b78
3 changed files with 30 additions and 12 deletions

View File

@ -13,7 +13,6 @@ disable=
W0201, # attribute-defined-outside-init
W0703, # broad-except
# py3k transition issues:
W1612, # unicode-builtin
W1619, # old-divison
W1633, # round-builtin
W1645, # exception-message-attribute

View File

@ -60,6 +60,13 @@ TRANSMITTER_STATUS = ['active', 'inactive', 'invalid']
TRANSMITTER_TYPE = ['Transmitter', 'Transceiver', 'Transponder']
def _decode_pretty_hex(binary_data):
"""Return the binary data as hex dump of the following form: `DE AD C0 DE`"""
data = codecs.encode(binary_data, 'hex').decode('ascii').upper()
return ' '.join(data[i:i + 2] for i in range(0, len(data), 2))
def _name_obs_files(instance, filename):
"""Return a filepath formatted by Observation ID"""
return 'data_obs/{0}/{1}'.format(instance.id, filename)
@ -563,15 +570,27 @@ class DemodData(models.Model):
else:
return True
def display_payload(self):
"""Return the content of the data file"""
with open(self.payload_demod.path) as file_path:
payload = file_path.read()
try:
return unicode(payload)
except UnicodeDecodeError:
data = codecs.encode(payload, 'hex').encode('ascii').upper()
return ' '.join(data[i:i + 2] for i in range(0, len(data), 2))
def display_payload_hex(self):
"""
Return the content of the data file as hex dump of the following form: `DE AD C0 DE`.
"""
with open(self.payload_demod.path) as data_file:
payload = data_file.read()
return _decode_pretty_hex(payload)
def display_payload_utf8(self):
"""
Return the content of the data file decoded as UTF-8. If this fails,
show as hex dump.
"""
with open(self.payload_demod.path) as data_file:
payload = data_file.read()
try:
return payload.decode('utf-8')
except UnicodeDecodeError:
return _decode_pretty_hex(payload)
def __str__(self):
return '{} - {}'.format(self.id, self.payload_demod)

View File

@ -385,11 +385,11 @@
</div>
{% elif observation.transmitter_mode == 'CW' %}
<div class="well well-sm data-well">
{{ demoddata.display_payload }}
{{ demoddata.display_payload_utf8 }}
</div>
{% else %}
<div class="well well-sm data-well hex">
{{ demoddata.display_payload }}
{{ demoddata.display_payload_hex }}
</div>
{% endif %}
{% endfor %}