コード例 #1
0
ファイル: file_element.py プロジェクト: spongezhang/SMQTK
    def set_bytes(self, b):
        """
        Set bytes to this data element in the form of a string.

        Not all implementations may support setting bytes (writing). See the
        ``writable`` method.

        :param b: bytes to set.
        :type b: str

        :raises ReadOnlyError: This data element can only be read from / does
            not support writing.

        """
        if not self._readonly:
            safe_file_write(self._filepath, b)
        else:
            raise ReadOnlyError("This file element is read only.")
コード例 #2
0
ファイル: file_element.py プロジェクト: Kitware/SMQTK
    def set_bytes(self, b):
        """
        Set bytes to this data element in the form of a string.

        Not all implementations may support setting bytes (writing). See the
        ``writable`` method.

        :param b: bytes to set.
        :type b: str

        :raises ReadOnlyError: This data element can only be read from / does
            not support writing.

        """
        if not self._readonly:
            safe_file_write(self._filepath, b)
        else:
            raise ReadOnlyError("This file element is read only.")
コード例 #3
0
    def test_safe_file_write_relative_simple(
            self, m_NTF, m_remove, m_rename, m_scd):
        # Experimental filepath and content.
        fp = 'bar.txt'
        expected_bytes = 'hello world'

        # Mock return for temp file creation so we can check os.* calls.
        m_file = m_NTF.return_value
        test_tmp_fp = 'temp fp'
        m_file.name = test_tmp_fp

        safe_file_write(fp, expected_bytes)

        m_scd.assert_called_once_with('')
        m_NTF.assert_called_once_with(suffix='.txt', prefix='bar.', dir='', delete=False)
        m_file.write.assert_called_once_with(expected_bytes)
        m_file.__exit__.assert_called_once_with(None, None, None)
        self.assertEqual(m_remove.call_count, 0)
        m_rename.assert_called_once_with(test_tmp_fp, fp)
コード例 #4
0
    def test_safe_file_write_relative_simple(
            self, m_mkstemp, m_write, m_remove, m_close, m_rename, m_scd):
        # Experimental filepath and content.
        fp = 'bar.txt'
        expected_bytes = 'hello world'

        # Mock return for temp file creation so we can check os.* calls.
        test_tmp_fd = 'temp fd'
        test_tmp_fp = 'temp fp'
        m_mkstemp.return_value = (test_tmp_fd, test_tmp_fp)
        # Mock return from write simulating complete byte writing.
        m_write.return_value = len(expected_bytes)

        safe_file_write(fp, expected_bytes)

        m_scd.assert_called_once_with('')
        m_mkstemp.assert_called_once_with(suffix='.txt', prefix='bar.', dir='')
        m_write.assert_called_once_with(test_tmp_fd, expected_bytes)
        nose.tools.assert_equal(m_remove.call_count, 0)
        m_close.assert_called_once_with(test_tmp_fd)
        m_rename.assert_called_once_with(test_tmp_fp, fp)
コード例 #5
0
    def test_safe_file_write_custom_tmp_dir(self, m_mkstemp, m_write, m_remove,
                                            m_close, m_rename, m_scd):
        # Experimental filepath and content.
        fp = 'foo/other/bar.txt'
        expected_bytes = 'hello world'
        custom_tmp_dir = '/some/other/directory'

        # Mock return for temp file creation so we can check os.* calls.
        test_tmp_fd = 'temp fd'
        test_tmp_fp = 'temp fp'
        m_mkstemp.return_value = (test_tmp_fd, test_tmp_fp)
        # Mock return from write simulating complete byte writing.
        m_write.return_value = len(expected_bytes)

        safe_file_write(fp, expected_bytes, custom_tmp_dir)

        m_scd.assert_called_once_with('foo/other')
        m_mkstemp.assert_called_once_with(suffix='.txt',
                                          prefix='bar.',
                                          dir=custom_tmp_dir)
        m_write.assert_called_once_with(test_tmp_fd, expected_bytes)
        self.assertEqual(m_remove.call_count, 0)
        m_close.assert_called_once_with(test_tmp_fd)
        m_rename.assert_called_once_with(test_tmp_fp, fp)