Skip to content

Commit

Permalink
Dodge homeworld-importance jam-ups in star lines
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberiaResurrection committed Nov 15, 2023
1 parent f2586df commit da77572
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
18 changes: 18 additions & 0 deletions PyRoute/TradeCodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions Tests/testTradeCode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit da77572

Please sign in to comment.