Exemple #1
0
    def __init__(self, ffd):
        self._fd = ffd

        self._header = Header()
        self._body = Body()
        self._crc = Crc()

        self.file_size = fstat(self._fd.fileno()).st_size
        self.header_chunk = ""
Exemple #2
0
class Header(object):
    def __init__(self):
        self._format = Struct("<BBHL4s")

        self.size = None

        self.data_type = ".FIT"
        self.data_size = None

        self.crc = Crc()

        self.protocol_version = 16
        self.profile_version = 1005

    def __nonzero__(self):
        return self.size is not None

    def __repr__(self):
        return '<%s protocol=%d profile=%d crc=%r>' % (
            self.__class__.__name__,
            self.protocol_version,
            self.profile_version,
            self.crc
        )

    @property
    def total_size(self):
        return self.size + self.data_size + self.crc.size

    @property
    def valid(self):
        return self.data_type == ".FIT"

    def read(self, chunk):
        (
            self.size, self.protocol_version, self.profile_version,
            self.data_size, self.data_type
        ) = self._format.unpack(chunk[:12])

        if len(chunk) == 14:
            self.crc.read(chunk[12:])

            if self.crc.value and not self.crc.check(chunk[:12]):
                raise BodyFormatError("Invalid CRC %x, should be %x" % (
                    compute_crc(chunk[:12]), self.crc.value))

    def write(self):
        self.size = 14
        chunk = self._format.pack(self.size, self.protocol_version,
                                  self.profile_version, self.data_size,
                                  self.data_type)
        self.crc.value = compute_crc(chunk)
        return chunk + self.crc.write()
Exemple #3
0
    def __init__(self):
        self._format = Struct("<BBHL4s")

        self.size = None

        self.data_type = ".FIT"
        self.data_size = None

        self.crc = Crc()

        self.protocol_version = 16
        self.profile_version = 1005
Exemple #4
0
class Header(object):
    def __init__(self):
        self._format = Struct("<BBHL4s")

        self.size = None

        self.data_type = ".FIT"
        self.data_size = None

        self.crc = Crc()

        self.protocol_version = 16
        self.profile_version = 1005

    def __nonzero__(self):
        return self.size is not None

    def __repr__(self):
        return '<%s protocol=%d profile=%d crc=%r>' % (
            self.__class__.__name__, self.protocol_version,
            self.profile_version, self.crc)

    @property
    def total_size(self):
        return self.size + self.data_size + self.crc.size

    @property
    def valid(self):
        return self.data_type == ".FIT"

    def read(self, chunk):
        (self.size, self.protocol_version, self.profile_version,
         self.data_size, self.data_type) = self._format.unpack(chunk[:12])

        if len(chunk) == 14:
            self.crc.read(chunk[12:])

            if self.crc.value and not self.crc.check(chunk[:12]):
                raise BodyFormatError(
                    "Invalid CRC %x, should be %x" %
                    (compute_crc(chunk[:12]), self.crc.value))

    def write(self):
        self.size = 14
        chunk = self._format.pack(self.size, self.protocol_version,
                                  self.profile_version, self.data_size,
                                  self.data_type)
        self.crc.value = compute_crc(chunk)
        return chunk + self.crc.write()
Exemple #5
0
    def __init__(self, ffd):
        self._fd = ffd

        self._header = Header()
        self._body = Body()
        self._crc = Crc()

        try:
            self.file_size = fstat(self._fd.fileno()).st_size
        except UnsupportedOperation:
            self._fd.seek(0, SEEK_END)
            self.file_size = self._fd.tell()
            self._fd.seek(0)

        self.header_chunk = ""
Exemple #6
0
class Writer(object):
    def __init__(self, ffd, body=None):
        self._fd = ffd

        self.header = Header()
        self.body = body or Body()
        self.crc = Crc()

    def __repr__(self):
        if not self.header or self.crc:
            self._prepare()

        return '<%s header=%r body=%r crc=%r>' % (
            self.__class__.__name__,
            self.header, self.body, self.crc
        )

    def _prepare(self):
        chunk = self.body.write()
        self.header.data_size = len(chunk)
        self.crc.value = compute_crc(chunk)
        return chunk

    def write(self):
        chunk = self._prepare()

        self._fd.seek(0)
        self._fd.truncate()

        self._fd.write(self.header.write())
        self._fd.write(chunk)
        self._fd.write(self.crc.write())
Exemple #7
0
class Writer(object):
    def __init__(self, ffd, body=None):
        self._fd = ffd

        self.header = Header()
        self.body = body or Body()
        self.crc = Crc()

    def __repr__(self):
        if not self.header or self.crc:
            self._prepare()

        return '<%s header=%r body=%r crc=%r>' % (
            self.__class__.__name__, self.header, self.body, self.crc)

    def _prepare(self):
        chunk = self.body.write()
        self.header.data_size = len(chunk)
        self.crc.value = compute_crc(chunk)
        return chunk

    def write(self):
        chunk = self._prepare()

        self._fd.seek(0)
        self._fd.truncate()

        self._fd.write(self.header.write())
        self._fd.write(chunk)
        self._fd.write(self.crc.write())
Exemple #8
0
    def __init__(self):
        self._format = Struct("<BBHL4s")

        self.size = None

        self.data_type = ".FIT"
        self.data_size = None

        self.crc = Crc()

        self.protocol_version = 16
        self.profile_version = 1005
Exemple #9
0
    def __init__(self, ffd):
        self._fd = ffd

        self._header = Header()
        self._body = Body()
        self._crc = Crc()

        try:
            self.file_size = fstat(self._fd.fileno()).st_size
        except UnsupportedOperation:
            self._fd.seek(0, SEEK_END)
            self.file_size = self._fd.tell()
            self._fd.seek(0)

        self.header_chunk = ""
Exemple #10
0
class Reader(object):
    def __init__(self, ffd):
        self._fd = ffd

        self._header = Header()
        self._body = Body()
        self._crc = Crc()

        try:
            self.file_size = fstat(self._fd.fileno()).st_size
        except UnsupportedOperation:
            self._fd.seek(0, SEEK_END)
            self.file_size = self._fd.tell()
            self._fd.seek(0)

        self.header_chunk = ""

    def __repr__(self):
        return '<%s header=%r body=%r crc=%r>' % (
            self.__class__.__name__,
            self.header, self.body, self.crc
        )

    @property
    def header(self):
        if self._header:
            return self._header

        self._fd.seek(0)

        header_size = ord(self._fd.read(1))
        if header_size not in (12, 14):
            raise HeaderFormatError(
                "Strange size: %d bytes" % header_size)

        header = self._fd.read(header_size - 1)
        if len(header) != header_size - 1:
            raise HeaderFormatError(
                "Can't read %d bytes, read %d bytes instead" % (
                    header_size, len(header) + 1))

        self.header_chunk = header = "%c%s" % (header_size, header)
        self._header.read(header)

        if not self._header.valid:
            raise HeaderFormatError("Not FIT filetype")

        if self._header.total_size != self.file_size:
            raise HeaderFormatError(
                "File size should be %d bytes, but actually is %d bytes" % (
                    self._header.total_size, self.file_size))

        return self._header

    @property
    def body(self):
        if not self._body:
            self._fd.seek(self.header.size)

            body = self._fd.read(self.header.data_size)
            if len(body) != self.header.data_size:
                raise BodyFormatError(
                    "Can't read %d bytes, read %d bytes instead" % (
                        self.header.data_size, len(body)))

            header = "" if self.header.crc.value else self.header_chunk
            if not self.crc.check(header + body):
                raise BodyFormatError("Invalid CRC %x, should be %x" % (
                    compute_crc(body), self.crc.value))

            self._body.read(body)
        return self._body

    @property
    def crc(self):
        if not self._crc:
            self._fd.seek(self.header.size + self.header.data_size)

            chunk = self._fd.read(self._crc.size)
            if len(chunk) != self._crc.size:
                raise CrcFormatError(
                    "Can't read %d bytes, read %d bytes instead" % (
                        self.header.data_size, len(chunk)))

            self._crc.read(chunk)
        return self._crc
Exemple #11
0
    def __init__(self, ffd, body=None):
        self._fd = ffd

        self.header = Header()
        self.body = body or Body()
        self.crc = Crc()
Exemple #12
0
class Reader(object):
    def __init__(self, ffd):
        self._fd = ffd

        self._header = Header()
        self._body = Body()
        self._crc = Crc()

        self.file_size = fstat(self._fd.fileno()).st_size
        self.header_chunk = ""

    def __repr__(self):
        return '<%s header=%r body=%r crc=%r>' % (
            self.__class__.__name__, self.header, self.body, self.crc)

    @property
    def header(self):
        if self._header:
            return self._header

        self._fd.seek(0)

        header_size = ord(self._fd.read(1))
        if header_size not in (12, 14):
            raise HeaderFormatError("Strange size: %d bytes" % header_size)

        header = self._fd.read(header_size - 1)
        if len(header) != header_size - 1:
            raise HeaderFormatError(
                "Can't read %d bytes, read %d bytes instead" %
                (header_size, len(header) + 1))

        self.header_chunk = header = "%c%s" % (header_size, header)
        self._header.read(header)

        if not self._header.valid:
            raise HeaderFormatError("Not FIT filetype")

        if self._header.total_size != self.file_size:
            raise HeaderFormatError(
                "File size should be %d bytes, but actually is %d bytes" %
                (self._header.total_size, self.file_size))

        return self._header

    @property
    def body(self):
        if not self._body:
            self._fd.seek(self.header.size)

            body = self._fd.read(self.header.data_size)
            if len(body) != self.header.data_size:
                raise BodyFormatError(
                    "Can't read %d bytes, read %d bytes instead" %
                    (self.header.data_size, len(body)))

            header = "" if self.header.crc.value else self.header_chunk
            if not self.crc.check(header + body):
                raise BodyFormatError("Invalid CRC %x, should be %x" %
                                      (compute_crc(body), self.crc.value))

            self._body.read(body)
        return self._body

    @property
    def crc(self):
        if not self._crc:
            self._fd.seek(self.header.size + self.header.data_size)

            chunk = self._fd.read(self._crc.size)
            if len(chunk) != self._crc.size:
                raise CrcFormatError(
                    "Can't read %d bytes, read %d bytes instead" %
                    (self.header.data_size, len(chunk)))

            self._crc.read(chunk)
        return self._crc
Exemple #13
0
    def __init__(self, ffd, body=None):
        self._fd = ffd

        self.header = Header()
        self.body = body or Body()
        self.crc = Crc()