Skip to content

Commit ad0c137

Browse files
committed
Adds sc2reader.exceptions.ParseError for parse errors.
1 parent 3104473 commit ad0c137

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

sc2reader/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ def __init__(self, msg, type, code, location, replay=None, game_events=[], buff
1818
def __str__(self):
1919
return "{0}, Type: {1}, Code: {2}".format(self.msg, self.type, self.code)
2020

21+
class ParseError(SC2ReaderError):
22+
pass
23+
2124
class ProcessError(SC2ReaderError):
2225
pass
2326

sc2reader/parsers.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from .objects import *
55
from .utils import BIG_ENDIAN,LITTLE_ENDIAN
6-
6+
from .exceptions import ParseError
77

88
class SetupParser(object):
99
def parse_join_event(self, buffer, frames, type, code, pid):
@@ -131,7 +131,7 @@ def parse_ability_event(self, buffer, frames, type, code, pid):
131131
return UnknownLocationAbilityEvent(frames, pid, type, code, None)
132132

133133
else:
134-
raise TypeError("Unknown atype & 0x40: flag %X at frame %X" % (flag,frames))
134+
raise ParseError("Unknown atype & 0x40: flag %X at frame %X" % (flag,frames))
135135

136136
elif atype & 0x80: # right-click on target?
137137
# ability (2), object id (4), object type (2), ?? (10)
@@ -154,10 +154,10 @@ def parse_ability_event(self, buffer, frames, type, code, pid):
154154
else:
155155
# print hex(atype)
156156
# print hex(buffer.cursor)
157-
raise TypeError()
157+
raise ParseError()
158158

159159
# print "%s - %s" % (hex(atype),hex(flag))
160-
raise TypeError("Shouldn't be here EVER!")
160+
raise ParseError("Shouldn't be here EVER!")
161161

162162
def parse_selection_event(self, buffer, frames, type, code, pid):
163163
bank = code >> 4
@@ -210,7 +210,7 @@ def parse_hotkey_event(self, buffer, frames, type, code, pid):
210210
elif action == 2:
211211
return GetHotkeyEvent(frames, pid, type, code, hotkey, overlay)
212212
else:
213-
raise TypeError("Hotkey Action '{0}' unknown")
213+
raise ParseError("Hotkey Action '{0}' unknown")
214214

215215
class ActionParser_18574(ActionParser_16561):
216216
def parse_ability_event(self, buffer, frames, type, code, pid):
@@ -282,10 +282,10 @@ def parse_ability_event(self, buffer, frames, type, code, pid):
282282
else:
283283
# print hex(atype)
284284
# print hex(buffer.cursor)
285-
raise TypeError()
285+
raise ParseError()
286286

287287
# print "%s - %s" % (hex(atype),hex(flag))
288-
raise TypeError("Shouldn't be here EVER!")
288+
raise ParseError("Shouldn't be here EVER!")
289289

290290
class Unknown2Parser(object):
291291
def parse_0206_event(self, buffer, frames, type, code, pid):

sc2reader/readers.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from .parsers import *
22
from .objects import *
33
from .utils import AttributeDict, LITTLE_ENDIAN
4-
import exceptions
4+
from .exceptions import ParseError, ReadError
55

66
class InitDataReader(object):
77
def __call__(self,buffer, replay):
@@ -237,16 +237,16 @@ def __call__(self, buffer, replay):
237237
# If the type is not a key in the PARSERS lookup table we
238238
# probably incorrectly parsed the previous event
239239
# TODO: Raise an error instead an store the previous event
240-
raise exceptions.ReadError("Unknown event type", type, code, start, replay, game_events, buffer)
240+
raise ReadError("Unknown event type", type, code, start, replay, game_events, buffer)
241241

242-
except TypeError:
242+
except ParseError:
243243
# For some reason, the type handler that we delegated to didn't
244244
# recognize the event code that we extracated.
245245
# TODO: Do something meaningful here
246-
raise exceptions.ReadError("Unknown event code", type, code, start, replay, game_events, buffer)
246+
raise ReadError("Unknown event code", type, code, start, replay, game_events, buffer)
247247

248-
except exceptions.ReadError as e:
249-
raise exceptions.ReadError(e.msg, replay, game_events, buffer, start)
248+
except ReadError as e:
249+
raise ReadError(e.msg, replay, game_events, buffer, start)
250250

251251
# Because events are parsed in a bitwise fashion, they sometimes
252252
# leave the buffer in a bitshifted state. Each new event always
@@ -265,7 +265,7 @@ def get_setup_parser(self, code):
265265
elif code in (0x05,): return self.parse_start_event
266266
else:
267267
# TODO: Raise a better error
268-
raise exceptions.ReadError("Unknown Setup Parser Code {0}".format(code))
268+
raise ReadError("Unknown Setup Parser Code {0}".format(code))
269269

270270
def get_action_parser(self, code):
271271
# The action events are always associated with a particular player and
@@ -277,7 +277,7 @@ def get_action_parser(self, code):
277277
elif code & 0x0F == 0xF: return self.parse_transfer_event
278278
else:
279279
# TODO: Raise a better error
280-
raise exceptions.ReadError("Unknown Action Parser Code {0}".format(code))
280+
raise ReadError("Unknown Action Parser Code {0}".format(code))
281281

282282
def get_unknown2_parser(self, code):
283283
# While its unclear what these events represent, they are MUCH more
@@ -288,7 +288,7 @@ def get_unknown2_parser(self, code):
288288
elif code == 0x0E: return self.parse_020E_event
289289
else:
290290
# TODO: Raise a better error
291-
raise exceptions.ReadError("Unknown Unknown2 Parser Code {0}".format(code))
291+
raise ReadError("Unknown Unknown2 Parser Code {0}".format(code))
292292

293293
def get_camera_parser(self, code):
294294
# Each player's camera control events are recorded, separately from the
@@ -301,7 +301,7 @@ def get_camera_parser(self, code):
301301
elif code == 0x0a: return self.parse_camera0A_event
302302
else:
303303
# TODO: Raise a better error
304-
raise exceptions.ReadError("Unknown Camera Parser Code {0}".format(code))
304+
raise ReadError("Unknown Camera Parser Code {0}".format(code))
305305

306306
def get_unknown4_parser(self, code):
307307
# I don't know anything about these events. Any parse information for
@@ -317,7 +317,7 @@ def get_unknown4_parser(self, code):
317317
elif code & 0x0F == 0x0C: return self.parse_04XC_event
318318
else:
319319
# TODO: Raise a better error
320-
raise exceptions.ReadError("Unknown Unknown4 Parser Code {0}".format(code))
320+
raise ReadError("Unknown Unknown4 Parser Code {0}".format(code))
321321

322322
# The storage format for many of the game events has changed, sometimes
323323
# dramatically, over time. To handle this inconsistency sc2reader uses mixins

0 commit comments

Comments
 (0)