示例#1
0
    def get_downloadable_data(node, download_format=None):
        """
        Generic function to download file in specified format.
        Actual definition is in child classes as the content to be
        returned and its format depends on the download plugin specific
        to the resource

        :param node: node object
        :param download_format: file extension format
        :returns: data in selected format to download

        If this method is called for a Data node resource it will
        invoke the get_downloadable_data method in the Data transaltor.
        Otherwise it raises RestFeatureNotAvailable exception
        """
        from aiida.orm import Data
        from aiida.restapi.translator.nodes.data import DataTranslator  # pylint: disable=cyclic-import
        from aiida.restapi.common.exceptions import RestFeatureNotAvailable

        # This needs to be here because currently, for all nodes the `NodeTranslator` will be instantiated. Once that
        # logic is cleaned where the correct translator sub class is instantiated based on the node type that is
        # referenced, this hack can be removed.
        if isinstance(node, Data):
            downloadable_data = DataTranslator.get_downloadable_data(
                node, download_format=download_format)
            return downloadable_data

        raise RestFeatureNotAvailable(
            'This endpoint is not available for Process nodes.')
示例#2
0
    def get_all_download_formats(full_type=None):
        """
        returns dict of possible node formats for all available node types
        """

        all_formats = {}

        if full_type:
            try:
                node_cls = load_entry_point_from_full_type(full_type)
            except (TypeError, ValueError):
                raise RestInputValidationError(f'The full type {full_type} is invalid.')
            except EntryPointError:
                raise RestFeatureNotAvailable('The download formats for this node type are not available.')

            try:
                available_formats = node_cls.get_export_formats()
                all_formats[full_type] = available_formats
            except AttributeError:
                pass
        else:
            entry_point_group = 'aiida.data'

            for name in get_entry_point_names(entry_point_group):
                try:
                    node_cls = load_entry_point(entry_point_group, name)
                    available_formats = node_cls.get_export_formats()
                except (AttributeError, LoadingEntryPointError):
                    continue

                if available_formats:
                    full_type = construct_full_type(node_cls.class_node_type, '')
                    all_formats[full_type] = available_formats

        return all_formats
示例#3
0
    def get_downloadable_data(node, format=None):
        """
        Generic function extented for kpoints data. Currently
        it is not implemented.

        :param node: node object that has to be visualized
        :param format: file extension format
        :returns: raise RestFeatureNotAvailable exception
        """

        from aiida.restapi.common.exceptions import RestFeatureNotAvailable

        raise RestFeatureNotAvailable(
            "This endpoint is not available for Bands.")
示例#4
0
    def get_all_download_formats(full_type=None):
        """
        returns dict of possible node formats for all available node types
        """
        from aiida.plugins.entry_point import load_entry_point, get_entry_point_names
        from aiida.restapi.common.identifiers import load_entry_point_from_full_type, construct_full_type
        from aiida.common import EntryPointError
        from aiida.common.exceptions import LoadingEntryPointError
        from aiida.restapi.common.exceptions import RestFeatureNotAvailable, RestInputValidationError

        all_formats = {}

        if full_type:
            try:
                node_cls = load_entry_point_from_full_type(full_type)
            except (TypeError, ValueError):
                raise RestInputValidationError(
                    'The full type {} is invalid.'.format(full_type))
            except EntryPointError:
                raise RestFeatureNotAvailable(
                    'The download formats for this node type are not available.'
                )

            try:
                available_formats = node_cls.get_export_formats()
                all_formats[full_type] = available_formats
            except AttributeError:
                pass
        else:
            entry_point_group = 'aiida.data'

            for name in get_entry_point_names(entry_point_group):
                try:
                    node_cls = load_entry_point(entry_point_group, name)
                except LoadingEntryPointError:
                    pass
                else:
                    node_cls.get_export_formats()
                try:
                    available_formats = node_cls.get_export_formats()
                    if available_formats:
                        full_type = construct_full_type(
                            node_cls.class_node_type, '')
                        all_formats[full_type] = available_formats
                except AttributeError:
                    pass
        return all_formats