Ejemplo n.º 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 = ""
Ejemplo n.º 2
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 = ""
Ejemplo n.º 3
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())
Ejemplo n.º 4
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())
Ejemplo n.º 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 = ""
Ejemplo n.º 6
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
Ejemplo n.º 7
0
    def __init__(self, ffd, body=None):
        self._fd = ffd

        self.header = Header()
        self.body = body or Body()
        self.crc = Crc()
Ejemplo n.º 8
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
Ejemplo n.º 9
0
    def __init__(self, ffd, body=None):
        self._fd = ffd

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