From 06bd76ba470b684e45454b441fe3c3682276d89a Mon Sep 17 00:00:00 2001 From: Alex Goodwin Date: Mon, 18 Nov 2024 23:11:11 +1000 Subject: [PATCH] Fix corner case in transformer --- PyRoute/Inputs/BaseTransformer.py | 12 ++++++++++++ PyRoute/Inputs/ParseStarInput.py | 4 +++- Tests/Hypothesis/testStar.py | 1 + 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/PyRoute/Inputs/BaseTransformer.py b/PyRoute/Inputs/BaseTransformer.py index 58d90e363..574e28c3a 100644 --- a/PyRoute/Inputs/BaseTransformer.py +++ b/PyRoute/Inputs/BaseTransformer.py @@ -391,9 +391,21 @@ def trim_raw_string(self, tree): self.raw = self.raw[index:] def _square_up_parsed_zero(self, rawstring, parsed): + from PyRoute.Inputs.ParseStarInput import ParseStarInput bitz = [item for item in rawstring.split(' ') if '' != item] if 3 == len(bitz) and bitz[0] == parsed['nobles'] and bitz[1] == parsed['base'] and bitz[2] == parsed['zone']: return parsed + if 2 == len(bitz) and "" == parsed['zone']: + if 2 < len(bitz[0]): # bitz[0] can only possibly be nobles, so return + return parsed + if 1 < len(bitz[1]): # if bitz[1] is more than one char, it can't be a trade zone, so return + return parsed + non_noble = [item for item in bitz[0] if item not in ParseStarInput.valid_nobles] + if 0 < len(non_noble): # If one or more chars in bitz[0] is not a valid noble call, then we have a base code and trade zone + parsed['zone'] = parsed['base'] + parsed['base'] = parsed['nobles'] + parsed['nobles'] = '' + return parsed if 3 == len(bitz): parsed['nobles'] = bitz[0] parsed['base'] = bitz[1] diff --git a/PyRoute/Inputs/ParseStarInput.py b/PyRoute/Inputs/ParseStarInput.py index faf2f6c95..57aa59085 100644 --- a/PyRoute/Inputs/ParseStarInput.py +++ b/PyRoute/Inputs/ParseStarInput.py @@ -50,6 +50,8 @@ class ParseStarInput: station_parser = None station_transformer = None deep_space = {} + valid_zone = 'arufgbARUFGB-' + valid_nobles = 'BCcDEeFfGH-' @staticmethod def parse_line_into_star_core(star, line, sector, pop_code, ru_calc, fix_pop=False): @@ -84,7 +86,7 @@ def parse_line_into_star_core(star, line, sector, pop_code, ru_calc, fix_pop=Fal if ('' == star.baseCode) or ('-' != star.baseCode and 1 == len(star.baseCode) and not star.baseCode.isalpha()): star.baseCode = '-' star.zone = data[13].strip() - if not star.zone or star.zone not in 'arufgbARUFGB-': + if not star.zone or star.zone not in ParseStarInput.valid_zone: star.zone = '-' star.zone = star.zone.upper() star.ggCount = 0 if (len(data[14]) < 3 or not data[14][2] or data[14][2] in 'X?') else int(data[14][2], 16) diff --git a/Tests/Hypothesis/testStar.py b/Tests/Hypothesis/testStar.py index c5eae7841..85f5b463e 100644 --- a/Tests/Hypothesis/testStar.py +++ b/Tests/Hypothesis/testStar.py @@ -123,6 +123,7 @@ def setUp(self) -> None: @example('0101 000000000000000 ???????-? 0000000000000 0 0 B - A 000 00') @example('0101 000000000000000 ???????-? 000000000000000 {-0} (000-0) - * A 000 00') @example('0101 000000000000000 ???????-? 000000000000000 {-0} (000-0) - - - A 000 00') + @example("0101 000000000000000 ?000000-0 {3 } (7TG-1) [2IBt] EHEG - 569 9 C5'") def test_parse_line_to_star(self, s): hyp_line = "Hypothesis input: " + s sector = Sector('# Core', '# 0, 0')