コード例 #1
0
ファイル: test_numba.py プロジェクト: rikba/libsbp
def test_parse():

    header_len = 6
    data = b'floofydata'

    m = MsgFileioWriteReq(sequence=123,
                          offset=42,
                          filename=b'floof.bin\0' + data,
                          data=b'')

    buf = np.fromstring(m.to_binary(), dtype=np.uint8)

    assert len(buf) > 0

    pkt_len, payload_len, msg_type, sender, crc, crc_fail = unpack_payload(
        buf, 0, len(buf))
    assert not crc_fail

    m = dispatch(msg_type)(msg_type)
    res, offset, length = m.unpack(buf, header_len, payload_len)

    assert res is not None

    assert res['sequence'] == 123
    assert res['offset'] == 42
    assert res['filename'] == 'floof.bin'

    assert bytearray(res['data']) == bytearray(data)
コード例 #2
0
ファイル: fileio.py プロジェクト: wangfandasha/piksi_tools
    def write(self, filename, data, offset=0, trunc=True, progress_cb=None):
        """
        Write to a file.

        Parameters
        ----------
        filename : str
            Name of the file to write to.
        data : str
            Data to write
        offset : int (optional)
            Offset into the file at which to start writing in bytes.
        trunc : bool (optional)
            Overwite the file, i.e. delete any existing file before writing. If
            this option is not specified and the existing file is longer than the
            current write then the contents of the file beyond the write will
            remain. If offset is non-zero then this flag is ignored.

        Returns
        -------
        out : str
            Contents of the file.
        """
        if trunc and offset == 0:
            self.remove(filename)

        # How do we calculate this from the MsgFileioWriteRequest class?
        chunksize = MAX_PAYLOAD_SIZE - len(filename) - 9
        current_index = 0

        with SelectiveRepeater(self.link, SBP_MSG_FILEIO_WRITE_RESP) as sr:
            while offset < len(data):
                seq = self.next_seq()
                end_index = offset + chunksize - 1
                if end_index > len(data):
                    end_index = len(data)
                # print "going from {0} to {1} in array for chunksize {2}".format(offset, end_index, chunksize)
                chunk = data[offset:offset + chunksize - 1]
                # print "len is {0}".format(len(chunk))
                msg = MsgFileioWriteReq(sequence=seq,
                                        filename=(filename + '\0' + chunk),
                                        offset=offset,
                                        data='')
                sr.send(msg)
                offset += len(chunk)
                if progress_cb is not None:
                    progress_cb(offset)
            sr.flush()
コード例 #3
0
    def write(self, filename, data, offset=0, trunc=True, progress_cb=None):
        """
        Write to a file.

        Parameters
        ----------
        filename : bytes
            Name of the file to write to.
        data : bytearray
            Data to write
        offset : int (optional)
            Offset into the file at which to start writing in bytes.
        trunc : bool (optional)
            Overwite the file, i.e. delete any existing file before writing. If
            this option is not specified and the existing file is longer than the
            current write then the contents of the file beyond the write will
            remain. If offset is non-zero then this flag is ignored.

        Returns
        -------
        out : str
            Contents of the file.
        """
        if trunc and offset == 0:
            self.remove(filename)

        # How do we calculate this from the MsgFileioWriteReq class?
        chunksize = MAX_PAYLOAD_SIZE - len(filename) - 9
        current_index = 0

        with SelectiveRepeater(self.link, SBP_MSG_FILEIO_WRITE_RESP) as sr:
            while offset < len(data):
                seq = self.next_seq()
                end_index = offset + chunksize - 1
                if end_index > len(data):
                    end_index = len(data)
                chunk = data[offset:offset + chunksize - 1]
                msg = MsgFileioWriteReq(
                    sequence=seq,
                    offset=offset,
                    filename=filename + b'\x00',
                    data=chunk)
                sr.send(msg)
                offset += len(chunk)
                if (progress_cb is not None and seq % PROGRESS_CB_REDUCTION_FACTOR == 0):
                    progress_cb(offset)
            sr.flush()
コード例 #4
0
    def write(self, filename, data, offset=0, trunc=True, progress_cb=None):
        """
        Write to a file.

        Parameters
        ----------
        filename : bytes
            Name of the file to write to.
        data : bytearray
            Data to write
        offset : int (optional)
            Offset into the file at which to start writing in bytes.
        trunc : bool (optional)
            Overwite the file, i.e. delete any existing file before writing. If
            this option is not specified and the existing file is longer than the
            current write then the contents of the file beyond the write will
            remain. If offset is non-zero then this flag is ignored.

        Returns
        -------
        out : str
            Contents of the file.
        """
        if trunc and offset == 0:
            self.remove(filename)

        filename_len = len(filename)
        data_len = len(data)

        sequence_len = 4
        offset_len = 4
        null_sep_len = 1

        write_req_overhead_len = sequence_len + offset_len + null_sep_len

        # How do we calculate this from the MsgFileioWriteReq class?
        chunksize = MAX_PAYLOAD_SIZE - filename_len - write_req_overhead_len

        chunk_buf = bytearray(filename_len + null_sep_len + chunksize)

        chunk_buf[0:filename_len] = filename
        chunk_buf[filename_len] = ord(b'\x00')

        chunk_offset = filename_len + null_sep_len

        with SelectiveRepeater(self.link, SBP_MSG_FILEIO_WRITE_RESP) as sr:
            while offset < data_len:

                seq = self.next_seq()

                end_offset = min(offset + chunksize, data_len)
                chunk_len = min(chunksize, data_len - offset)
                chunk_end = (chunk_offset + chunk_len)

                chunk = data[offset:end_offset]
                chunk_buf[chunk_offset:chunk_end] = chunk

                if chunk_len < len(chunk_buf):
                    write_buf = chunk_buf[:chunk_end]
                else:
                    write_buf = chunk_buf

                msg = MsgFileioWriteReq(
                    sequence=seq,
                    offset=offset,
                    filename=
                    write_buf,  # Note: We put "write_buf" into the name because
                    #   putting in the correct place (the data
                    #   field) results in a huge slowdown
                    #   (presumably because an issue in the
                    #   construct library).
                    data=b'')

                sr.send(msg)
                offset += chunk_len

                if (progress_cb is not None
                        and seq % sr.progress_cb_reduction_factor == 0):
                    progress_cb(offset, sr)

            if progress_cb is not None:
                progress_cb(offset, sr)
            sr.flush()