From 3609e46b78969fffa69d237988854907d4cf1318 Mon Sep 17 00:00:00 2001 From: "Fabian P. Schmidt" Date: Mon, 30 Dec 2019 20:38:18 +0100 Subject: [PATCH] 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 --- .pylintrc | 1 - network/base/models.py | 37 +++++++++++++++----- network/templates/base/observation_view.html | 4 +-- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/.pylintrc b/.pylintrc index 67bff61..456275d 100644 --- a/.pylintrc +++ b/.pylintrc @@ -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 diff --git a/network/base/models.py b/network/base/models.py index 047fedf..f03651e 100644 --- a/network/base/models.py +++ b/network/base/models.py @@ -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) diff --git a/network/templates/base/observation_view.html b/network/templates/base/observation_view.html index 27ec1d7..a64628a 100644 --- a/network/templates/base/observation_view.html +++ b/network/templates/base/observation_view.html @@ -385,11 +385,11 @@ {% elif observation.transmitter_mode == 'CW' %}
- {{ demoddata.display_payload }} + {{ demoddata.display_payload_utf8 }}
{% else %}
- {{ demoddata.display_payload }} + {{ demoddata.display_payload_hex }}
{% endif %} {% endfor %}