# Current message isn't compressed TS data if not hasattr(self._definition.header, "offset"): return timestamp, offset # Compressed TS data message: calculate new TS and update model timestamp += (self._definition.header.offset - offset) & TIMESTAMP_MASK offset = self._definition.header.offset self._meta.names[TIMESTAMP_FIELD_NUM] = TIMESTAMP_FIELD_NAME ts_field = self._meta.model[TIMESTAMP_FIELD_NUM] = \ LocalDateTime(TIMESTAMP_FIELD_NUM) setattr(self, TIMESTAMP_FIELD_NAME, ts_field._load(timestamp)) return timestamp, offset KNOWN = get_known(__name__, Message, key="msg_type") def register(message_cls): if not issubclass(message_cls, Message): raise ValueError( "%s should be subclass of Message" % message_cls.__name__) if not isinstance(message_cls.msg_type, int): raise ValueError( "%s should have defined message type" % message_cls.__name__) if not message_cls._meta.model: raise ValueError( "%s should have not empty model" % message_cls.__name__) KNOWN[message_cls.msg_type] = message_cls
return self def __add__(self, other): self.offset = other return self def _load(self, data): if not self.scale and not self.offset: return super(BinaryType, self)._load(data) value = float(data) if self.scale: value /= float(self.scale) if self.offset: value -= float(self.offset) return value def _save(self, value): if not self.scale and not self.offset: return super(BinaryType, self)._save(value) data = value if self.offset: data += self.offset if self.scale: data *= self.scale return int(data) KNOWN = get_known(__name__, Type)
# Current message isn't compressed TS data if not hasattr(self._definition.header, "offset"): return timestamp, offset # Compressed TS data message: calculate new TS and update model timestamp += (self._definition.header.offset - offset) & TIMESTAMP_MASK offset = self._definition.header.offset self._meta.names[TIMESTAMP_FIELD_NUM] = TIMESTAMP_FIELD_NAME ts_field = self._meta.model[TIMESTAMP_FIELD_NUM] = \ LocalDateTime(TIMESTAMP_FIELD_NUM) setattr(self, TIMESTAMP_FIELD_NAME, ts_field._load(timestamp)) return timestamp, offset KNOWN = get_known(__name__, Message, key="msg_type") def register(message_cls): if not issubclass(message_cls, Message): raise ValueError("%s should be subclass of Message" % message_cls.__name__) if not isinstance(message_cls.msg_type, int): raise ValueError("%s should have defined message type" % message_cls.__name__) if not message_cls._meta.model: raise ValueError("%s should have not empty model" % message_cls.__name__) KNOWN[message_cls.msg_type] = message_cls
mcs = [FitFile] if self.file_id: mixin_cls = KNOWN.get(self.file_id.filetype) if mixin_cls: if issubclass(mixin_cls, FitFile): mcs = [mixin_cls] else: mcs.append(mixin_cls) self.__class__ = type(mcs[-1].__name__, tuple(mcs), {}) def _validate(self, i, value=None): if value and not isinstance(value, Message): raise ValueError("Item should be instance of %s type" % Message.__name__) if not value: # removing if i == 0 and len(self.body) > 1 and self.file_id: raise IndexError( "Can't remove file id record from not empty file") else: # inserting or updating if i == 0 and len(self.body) > 1 and self.file_id: raise IndexError( "Can't update file id record of not empty file") elif not isinstance(value, tuple(self.record_types)): raise TypeError( "%s doesn't support %s records" % (self.__class__.__name__, value.__class__.__name__)) KNOWN = get_known(__name__, FileLike)
return instance def _apply_mixin(self): from fit import FitFile mcs = [FitFile] if self.file_id: mixin_cls = KNOWN.get(self.file_id.filetype) if mixin_cls: if issubclass(mixin_cls, FitFile): mcs = [mixin_cls] else: mcs.append(mixin_cls) self.__class__ = type(mcs[-1].__name__, tuple(mcs), {}) def _validate(self, i, value=None): if value and not isinstance(value, Message): raise ValueError("Item should be instance of %s type" % Message.__name__) if not value: # removing if i == 0 and len(self.body) > 1 and self.file_id: raise IndexError("Can't remove file id record from not empty file") else: # inserting or updating if i == 0 and len(self.body) > 1 and self.file_id: raise IndexError("Can't update file id record of not empty file") elif not isinstance(value, tuple(self.record_types)): raise TypeError("%s doesn't support %s records" % (self.__class__.__name__, value.__class__.__name__)) KNOWN = get_known(__name__, FileLike)