From da77572ef00a3fecf8818081fd291b32262ff07e Mon Sep 17 00:00:00 2001 From: Alex Goodwin Date: Wed, 15 Nov 2023 13:05:11 +1000 Subject: [PATCH] Dodge homeworld-importance jam-ups in star lines --- PyRoute/TradeCodes.py | 18 ++++++++++++++++++ Tests/testTradeCode.py | 23 +++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/PyRoute/TradeCodes.py b/PyRoute/TradeCodes.py index 5a7f638f1..2c6e5af62 100644 --- a/PyRoute/TradeCodes.py +++ b/PyRoute/TradeCodes.py @@ -166,6 +166,10 @@ def _preprocess_initial_codes(self, initial_codes): codes.append(raw) continue if raw.endswith(')') or raw.endswith(']'): # this _is_ a sophont code + if raw.startswith('[') and raw.endswith(']'): + raw = self._trim_overlong_homeworld_code(raw) # trim overlong _major_ race homeworld + elif raw.startswith('(') and raw.endswith(')'): + raw = self._trim_overlong_homeworld_code(raw) # trim overlong _minor_ race homeworld codes.append(raw) continue if i < num_codes - 1: @@ -182,8 +186,22 @@ def _preprocess_initial_codes(self, initial_codes): return codes, initial_codes + def _trim_overlong_homeworld_code(self, raw): + # We're assuming raw is a (homeworld) code - not handling pop codes at the moment + # If the homeworld string itself exceeds 35 characters in length, it will jam up against the left side of + # the importance code in the starline, which both looks ugly and causes re-parsing havoc. + left_bracket = raw[0] + right_bracket = ']' if '[' == left_bracket else ')' + + trim = raw[1:-1] + if 35 < len(trim): + trim = trim[0:35] + + return left_bracket + trim + right_bracket + def _process_homeworld(self, homeworld, homeworlds_found, initial_codes): full_name = re.sub(r'\(([^)]+)\)\d?', r'\1', homeworld) + homeworlds_found.append(homeworld) match = TradeCodes.search.match(homeworld) if match is None: # try again with major-raceversion diff --git a/Tests/testTradeCode.py b/Tests/testTradeCode.py index 18f30947f..45739ed3e 100644 --- a/Tests/testTradeCode.py +++ b/Tests/testTradeCode.py @@ -312,6 +312,29 @@ def testSharedHomeworld(self): result, msg = nu_code.is_well_formed() self.assertTrue(result, msg) + def testHandleOverlyLongSophontName(self): + cases = [ + ('Minor race', '(', ')'), + ('Major race', '(', ')') + ] + + for msg, left_bracket, right_bracket in cases: + with self.subTest(msg): + line = left_bracket + '000000000000000000000000000000000000' + right_bracket + code = TradeCodes(line) + sophont = line[1:36] # strip the brackets and trim what's left to 35 characters + + self.assertEqual(1, len(code.sophont_list), "Actual homeworld code should result in sophont") + self.assertEqual(1, len(code.homeworld_list), "Actual homeworld code should result in homeworld") + self.assertEqual(['0000W'], code.sophont_list) + self.assertEqual([sophont], code.homeworld_list) + + # verify shortened soph code turns up + expected_sophont = left_bracket + sophont + right_bracket + nu_line = str(code) + self.assertEqual(expected_sophont, nu_line) + + if __name__ == "__main__": # import sys;sys.argv = ['', 'Test.testName'] unittest.main()