def parse(self, hook_func=None, hook_definitions=False): # TODO: Document hook function self._parse_file_header() try: while True: record = self._parse_record() if hook_func: if hook_definitions or isinstance(record, r.DataRecord): hook_func(record) except FitParseComplete: pass except Exception as e: self._file.close() raise FitParseError("Unexpected exception while parsing (%s: %s)" % ( e.__class__.__name__, e, )) # Compare CRC (read last two bytes on _file without recalculating CRC) stored_crc, = struct.unpack('H', self._file.read(2)) self._file.close() if stored_crc != self._crc: raise FitParseError("Invalid CRC")
def parse(self, hook_func=None, hook_definitions=False): # TODO: Document hook function self._parse_file_header() try: while True: record = self._parse_record() if hook_func: if hook_definitions or isinstance(record, r.DataRecord): hook_func(record) except FitParseComplete: pass except Exception, e: self._file.close() raise FitParseError("Unexpected exception while parsing (%s: %s)" % ( e.__class__.__name__, e, ))
def parse(self, *args, **kwargs): return_value = super(Activity, self).parse(*args, **kwargs) if self.records[0].get_data('type') != 'activity': raise FitParseError("File parsed is not an activity file.") return return_value
def throw_exception(error): raise FitParseError("Bad .FIT file header: %s" % error)
class FitFile(object): FILE_HEADER_FMT = '2BHI4s' RECORD_HEADER_FMT = 'B' # First two bytes of a definition, to get endian_ness DEFINITION_PART1_FMT = '2B' # Second part, relies on endianness and tells us how large the rest is DEFINITION_PART2_FMT = 'HB' # Field definitions DEFINITION_PART3_FIELDDEF_FMT = '3B' CRC_TABLE = ( 0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401, 0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400, ) def __init__(self, f): ''' Create a fit file. Argument f can be an open file-like object or a filename ''' if isinstance(f, basestring): f = open(f, 'rb') # Private: call FitFile._read(), don't read from this. Important for CRC. self._file = f self._file_size = os.path.getsize(f.name) self._data_read = 0 self._crc = 0 self._last_timestamp = None self._global_messages = {} self.definitions = [] self.records = [] def get_records_by_type(self, t): # TODO: let t be a list/tuple of arbitary types (str, num, actual type) if isinstance(t, str): return (rec for rec in self.records if rec.type.name == t) elif isinstance(t, int): return (rec for rec in self.records if rec.type.num == t) elif isinstance(t, rec.MessageType): return (rec for rec in self.records if rec.type == t) else: return () def get_records_as_dicts(self, t=None, with_ommited_fields=False): if t is None: records = self.records else: records = self.get_records_by_type(t) return (rec for rec in (rec.as_dict(with_ommited_fields) for rec in records) if rec) def parse(self, hook_func=None, hook_definitions=False): # TODO: Document hook function self._parse_file_header() try: while True: record = self._parse_record() if hook_func: if hook_definitions or isinstance(record, r.DataRecord): hook_func(record) except FitParseComplete: pass except Exception, e: self._file.close() raise FitParseError("Unexpected exception while parsing (%s: %s)" % ( e.__class__.__name__, e, )) # Compare CRC (read last two bytes on _file without recalculating CRC) stored_crc, = struct.unpack('H', self._file.read(2)) self._file.close() if stored_crc != self._crc: raise FitParseError("Invalid CRC")