From 7a577ab9ef8bb523d3919473c36d417ca4e62e92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Gru=CC=88ndger?= Date: Mon, 3 Jun 2019 22:17:55 +0200 Subject: [PATCH] Added generic parser for unknown protocol --- ogn/parser/aprs_comment/generic_parser.py | 12 ++++++++++++ ogn/parser/parse.py | 6 ++++-- tests/parser/test_parse.py | 10 +++++----- 3 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 ogn/parser/aprs_comment/generic_parser.py diff --git a/ogn/parser/aprs_comment/generic_parser.py b/ogn/parser/aprs_comment/generic_parser.py new file mode 100644 index 0000000..2676704 --- /dev/null +++ b/ogn/parser/aprs_comment/generic_parser.py @@ -0,0 +1,12 @@ +from .base import BaseParser + + +class GenericParser(BaseParser): + def __init__(self): + self.beacon_type = 'generic' + + def parse_position(self, aprs_comment): + return {'comment': aprs_comment} + + def parse_status(self, aprs_comment): + return {'comment': aprs_comment} diff --git a/ogn/parser/parse.py b/ogn/parser/parse.py index 37f3774..cc20c25 100644 --- a/ogn/parser/parse.py +++ b/ogn/parser/parse.py @@ -3,7 +3,7 @@ from datetime import datetime from ogn.parser.utils import createTimestamp, parseAngle, KNOTS_TO_MS, KPH_TO_MS, FEETS_TO_METER from ogn.parser.pattern import PATTERN_APRS, PATTERN_APRS_POSITION, PATTERN_APRS_STATUS, PATTERN_SERVER -from ogn.parser.exceptions import AprsParseError, OgnParseError +from ogn.parser.exceptions import AprsParseError from ogn.parser.aprs_comment.ogn_parser import OgnParser from ogn.parser.aprs_comment.fanet_parser import FanetParser @@ -15,6 +15,7 @@ from ogn.parser.aprs_comment.receiver_parser import ReceiverParser from ogn.parser.aprs_comment.skylines_parser import SkylinesParser from ogn.parser.aprs_comment.spider_parser import SpiderParser from ogn.parser.aprs_comment.spot_parser import SpotParser +from ogn.parser.aprs_comment.generic_parser import GenericParser def parse(aprs_message, reference_timestamp=None): @@ -105,6 +106,7 @@ dstcall_parser_mapping = {'APRS': OgnParser(), 'OGSKYL': SkylinesParser(), 'OGSPID': SpiderParser(), 'OGSPOT': SpotParser(), + 'GENERIC': GenericParser(), } @@ -113,4 +115,4 @@ def parse_comment(aprs_comment, dstcall='APRS', aprs_type="position"): if parser: return parser.parse(aprs_comment, aprs_type) else: - raise OgnParseError("No parser for dstcall {} found. APRS comment: {}".format(dstcall, aprs_comment)) + return dstcall_parser_mapping.get('GENERIC').parse(aprs_comment, aprs_type) diff --git a/tests/parser/test_parse.py b/tests/parser/test_parse.py index d48977d..90a9bff 100644 --- a/tests/parser/test_parse.py +++ b/tests/parser/test_parse.py @@ -6,7 +6,7 @@ from datetime import datetime from time import sleep from ogn.parser.parse import parse -from ogn.parser.exceptions import AprsParseError, OgnParseError +from ogn.parser.exceptions import AprsParseError class TestStringMethods(unittest.TestCase): @@ -54,6 +54,10 @@ class TestStringMethods(unittest.TestCase): def test_spot_beacons(self): self.parse_valid_beacon_data_file(filename='spot.txt', beacon_type='spot') + def test_generic_beacons(self): + message = parse("EPZR>WTFDSTCALL,TCPIP*,qAC,GLIDERN1:>093456h this is a comment") + self.assertEqual(message['beacon_type'], 'generic') + def test_fail_parse_aprs_none(self): with self.assertRaises(TypeError): parse(None) @@ -66,10 +70,6 @@ class TestStringMethods(unittest.TestCase): with self.assertRaises(AprsParseError): parse("Lachens>APRS,TCPIwontbeavalidstring") - def test_fail_bad_dstcall(self): - with self.assertRaises(OgnParseError): - parse("EPZR>WTFDSTCALL,TCPIP*,qAC,GLIDERN1:>093456h this is a comment") - def test_v026_chile(self): # receiver beacons from chile have a APRS position message with a pure user comment message = parse("VITACURA1>APRS,TCPIP*,qAC,GLIDERN4:/201146h3322.79SI07034.80W&/A=002329 Vitacura Municipal Aerodrome, Club de Planeadores Vitacura")