def resolve_data_element(self, uri): """ Given the URI to some data, resolve it down to a DataElement instance. TODO: More simply use ``smqtk.representation.data_element.from_uri`` function, which should cover these cases as well as others. :raises ValueError: Issue with the given URI regarding either URI source resolution or data resolution. :param uri: URI to data :type uri: str :return: DataElement instance wrapping given URI to data. :rtype: smqtk.representation.DataElement """ self._log.debug("Resolving URI: %s", uri) # Resolve URI into appropriate DataElement instance de: DataElement # declare as to contain the base-class. if uri[:7] == "file://": self._log.debug("Given local disk filepath") filepath = uri[7:] if not os.path.isfile(filepath): raise ValueError("File URI did not point to an existing file " "on disk.") else: de = DataFileElement(filepath) elif uri[:9] == "base64://": self._log.debug("Given base64 string") content_type = flask.request.args.get('content_type', None) self._log.debug("Content type: %s", content_type) if not content_type: raise ValueError("No content-type with given base64 data") else: b64str = uri[9:] de = DataMemoryElement.from_base64(b64str, content_type) else: self._log.debug("Given URL") try: de = DataUrlElement(uri) except requests.HTTPError as ex: raise ValueError("Failed to initialize URL element due to " "HTTPError: %s" % str(ex)) return de
def resolve_data_element(self, uri): """ Given the URI to some data, resolve it down to a DataElement instance. :raises ValueError: Issue with the given URI regarding either URI source resolution or data resolution. :param uri: URI to data :type uri: str :return: DataElement instance wrapping given URI to data. :rtype: smqtk.representation.DataElement """ self._log.debug("Resolving URI: %s", uri) # Resolve URI into appropriate DataElement instance if uri[:7] == "file://": self._log.debug("Given local disk filepath") filepath = uri[7:] if not os.path.isfile(filepath): raise ValueError("File URI did not point to an existing file " "on disk.") else: de = DataFileElement(filepath) elif uri[:9] == "base64://": self._log.debug("Given base64 string") content_type = flask.request.args.get('content_type', None) self._log.debug("Content type: %s", content_type) if not content_type: raise ValueError("No content-type with given base64 data") else: b64str = uri[9:] de = DataMemoryElement.from_base64(b64str, content_type) else: self._log.debug("Given URL") try: de = DataUrlElement(uri) except requests.HTTPError as ex: raise ValueError("Failed to initialize URL element due to " "HTTPError: %s" % str(ex)) return de
def test_from_base64_empty_string(self): # Should translate to empty byte string e = DataMemoryElement.from_base64(six.b(''), None) ntools.assert_is_instance(e, DataMemoryElement) ntools.assert_equal(e.get_bytes(), six.b(''))
def test_from_base64_with_ct(self): e = DataMemoryElement.from_base64(self.VALID_BASE64, self.EXPECTED_CT) ntools.assert_is_instance(e, DataMemoryElement) ntools.assert_equal(e.get_bytes(), self.EXPECTED_BYTES) ntools.assert_equal(e.content_type(), self.EXPECTED_CT)
def test_from_base64_empty_string(self): # Should translate to empty byte string e = DataMemoryElement.from_base64('', None) self.assertIsInstance(e, DataMemoryElement) self.assertEqual(e.get_bytes(), six.b(''))
def test_from_base64_no_ct(self): e = DataMemoryElement.from_base64(self.VALID_BASE64) self.assertIsInstance(e, DataMemoryElement) self.assertEqual(e.get_bytes(), self.EXPECTED_BYTES)
def test_from_base64_empty_string(self): # Should translate to empty byte string e = DataMemoryElement.from_base64('', None) self.assertIsInstance(e, DataMemoryElement) self.assertEqual(e.get_bytes(), six.b(''))
def test_from_base64_with_ct(self): e = DataMemoryElement.from_base64(self.VALID_BASE64, self.EXPECTED_CT) self.assertIsInstance(e, DataMemoryElement) self.assertEqual(e.get_bytes(), self.EXPECTED_BYTES) self.assertEqual(e.content_type(), self.EXPECTED_CT)
def test_from_base64_no_ct(self): e = DataMemoryElement.from_base64(self.VALID_BASE64) self.assertIsInstance(e, DataMemoryElement) self.assertEqual(e.get_bytes(), self.EXPECTED_BYTES)