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.data_rep.DataElement """ # 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, ex: raise ValueError("Failed to initialize URL element due to " "HTTPError: %s" % str(ex))
def compute_descriptor(descriptor_label, uri): """ # Data modes for upload/use - local filepath - base64 - http/s URL The following sub-sections detail how different URI's can be used. ## Local Filepath The URI string must be prefixed with ``file://``, followed by the full path to the data file to describe. ## Base 64 data The URI string must be prefixed with "base64://", followed by the base64 encoded string. This mode also requires an additional ``?content_type=`` to provide data content type information. This mode saves the encoded data to temporary file for processing. ## HTTP/S address This is the default mode when the URI prefix is none of the above. This uses the requests module to locally download a data file for processing. """ success = True message = "Nothing has happened yet" # Data element to compute a descriptor for de = None # Resolve URI into DataElement instance if uri[:7] == "file://": self.log.debug("Given local disk filepath") filepath = uri[7:] if not os.path.isfile(filepath): success = False message = "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: self.log.warning("No content-type with given base64 data") success = False message = "No content-type with given base64 content." else: b64str = uri[9:] de = DataMemoryElement.from_base64(b64str, content_type) else: self.log.debug("Given URL") de = DataUrlElement(uri) descriptor = None if success: # so far... # Get the descriptor instance for the given label, creating it # if necessary. if descriptor_label not in descriptor_cache: self.log.debug("Creating descriptor '%s'", descriptor_label) with descriptor_cache_lock: descriptor_cache[descriptor_label] = \ ContentDescriptorConfiguration\ .new_inst(descriptor_label) with descriptor_cache_lock: #: :type: smqtk.content_description.ContentDescriptor d = descriptor_cache[descriptor_label] with SimpleTimer("Computing descriptor...", self.log.debug): try: descriptor = d.compute_descriptor(de).tolist() message = "Descriptor computed of type '%s'" \ % descriptor_label except ValueError, ex: success = False message = "Descriptor '%s' had an issue with the input " \ "data: %s" \ % (descriptor_label, str(ex))