Merge branch 'wsmitchell3-third-party-v2'

Close #45
This commit is contained in:
Rossen Georgiev 2021-11-27 13:03:37 +00:00
commit 4d47818791
4 changed files with 87 additions and 2 deletions

View File

@ -41,6 +41,7 @@ from aprslib.parsing.position import *
from aprslib.parsing.mice import * from aprslib.parsing.mice import *
from aprslib.parsing.message import * from aprslib.parsing.message import *
from aprslib.parsing.telemetry import * from aprslib.parsing.telemetry import *
from aprslib.parsing.thirdparty import *
from aprslib.parsing.weather import * from aprslib.parsing.weather import *
@ -158,9 +159,14 @@ def _try_toparse_body(packet_type, body, parsed):
# ] - unused # ] - unused
# ^ - unused # ^ - unused
# } - 3rd party traffic # } - 3rd party traffic
if packet_type in '#$%)*<?T[}': if packet_type in '#$%)*<?T[':
raise UnknownFormat("format is not supported") raise UnknownFormat("format is not supported")
# 3rd party traffic
elif packet_type == '}':
logger.debug("Packet is third-party")
body, result = parse_thirdparty(body)
# user defined # user defined
elif packet_type == ',': elif packet_type == ',':
logger.debug("Packet is invalid format") logger.debug("Packet is invalid format")

View File

@ -0,0 +1,21 @@
import re
from aprslib.parsing.__init__ import parse
from aprslib.exceptions import UnknownFormat
from aprslib.exceptions import ParseError
__all__ = [
'parse_thirdparty',
]
def parse_thirdparty(body):
parsed = {'format':'thirdparty'}
# Parse sub-packet
try:
subpacket = parse(body)
except (UnknownFormat,ParseError) as ukf:
raise
parsed.update({'subpacket':subpacket})
return('',parsed)

View File

@ -66,7 +66,7 @@ class ParseTestCase(unittest.TestCase):
def test_unsupported_formats_raising(self): def test_unsupported_formats_raising(self):
with self.assertRaises(UnknownFormat): with self.assertRaises(UnknownFormat):
for packet_type in '#$%)*,<?T[_{}': for packet_type in '#$%)*,<?T[_{':
packet = "A>B:%saaa" % packet_type packet = "A>B:%saaa" % packet_type
try: try:

58
tests/test_thirdparty.py Normal file
View File

@ -0,0 +1,58 @@
import unittest
from aprslib import parse
from aprslib.exceptions import ParseError, UnknownFormat
class thirdpartyTC(unittest.TestCase):
def test_empty_subpacket(self):
self.assertRaises(ParseError, parse, "A>B:}")
def test_no_body(self):
self.assertRaises(ParseError, parse, "A>B:}C>D")
def test_empty_body(self):
self.assertRaises(ParseError, parse, "A>B:}C>D:")
def testparse_header_exception(self):
self.assertRaises(ParseError, parse, "A>B:}C:asd")
def test_empty_body_of_format_that_is_not_status(self):
self.assertRaises(ParseError, parse, "A>B:}C>D:!")
try:
parse("A>B:}C>D:>")
except:
self.fail("empty status packet shouldn't raise exception")
def test_unsupported_formats_raising(self):
with self.assertRaises(UnknownFormat):
for packet_type in '#$%)*,<?T[_{':
packet = "A>B:}C>D:%saaa" % packet_type
try:
parse(packet)
except UnknownFormat as exp:
self.assertEqual(exp.packet, packet)
raise
def test_valid_thirdparty_msg(self):
packet = "A-1>APRS,B-2,WIDE1*:}C>APU25N,TCPIP,A-1*::DEF :ack56"
result = parse(packet)
self.assertEqual(result['via'],'')
self.assertEqual(result['to'],'APRS')
self.assertEqual(result['from'],'A-1')
self.assertEqual(result['format'],'thirdparty')
self.assertEqual(result['raw'],packet)
self.assertEqual(result['path'],['B-2', 'WIDE1*'])
self.assertEqual(result['subpacket']['raw'],packet[21:])
self.assertEqual(result['subpacket']['via'],'')
self.assertEqual(result['subpacket']['msgNo'],'56')
self.assertEqual(result['subpacket']['from'],'C')
self.assertEqual(result['subpacket']['path'],['TCPIP', 'A-1*'])
self.assertEqual(result['subpacket']['response'],'ack')
self.assertEqual(result['subpacket']['format'],'message')
self.assertEqual(result['subpacket']['to'],'APU25N')
self.assertEqual(result['subpacket']['addresse'],'DEF')