<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def parse(self):
 """ Parse a replay """


        self.important("Parsing demo file '%s'" % (self.filename, ))

        with open(self.filename, 'rb') as f:
            reader = Reader(StringIO(f.read()))

            filestamp = reader.read(8)
            offset = reader.read_int32()

            if filestamp != "PBUFDEM\x00":
                raise ValueError("Invalid replay - incorrect filestamp")

            buff = StringIO(f.read())

            frame = 0
            more = True
            while more and reader.remaining > 0:
                cmd = reader.read_vint32()
                tick = reader.read_vint32()
                compressed = False

                if cmd & demo_pb2.DEM_IsCompressed:
                    compressed = True
                    cmd = cmd & ~demo_pb2.DEM_IsCompressed

                if cmd not in messages.MESSAGE_TYPES:
                    raise KeyError("Unknown message type found")

                message_type = messages.MESSAGE_TYPES[cmd]
                message = reader.read_message(message_type, compressed)

                self.info('%s: %s' % (frame, message_type))
                self.worthless(message)

                self.run_hooks(message)

                self.info('|%s' % ('-' * 79, ))

                frame += 1

                if self.frames and frame > self.frames:
                    break