Ejemplo n.º 1
0
 def test_get_bytes_no_file(self):
     e = DataFileElement("/not/a/valid/path.txt", readonly=True)
     # We currently expect, in the case where the filepath doesn't exist, to
     # get the same bytes as if the file existed and were empty.
     self.assertEqual(e.get_bytes(), six.b(""))
     # read-only status should have no effect.
     e = DataFileElement("/not/a/valid/path.txt", readonly=True)
     self.assertEqual(e.get_bytes(), six.b(""))
Ejemplo n.º 2
0
 def test_get_bytes_no_file(self):
     e = DataFileElement("/not/a/valid/path.txt", readonly=True)
     # We currently expect, in the case where the filepath doesn't exist, to
     # get the same bytes as if the file existed and were empty.
     self.assertEqual(e.get_bytes(), six.b(""))
     # read-only status should have no effect.
     e = DataFileElement("/not/a/valid/path.txt", readonly=True)
     self.assertEqual(e.get_bytes(), six.b(""))
Ejemplo n.º 3
0
    def test_load_dataset_tempfile(self):
        """
        Test DataElement temporary file based context loader.
        """
        # Creating separate element from global so we can mock it up.
        e = DataFileElement(GH_IMAGE_FP, readonly=True)
        e.write_temp = mock.MagicMock(wraps=e.write_temp)
        e.clean_temp = mock.MagicMock(wraps=e.clean_temp)
        e.get_bytes = mock.MagicMock(wraps=e.get_bytes)

        # Using explicit patcher start/stop in order to avoid using ``patch``
        # as a decorator because ``osgeo`` might not be defined when
        # decorating the method.
        patcher_gdal_open = mock.patch('smqtk.algorithms.image_io.gdal_io.gdal'
                                       '.Open', wraps=osgeo.gdal.Open)
        self.addCleanup(patcher_gdal_open.stop)

        m_gdal_open = patcher_gdal_open.start()

        with load_dataset_tempfile(e) as gdal_ds:
            # noinspection PyUnresolvedReferences
            e.write_temp.assert_called_once_with()
            # noinspection PyUnresolvedReferences
            e.get_bytes.assert_not_called()

            m_gdal_open.assert_called_once()

            assert gdal_ds.RasterCount == 3
            assert gdal_ds.RasterXSize == 512
            assert gdal_ds.RasterYSize == 600

        # noinspection PyUnresolvedReferences
        e.clean_temp.assert_called_once_with()
        assert len(e._temp_filepath_stack) == 0
Ejemplo n.º 4
0
    def test_load_dataset_vsimem(self):
        """
        Test that VSIMEM loading context
        """
        if LooseVersion(osgeo.__version__).version[0] < 2:
            pytest.skip("Skipping VSIMEM test because GDAL version < 2")

        # Creating separate element from global so we can mock it up.
        e = DataFileElement(GH_IMAGE_FP, readonly=True)
        e.write_temp = mock.MagicMock(wraps=e.write_temp)
        e.clean_temp = mock.MagicMock(wraps=e.clean_temp)
        e.get_bytes = mock.MagicMock(wraps=e.get_bytes)

        vsimem_path_re = re.compile(r'^/vsimem/\w+$')

        # Using explicit patcher start/stop in order to avoid using ``patch``
        # as a *decorator* because ``osgeo`` might not be defined when
        # decorating the method.
        patcher_gdal_open = mock.patch(
            'smqtk.algorithms.image_io.gdal_io.gdal.Open',
            wraps=osgeo.gdal.Open,
        )
        self.addCleanup(patcher_gdal_open.stop)
        patcher_gdal_unlink = mock.patch(
            'smqtk.algorithms.image_io.gdal_io.gdal.Unlink',
            wraps=osgeo.gdal.Unlink,
        )
        self.addCleanup(patcher_gdal_unlink.stop)

        m_gdal_open = patcher_gdal_open.start()
        m_gdal_unlink = patcher_gdal_unlink.start()

        with load_dataset_vsimem(e) as gdal_ds:
            # noinspection PyUnresolvedReferences
            e.write_temp.assert_not_called()
            # noinspection PyUnresolvedReferences
            e.get_bytes.assert_called_once_with()

            m_gdal_open.assert_called_once()
            ds_path = gdal_ds.GetDescription()
            assert vsimem_path_re.match(ds_path)

            assert gdal_ds.RasterCount == 3
            assert gdal_ds.RasterXSize == 512
            assert gdal_ds.RasterYSize == 600

        m_gdal_unlink.assert_called_once_with(ds_path)
        # noinspection PyUnresolvedReferences
        e.clean_temp.assert_not_called()
        assert len(e._temp_filepath_stack) == 0
Ejemplo n.º 5
0
        def iqr_ingest_file():
            """
            Ingest the file with the given UID, getting the path from the
            uploader.

            :return: string of data/descriptor element's UUID
            :rtype: str

            """
            # TODO: Add status dict with a "GET" method branch for getting that
            #       status information.

            fid = flask.request.form['fid']

            sid = self.get_current_iqr_session()

            self._log.debug(
                "[%s::%s] Getting temporary filepath from "
                "uploader module", sid, fid)
            upload_filepath = self.mod_upload.get_path_for_id(fid)
            self.mod_upload.clear_completed(fid)

            self._log.debug("[%s::%s] Moving uploaded file", sid, fid)
            sess_upload = osp.join(self._iqr_work_dirs[sid],
                                   osp.basename(upload_filepath))
            os.rename(upload_filepath, sess_upload)

            # Record uploaded data as user example data for this session.
            upload_data = DataFileElement(sess_upload)
            uuid = upload_data.uuid()
            self._iqr_example_data[sid][uuid] = upload_data

            # Extend session ingest -- modifying
            self._log.debug(
                "[%s::%s] Adding new data to session "
                "external positives", sid, fid)
            data_b64 = base64.b64encode(upload_data.get_bytes())
            data_ct = upload_data.content_type()
            r = self._iqr_service.post('add_external_pos',
                                       sid=sid,
                                       base64=data_b64,
                                       content_type=data_ct)
            r.raise_for_status()

            return str(uuid)
Ejemplo n.º 6
0
        def iqr_ingest_file():
            """
            Ingest the file with the given UID, getting the path from the
            uploader.

            :return: string of data/descriptor element's UUID
            :rtype: str

            """
            # TODO: Add status dict with a "GET" method branch for getting that
            #       status information.

            fid = flask.request.form['fid']

            sid = self.get_current_iqr_session()

            self._log.debug("[%s::%s] Getting temporary filepath from "
                            "uploader module", sid, fid)
            upload_filepath = self.mod_upload.get_path_for_id(fid)
            self.mod_upload.clear_completed(fid)

            self._log.debug("[%s::%s] Moving uploaded file",
                            sid, fid)
            sess_upload = osp.join(self._iqr_work_dirs[sid],
                                   osp.basename(upload_filepath))
            os.rename(upload_filepath, sess_upload)

            # Record uploaded data as user example data for this session.
            upload_data = DataFileElement(sess_upload)
            uuid = upload_data.uuid()
            self._iqr_example_data[sid][uuid] = upload_data

            # Extend session ingest -- modifying
            self._log.debug("[%s::%s] Adding new data to session "
                            "external positives", sid, fid)
            data_b64 = base64.b64encode(upload_data.get_bytes())
            data_ct = upload_data.content_type()
            r = self._iqr_service.post('add_external_pos', sid=sid,
                                       base64=data_b64, content_type=data_ct)
            r.raise_for_status()

            return str(uuid)
Ejemplo n.º 7
0
 def test_get_bytes(self):
     # Test with a known real file.
     test_file_path = os.path.join(TEST_DATA_DIR, 'text_file')
     e = DataFileElement(test_file_path)
     self.assertEqual(e.get_bytes(), six.b("Some text content.\n"))
Ejemplo n.º 8
0
 def test_get_bytes(self):
     # Test with a known real file.
     test_file_path = os.path.join(TEST_DATA_DIR, 'text_file')
     e = DataFileElement(test_file_path)
     self.assertEqual(e.get_bytes(), six.b("Some text content.\n"))