示例#1
0
def is_valid_element(data_element: DataElement,
                     valid_content_types: Optional[Iterable] = None,
                     check_image: bool = False) -> bool:
    """
    Determines if a given data element is valid.

    :param data_element: Data element
    :type data_element: DataElement

    :param valid_content_types: List of valid content types, or None to skip
        content type checking.
    :type valid_content_types: iterable | None

    :param check_image: Whether or not to try loading the image with PIL. This
        often catches issues that content type can't, such as corrupt images.
    :type check_image: bool

    :return: Whether or not the data element is valid
    :rtype: bool

    """
    log = logging.getLogger(__name__)

    if (valid_content_types is not None
            and data_element.content_type() not in valid_content_types):
        log.debug(
            "Skipping file (invalid content) type for "
            "descriptor generator (data_element='%s', ct=%s)", data_element,
            data_element.content_type())
        return False

    if check_image and not is_loadable_image(data_element):
        return False

    return isinstance(data_element, DataElement)
    def raise_valid_element(self,
                            data_element: DataElement,
                            exception_type: type = ValueError,
                            message: Optional[str] = None) -> DataElement:
        """
        Check if the given data element matches a reported valid content type,
        raising the given exception class (``ValueError`` by default) if not.

        :param smqtk.representation.DataElement data_element:
             Data element instance to check.
        :param StandardError exception_type:
            Custom exception type to raise if the given element does not report
            as a valid content type. By default we raise a ``ValueError``.
        :param str message:
            Specific message to provide with a raise exception. By default
            we compose a generic message that also reports the given
            element's content type.

        :return: The unmodified input data element.
        :rtype: smqtk.representation.DataElement
        """
        if not self.is_valid_element(data_element):
            if message is None:
                message = "Data element does not match a content type " \
                          "reported as valid. Given: \"{}\". Valid types: {}." \
                          .format(data_element.content_type(),
                                  list(self.valid_content_types()))
            # noinspection PyCallingNonCallable
            # - Leave the handling of whether or not an exception is
            # constructable to the exception class being constructed (user
            # decision repercussion).
            raise exception_type(message)
        return data_element
    def is_valid_element(self, data_element: DataElement) -> bool:
        """
        Check if the given DataElement instance reports a content type that
        matches one of the MIME types reported by ``valid_content_types``.

        :param data_element:
             Data element instance to check.

        :return: True if the given element has a valid content type as reported
            by ``valid_content_types``, and False if not.
        """
        return data_element.content_type() in self.valid_content_types()