From 5a90299ca5c9d05180e7c8290ef0a185c747092a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Gru=CC=88ndger?= Date: Sun, 4 Oct 2020 11:06:28 +0200 Subject: [PATCH] Refactoring --- ogn/parser/aprs_comment/fanet_parser.py | 17 ++++--- ogn/parser/aprs_comment/flarm_parser.py | 42 +++++++++------- ogn/parser/aprs_comment/ogn_parser.py | 4 +- ogn/parser/aprs_comment/tracker_parser.py | 58 ++++++++++++++--------- setup.cfg | 2 +- tests/parser/test_parse_fanet.py | 6 +-- 6 files changed, 76 insertions(+), 53 deletions(-) diff --git a/ogn/parser/aprs_comment/fanet_parser.py b/ogn/parser/aprs_comment/fanet_parser.py index aea969d..9e804d6 100644 --- a/ogn/parser/aprs_comment/fanet_parser.py +++ b/ogn/parser/aprs_comment/fanet_parser.py @@ -10,9 +10,14 @@ class FanetParser(BaseParser): self.position_parser = PATTERN_FANET_POSITION_COMMENT def parse_position(self, aprs_comment): - ac_match = self.position_parser.match(aprs_comment) - return {'address_type': int(ac_match.group('details'), 16) & 0b00000011 if ac_match.group('details') else None, - 'aircraft_type': (int(ac_match.group('details'), 16) & 0b01111100) >> 2 if ac_match.group('details') else None, - 'stealth': (int(ac_match.group('details'), 16) & 0b10000000) >> 7 == 1 if ac_match.group('details') else None, - 'address': ac_match.group('address') if ac_match.group('address') else None, - 'climb_rate': int(ac_match.group('climb_rate')) * FPM_TO_MS if ac_match.group('climb_rate') else None} + match = self.position_parser.match(aprs_comment) + result = {} + if match.group('details'): + result.update({ + 'address_type': int(match.group('details'), 16) & 0b00000011, + 'aircraft_type': (int(match.group('details'), 16) & 0b01111100) >> 2, + 'stealth': (int(match.group('details'), 16) & 0b10000000) >> 7 == 1, + 'address': match.group('address') + }) + if match.group('climb_rate'): result['climb_rate'] = int(match.group('climb_rate')) * FPM_TO_MS + return result diff --git a/ogn/parser/aprs_comment/flarm_parser.py b/ogn/parser/aprs_comment/flarm_parser.py index caadb48..f327310 100644 --- a/ogn/parser/aprs_comment/flarm_parser.py +++ b/ogn/parser/aprs_comment/flarm_parser.py @@ -12,20 +12,28 @@ class FlarmParser(BaseParser): def parse_position(self, aprs_comment): match = self.position_pattern.match(aprs_comment) - return {k: v for (k, v) in - {'address_type': int(match.group('details'), 16) & 0b00000011 if match.group('details') else None, - 'aircraft_type': (int(match.group('details'), 16) & 0b01111100) >> 2 if match.group('details') else None, - 'stealth': (int(match.group('details'), 16) & 0b10000000) >> 7 == 1 if match.group('details') else None, - 'address': match.group('address') or None, - 'climb_rate': int(match.group('climb_rate')) * FPM_TO_MS if match.group('climb_rate') else None, - 'turn_rate': float(match.group('turn_rate')) * HPM_TO_DEGS if match.group('turn_rate') else None, - 'signal_quality': float(match.group('signal_quality')) if match.group('signal_quality') else None, - 'error_count': int(match.group('error_count')) if match.group('error_count') else None, - 'frequency_offset': float(match.group('frequency_offset')) if match.group('frequency_offset') else None, - 'gps_quality': { - 'horizontal': int(match.group('gps_quality_horizontal')), - 'vertical': int(match.group('gps_quality_vertical'))} if match.group('gps_quality') else None, - 'software_version': float(match.group('software_version')) if match.group('software_version') else None, - 'hardware_version': int(match.group('hardware_version'), 16) if match.group('hardware_version') else None, - 'real_address': match.group('real_address') or None, - 'signal_power': float(match.group('signal_power')) if match.group('signal_power') else None}.items() if v is not None} + result = {} + if match.group('details'): + result.update({ + 'address_type': int(match.group('details'), 16) & 0b00000011, + 'aircraft_type': (int(match.group('details'), 16) & 0b01111100) >> 2, + 'stealth': (int(match.group('details'), 16) & 0b10000000) >> 7 == 1, + 'address': match.group('address'), + }) + if match.group('climb_rate'): result['climb_rate'] = int(match.group('climb_rate')) * FPM_TO_MS + if match.group('turn_rate'): result['turn_rate'] = float(match.group('turn_rate')) * HPM_TO_DEGS + if match.group('signal_quality'): result['signal_quality'] = float(match.group('signal_quality')) + if match.group('error_count'): result['error_count'] = int(match.group('error_count')) + if match.group('frequency_offset'): result['frequency_offset'] = float(match.group('frequency_offset')) + if match.group('gps_quality'): + result.update({ + 'gps_quality': { + 'horizontal': int(match.group('gps_quality_horizontal')), + 'vertical': int(match.group('gps_quality_vertical')) + } + }) + if match.group('software_version'): result['software_version'] = float(match.group('software_version')) + if match.group('hardware_version'): result['hardware_version'] = int(match.group('hardware_version'), 16) + if match.group('real_address'): result['real_address'] = match.group('real_address') + if match.group('signal_power'): result['signal_power'] = float(match.group('signal_power')) + return result diff --git a/ogn/parser/aprs_comment/ogn_parser.py b/ogn/parser/aprs_comment/ogn_parser.py index 64d8c9f..06bf07f 100644 --- a/ogn/parser/aprs_comment/ogn_parser.py +++ b/ogn/parser/aprs_comment/ogn_parser.py @@ -49,8 +49,8 @@ class OgnParser(BaseParser): 'gps_quality': { 'horizontal': int(match.group('gps_quality_horizontal')), 'vertical': int(match.group('gps_quality_vertical')) - } - }) + } + }) if match.group('flarm_software_version'): result['software_version'] = float(match.group('flarm_software_version')) if match.group('flarm_hardware_version'): result['hardware_version'] = int(match.group('flarm_hardware_version'), 16) if match.group('flarm_id'): result['real_address'] = match.group('flarm_id') diff --git a/ogn/parser/aprs_comment/tracker_parser.py b/ogn/parser/aprs_comment/tracker_parser.py index b81d1b3..e1149c0 100644 --- a/ogn/parser/aprs_comment/tracker_parser.py +++ b/ogn/parser/aprs_comment/tracker_parser.py @@ -12,34 +12,48 @@ class TrackerParser(BaseParser): def parse_position(self, aprs_comment): match = self.position_pattern.match(aprs_comment) - return {'address_type': int(match.group('details'), 16) & 0b00000011, + + result = {} + if match.group('details'): + result.update({ + 'address_type': int(match.group('details'), 16) & 0b00000011, 'aircraft_type': (int(match.group('details'), 16) & 0b01111100) >> 2, 'stealth': (int(match.group('details'), 16) & 0b10000000) >> 7 == 1, 'address': match.group('address'), - 'climb_rate': int(match.group('climb_rate')) * FPM_TO_MS if match.group('climb_rate') else None, - 'turn_rate': float(match.group('turn_rate')) * HPM_TO_DEGS if match.group('turn_rate') else None, - 'flightlevel': float(match.group('flight_level')) if match.group('flight_level') else None, - 'signal_quality': float(match.group('signal_quality')) if match.group('signal_quality') else None, - 'error_count': int(match.group('error_count')) if match.group('error_count') else None, - 'frequency_offset': float(match.group('frequency_offset')) if match.group('frequency_offset') else None, - 'gps_quality': {'horizontal': int(match.group('gps_quality_horizontal')), - 'vertical': int(match.group('gps_quality_vertical'))} if match.group('gps_quality') else None, - 'signal_power': float(match.group('signal_power')) if match.group('signal_power') else None} + }) + if match.group('climb_rate'): result['climb_rate'] = int(match.group('climb_rate')) * FPM_TO_MS + if match.group('turn_rate'): result['turn_rate'] = float(match.group('turn_rate')) * HPM_TO_DEGS + if match.group('flight_level'): result['flightlevel'] = float(match.group('flight_level')) + if match.group('signal_quality'): result['signal_quality'] = float(match.group('signal_quality')) + if match.group('error_count'): result['error_count'] = int(match.group('error_count')) + if match.group('frequency_offset'): result['frequency_offset'] = float(match.group('frequency_offset')) + if match.group('gps_quality'): + result.update({ + 'gps_quality': { + 'horizontal': int(match.group('gps_quality_horizontal')), + 'vertical': int(match.group('gps_quality_vertical')) + } + }) + if match.group('signal_power'): result['signal_power'] = float(match.group('signal_power')) + return result def parse_status(self, aprs_comment): match = self.status_pattern.match(aprs_comment) if match: - return {'hardware_version': int(match.group('hardware_version')) if match.group('hardware_version') else None, - 'software_version': int(match.group('software_version')) if match.group('software_version') else None, - 'gps_satellites': int(match.group('gps_satellites')) if match.group('gps_satellites') else None, - 'gps_quality': int(match.group('gps_quality')) if match.group('gps_quality') else None, - 'gps_altitude': int(match.group('gps_altitude')) if match.group('gps_altitude') else None, - 'pressure': float(match.group('pressure')) if match.group('pressure') else None, - 'temperature': float(match.group('temperature')) if match.group('temperature') else None, - 'humidity': int(match.group('humidity')) if match.group('humidity') else None, - 'voltage': float(match.group('voltage')) if match.group('voltage') else None, - 'transmitter_power': int(match.group('transmitter_power')) if match.group('transmitter_power') else None, - 'noise_level': float(match.group('noise_level')) if match.group('noise_level') else None, - 'relays': int(match.group('relays')) if match.group('relays') else None} + result = {} + + if match.group('hardware_version'): result['hardware_version'] = int(match.group('hardware_version')) + if match.group('software_version'): result['software_version'] = int(match.group('software_version')) + if match.group('gps_satellites'): result['gps_satellites'] = int(match.group('gps_satellites')) + if match.group('gps_quality'): result['gps_quality'] = int(match.group('gps_quality')) + if match.group('gps_altitude'): result['gps_altitude'] = int(match.group('gps_altitude')) + if match.group('pressure'): result['pressure'] = float(match.group('pressure')) + if match.group('temperature'): result['temperature'] = float(match.group('temperature')) + if match.group('humidity'): result['humidity'] = int(match.group('humidity')) + if match.group('voltage'): result['voltage'] = float(match.group('voltage')) + if match.group('transmitter_power'): result['transmitter_power'] = int(match.group('transmitter_power')) + if match.group('noise_level'): result['noise_level'] = float(match.group('noise_level')) + if match.group('relays'): result['relays'] = int(match.group('relays')) + return result else: return {'comment': aprs_comment} diff --git a/setup.cfg b/setup.cfg index e44b810..50bec38 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,2 @@ [flake8] -ignore = E501 +ignore = E501,E701 diff --git a/tests/parser/test_parse_fanet.py b/tests/parser/test_parse_fanet.py index 13e2f29..fa2a00a 100644 --- a/tests/parser/test_parse_fanet.py +++ b/tests/parser/test_parse_fanet.py @@ -17,11 +17,7 @@ class TestStringMethods(unittest.TestCase): def test_pseudo_status_comment(self): message = FanetParser().parse_position("") - self.assertIsNone(message['address_type']) - self.assertIsNone(message['aircraft_type']) - self.assertIsNone(message['stealth']) - self.assertIsNone(message['address']) - self.assertIsNone(message['climb_rate']) + self.assertEqual(message, {}) if __name__ == '__main__':