From c9f4b4f779c0b4e5cf7cbe101f06fdfb82fa15a7 Mon Sep 17 00:00:00 2001 From: Rossen Georgiev Date: Sat, 12 Mar 2022 14:24:28 +0000 Subject: [PATCH] parse: fix weather_data mangling comments fix #71 --- aprslib/parsing/weather.py | 16 ++++++++++------ tests/test_parse_weather_data.py | 10 +++++----- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/aprslib/parsing/weather.py b/aprslib/parsing/weather.py index 1ed140f..0502d21 100644 --- a/aprslib/parsing/weather.py +++ b/aprslib/parsing/weather.py @@ -48,13 +48,17 @@ def parse_weather_data(body): body = re.sub(r"^([0-9]{3})/([0-9]{3})", "c\\1s\\2", body) body = body.replace('s', 'S', 1) - data = re.findall(r"([cSgtrpPlLs#]\d{3}|t-\d{2}|h\d{2}|b\d{5}|s\.\d{2}|s\d\.\d)", body) - data = map(lambda x: (key_map[x[0]] , val_map[x[0]](x[1:])), data) + # match as many parameters from the start, rest is comment + data = re.match(r"^([cSgtrpPlLs#][0-9\-\. ]{3}|h[0-9\. ]{2}|b[0-9\. ]{5})+", body) - parsed.update(dict(data)) - - # strip weather data - body = re.sub(r"([cSgtrpPlLs#][0-9\-\. ]{3}|h[0-9\. ]{2}|b[0-9\. ]{5})", '', body) + if data: + data = data.group() + # split out data from comment + body = body[len(data):] + # parse all weather parameters + data = re.findall(r"([cSgtrpPlLs#]\d{3}|t-\d{2}|h\d{2}|b\d{5}|s\.\d{2}|s\d\.\d)", data) + data = map(lambda x: (key_map[x[0]] , val_map[x[0]](x[1:])), data) + parsed.update(dict(data)) return (body, parsed) diff --git a/tests/test_parse_weather_data.py b/tests/test_parse_weather_data.py index 53c8251..7793525 100644 --- a/tests/test_parse_weather_data.py +++ b/tests/test_parse_weather_data.py @@ -204,11 +204,11 @@ class ParseCommentWeather(unittest.TestCase): def test_positionless_packet(self): expected = { - 'comment': 'wRSW', + 'comment': '.ABS1.2CDF', 'format': 'wx', 'from': 'A', 'path': [], - 'raw': 'A>B:_10090556c220s004g005t077r010p020P030h50b09900s5.5wRSW', + 'raw': 'A>B:_10090556c220s004g005t077r010p020P030h50b09900s5.5.ABS1.2CDF', 'to': 'B', 'via': '', 'wx_raw_timestamp': '10090556', @@ -226,11 +226,11 @@ class ParseCommentWeather(unittest.TestCase): } } - packet = "A>B:_10090556c220s004g005t077r010p020P030h50b09900s5.5wRSW" + packet = "A>B:_10090556c220s004g005t077r010p020P030h50b09900s5.5.ABS1.2CDF" self.assertEqual(expected, parse(packet)) - packet2 = "A>B:_10090556c220s112g t r h b wRSW" + packet2 = "A>B:_10090556c220s112g t r h b .ABS1.2CDF" expected['raw'] = packet2 expected['weather'] = { "wind_direction": 220, @@ -239,7 +239,7 @@ class ParseCommentWeather(unittest.TestCase): self.assertEqual(expected, parse(packet2)) - packet3 = "A>B:_10090556c220s112g...t...r...p...P...b.....wRSW" + packet3 = "A>B:_10090556c220s112g...t...r...p...P...b......ABS1.2CDF" expected['raw'] = packet3 expected['weather'] = { "wind_direction": 220,