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
parent
65b379f6af
commit
3609e46b78
|
@ -13,7 +13,6 @@ disable=
|
||||||
W0201, # attribute-defined-outside-init
|
W0201, # attribute-defined-outside-init
|
||||||
W0703, # broad-except
|
W0703, # broad-except
|
||||||
# py3k transition issues:
|
# py3k transition issues:
|
||||||
W1612, # unicode-builtin
|
|
||||||
W1619, # old-divison
|
W1619, # old-divison
|
||||||
W1633, # round-builtin
|
W1633, # round-builtin
|
||||||
W1645, # exception-message-attribute
|
W1645, # exception-message-attribute
|
||||||
|
|
|
@ -60,6 +60,13 @@ TRANSMITTER_STATUS = ['active', 'inactive', 'invalid']
|
||||||
TRANSMITTER_TYPE = ['Transmitter', 'Transceiver', 'Transponder']
|
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):
|
def _name_obs_files(instance, filename):
|
||||||
"""Return a filepath formatted by Observation ID"""
|
"""Return a filepath formatted by Observation ID"""
|
||||||
return 'data_obs/{0}/{1}'.format(instance.id, filename)
|
return 'data_obs/{0}/{1}'.format(instance.id, filename)
|
||||||
|
@ -563,15 +570,27 @@ class DemodData(models.Model):
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def display_payload(self):
|
def display_payload_hex(self):
|
||||||
"""Return the content of the data file"""
|
"""
|
||||||
with open(self.payload_demod.path) as file_path:
|
Return the content of the data file as hex dump of the following form: `DE AD C0 DE`.
|
||||||
payload = file_path.read()
|
"""
|
||||||
try:
|
with open(self.payload_demod.path) as data_file:
|
||||||
return unicode(payload)
|
payload = data_file.read()
|
||||||
except UnicodeDecodeError:
|
|
||||||
data = codecs.encode(payload, 'hex').encode('ascii').upper()
|
return _decode_pretty_hex(payload)
|
||||||
return ' '.join(data[i:i + 2] for i in range(0, len(data), 2))
|
|
||||||
|
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):
|
def __str__(self):
|
||||||
return '{} - {}'.format(self.id, self.payload_demod)
|
return '{} - {}'.format(self.id, self.payload_demod)
|
||||||
|
|
|
@ -385,11 +385,11 @@
|
||||||
</div>
|
</div>
|
||||||
{% elif observation.transmitter_mode == 'CW' %}
|
{% elif observation.transmitter_mode == 'CW' %}
|
||||||
<div class="well well-sm data-well">
|
<div class="well well-sm data-well">
|
||||||
{{ demoddata.display_payload }}
|
{{ demoddata.display_payload_utf8 }}
|
||||||
</div>
|
</div>
|
||||||
{% else %}
|
{% else %}
|
||||||
<div class="well well-sm data-well hex">
|
<div class="well well-sm data-well hex">
|
||||||
{{ demoddata.display_payload }}
|
{{ demoddata.display_payload_hex }}
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
Loading…
Reference in New Issue