def calculate(self, bag_file):
     """Calculate mileage."""
     try:
         drive_event = drive_event_pb2.DriveEvent()
         reader = RecordReader(bag_file)
     except:
         print("can't open bag")
     else:
         f = file('/apollo/test.txt', 'a')
         for msg in reader.read_messages():
             if msg.topic == kEventTopic:
                 drive_event.ParseFromString(msg.message)
                 msg_time = time.localtime(drive_event.header.timestamp_sec)
                 f.write(time.strftime("%Y-%m-%d %H:%M:%S", msg_time))
                 f.write(str(drive_event.type) + ":")
                 f.write(drive_event.event.encode('utf-8') + '\n')
         f.close()
    def calculate(self, bag_file):
        """
        Calculate mileage
        """
        try:
	    drive_event = drive_event_pb2.DriveEvent()
            reader = RecordReader(bag_file)
        except Exception:
            print('Cannot open bag file %s' % bag_file)
        else:
            with open('/apollo/test.txt', 'a') as fp:
                for msg in reader.read_messages():
                    if msg.topic == kEventTopic:
                        drive_event.ParseFromString(msg.message)
                        msg_time = time.localtime(drive_event.header.timestamp_sec)
                        fp.write(time.strftime("%Y-%m-%d %H:%M:%S", msg_time))
                        fp.write(str(drive_event.type) + ':')
                        fp.write(drive_event.event.encode('utf-8') + '\n')