From 59987e96d09c4a77229ebb0c2f789e684d8caef0 Mon Sep 17 00:00:00 2001 From: Kevin Leung Date: Wed, 25 Jul 2018 22:18:09 -0700 Subject: [PATCH 1/4] #63 cast players in TrackerEvent.__str__ --- sc2reader/events/tracker.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sc2reader/events/tracker.py b/sc2reader/events/tracker.py index 41b5c2f2..fe3e6f78 100644 --- a/sc2reader/events/tracker.py +++ b/sc2reader/events/tracker.py @@ -230,7 +230,7 @@ def __init__(self, frames, data, build): self.ff_vespene_lost_technology = clamp(self.stats[38]) if build >= 26490 else None def __str__(self): - return self._str_prefix() + "{0: >15} - Stats Update".format(self.player) + return self._str_prefix() + "{0: >15} - Stats Update".format(str(self.player)) class UnitBornEvent(TrackerEvent): @@ -291,7 +291,7 @@ def __init__(self, frames, data, build): self.location = (self.x, self.y) def __str__(self): - return self._str_prefix() + "{0: >15} - Unit born {1}".format(self.unit_upkeeper, self.unit) + return self._str_prefix() + "{0: >15} - Unit born {1}".format(str(self.unit_upkeeper), self.unit) class UnitDiedEvent(TrackerEvent): @@ -361,7 +361,7 @@ def __init__(self, frames, data, build): self.killing_unit_id = self.killing_unit_index << 18 | self.killing_unit_recycle def __str__(self): - return self._str_prefix() + "{0: >15} - Unit died {1}.".format(self.unit.owner, self.unit) + return self._str_prefix() + "{0: >15} - Unit died {1}.".format(str(self.unit.owner), self.unit) class UnitOwnerChangeEvent(TrackerEvent): @@ -397,7 +397,7 @@ def __init__(self, frames, data, build): self.unit_controller = None def __str__(self): - return self._str_prefix() + "{0: >15} took {1}".format(self.unit_upkeeper, self.unit) + return self._str_prefix() + "{0: >15} took {1}".format(str(self.unit_upkeeper), self.unit) class UnitTypeChangeEvent(TrackerEvent): @@ -425,7 +425,7 @@ def __init__(self, frames, data, build): self.unit_type_name = data[2].decode('utf8') def __str__(self): - return self._str_prefix() + "{0: >15} - Unit {0} type changed to {1}".format(self.unit.owner, self.unit, self.unit_type_name) + return self._str_prefix() + "{0: >15} - Unit {0} type changed to {1}".format(str(self.unit.owner), self.unit, self.unit_type_name) class UpgradeCompleteEvent(TrackerEvent): @@ -448,7 +448,7 @@ def __init__(self, frames, data, build): self.count = data[2] def __str__(self): - return self._str_prefix() + "{0: >15} - {1}upgrade completed".format(self.player, self.upgrade_type_name) + return self._str_prefix() + "{0: >15} - {1} upgrade completed".format(str(self.player), self.upgrade_type_name) class UnitInitEvent(TrackerEvent): @@ -504,7 +504,7 @@ def __init__(self, frames, data, build): self.location = (self.x, self.y) def __str__(self): - return self._str_prefix() + "{0: >15} - Unit initiated {1}".format(self.unit_upkeeper, self.unit) + return self._str_prefix() + "{0: >15} - Unit initiated {1}".format(str(self.unit_upkeeper), self.unit) class UnitDoneEvent(TrackerEvent): @@ -528,7 +528,7 @@ def __init__(self, frames, data, build): self.unit = None def __str__(self): - return self._str_prefix() + "{0: >15} - Unit {1} done".format(self.unit.owner, self.unit) + return self._str_prefix() + "{0: >15} - Unit {1} done".format(str(self.unit.owner), self.unit) class UnitPositionsEvent(TrackerEvent): From fdb913fe7e5bd59eac37818e7015cb57f5b3cb90 Mon Sep 17 00:00:00 2001 From: Kevin Leung Date: Wed, 25 Jul 2018 22:18:09 -0700 Subject: [PATCH 2/4] #63 cast players in TrackerEvent.__str__ --- sc2reader/events/tracker.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sc2reader/events/tracker.py b/sc2reader/events/tracker.py index 41b5c2f2..fe3e6f78 100644 --- a/sc2reader/events/tracker.py +++ b/sc2reader/events/tracker.py @@ -230,7 +230,7 @@ def __init__(self, frames, data, build): self.ff_vespene_lost_technology = clamp(self.stats[38]) if build >= 26490 else None def __str__(self): - return self._str_prefix() + "{0: >15} - Stats Update".format(self.player) + return self._str_prefix() + "{0: >15} - Stats Update".format(str(self.player)) class UnitBornEvent(TrackerEvent): @@ -291,7 +291,7 @@ def __init__(self, frames, data, build): self.location = (self.x, self.y) def __str__(self): - return self._str_prefix() + "{0: >15} - Unit born {1}".format(self.unit_upkeeper, self.unit) + return self._str_prefix() + "{0: >15} - Unit born {1}".format(str(self.unit_upkeeper), self.unit) class UnitDiedEvent(TrackerEvent): @@ -361,7 +361,7 @@ def __init__(self, frames, data, build): self.killing_unit_id = self.killing_unit_index << 18 | self.killing_unit_recycle def __str__(self): - return self._str_prefix() + "{0: >15} - Unit died {1}.".format(self.unit.owner, self.unit) + return self._str_prefix() + "{0: >15} - Unit died {1}.".format(str(self.unit.owner), self.unit) class UnitOwnerChangeEvent(TrackerEvent): @@ -397,7 +397,7 @@ def __init__(self, frames, data, build): self.unit_controller = None def __str__(self): - return self._str_prefix() + "{0: >15} took {1}".format(self.unit_upkeeper, self.unit) + return self._str_prefix() + "{0: >15} took {1}".format(str(self.unit_upkeeper), self.unit) class UnitTypeChangeEvent(TrackerEvent): @@ -425,7 +425,7 @@ def __init__(self, frames, data, build): self.unit_type_name = data[2].decode('utf8') def __str__(self): - return self._str_prefix() + "{0: >15} - Unit {0} type changed to {1}".format(self.unit.owner, self.unit, self.unit_type_name) + return self._str_prefix() + "{0: >15} - Unit {0} type changed to {1}".format(str(self.unit.owner), self.unit, self.unit_type_name) class UpgradeCompleteEvent(TrackerEvent): @@ -448,7 +448,7 @@ def __init__(self, frames, data, build): self.count = data[2] def __str__(self): - return self._str_prefix() + "{0: >15} - {1}upgrade completed".format(self.player, self.upgrade_type_name) + return self._str_prefix() + "{0: >15} - {1} upgrade completed".format(str(self.player), self.upgrade_type_name) class UnitInitEvent(TrackerEvent): @@ -504,7 +504,7 @@ def __init__(self, frames, data, build): self.location = (self.x, self.y) def __str__(self): - return self._str_prefix() + "{0: >15} - Unit initiated {1}".format(self.unit_upkeeper, self.unit) + return self._str_prefix() + "{0: >15} - Unit initiated {1}".format(str(self.unit_upkeeper), self.unit) class UnitDoneEvent(TrackerEvent): @@ -528,7 +528,7 @@ def __init__(self, frames, data, build): self.unit = None def __str__(self): - return self._str_prefix() + "{0: >15} - Unit {1} done".format(self.unit.owner, self.unit) + return self._str_prefix() + "{0: >15} - Unit {1} done".format(str(self.unit.owner), self.unit) class UnitPositionsEvent(TrackerEvent): From 5cdfad17f6245a9680ad05d2c7b24dd05a47e7ef Mon Sep 17 00:00:00 2001 From: Gus Foley Date: Wed, 31 Oct 2018 02:39:41 -0700 Subject: [PATCH 3/4] Add test for print functionality. Load a lotv replay and print all the events to ensure they print. The output from the prints have been redirected to a stream using StringIO and then checked for the last few events to occur. --- test_replays/test_all.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test_replays/test_all.py b/test_replays/test_all.py index 61e73302..5d1f2c93 100644 --- a/test_replays/test_all.py +++ b/test_replays/test_all.py @@ -11,6 +11,11 @@ import unittest2 as unittest else: import unittest +# StringIO was changed in python 3 +try: + from io import StringIO +except ImportError: + from StringIO import StringIO import sc2reader from sc2reader.exceptions import CorruptTrackerFileError @@ -582,6 +587,15 @@ def test_65895(self): factory = sc2reader.factories.SC2Factory() replay = factory.load_replay(replayfilename) + def test_event_print(self): + replay = sc2reader.load_replay("test_replays/lotv/lotv1.SC2Replay") + with StringIO() as capturedOutput: + sys.stdout = capturedOutput + for event in replay.events: + print(event) + self.assertIn("PlayerLeaveEvent", capturedOutput.getvalue()) + sys.stdout = sys.__stdout__ + class TestGameEngine(unittest.TestCase): class TestEvent(object): From 8c9072873dadbaae4f78c64aa86349e2ea57addb Mon Sep 17 00:00:00 2001 From: Gus Foley Date: Fri, 2 Nov 2018 00:18:42 -0700 Subject: [PATCH 4/4] Fix Python 2 issues with test_event_print. Logic issue with conditional import. StringIO.StringIO does not support usage of the with keyword. --- test_replays/test_all.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test_replays/test_all.py b/test_replays/test_all.py index 5d1f2c93..1667fe1a 100644 --- a/test_replays/test_all.py +++ b/test_replays/test_all.py @@ -13,9 +13,9 @@ import unittest # StringIO was changed in python 3 try: - from io import StringIO -except ImportError: from StringIO import StringIO +except ImportError: + from io import StringIO import sc2reader from sc2reader.exceptions import CorruptTrackerFileError @@ -589,12 +589,12 @@ def test_65895(self): def test_event_print(self): replay = sc2reader.load_replay("test_replays/lotv/lotv1.SC2Replay") - with StringIO() as capturedOutput: - sys.stdout = capturedOutput - for event in replay.events: - print(event) - self.assertIn("PlayerLeaveEvent", capturedOutput.getvalue()) - sys.stdout = sys.__stdout__ + sys.stdout = capturedOutput = StringIO() + for event in replay.events: + print(event) + self.assertIn("PlayerLeaveEvent", capturedOutput.getvalue()) + sys.stdout = sys.__stdout__ + capturedOutput.close() class TestGameEngine(unittest.TestCase):