Ejemplo n.º 1
0
    def convert(self, value, param, ctx):
        """Attempt to interpret the value as a file first and if that fails try to load as a node."""
        from aiida.orm import StructureData, QueryBuilder

        try:
            filepath = pathlib.Path(
                __file__
            ).parent.parent / 'common' / 'data' / DEFAULT_STRUCTURES_MAPPING[
                value]
        except KeyError:
            try:
                return types.DataParamType(
                    sub_classes=('aiida.data:structure', )).convert(
                        value, param, ctx)
            except click.BadParameter:
                filepath = value

        try:
            import ase.io
        except ImportError as exception:
            raise click.BadParameter(
                f'failed to load a structure with identifier `{value}`.\n'
                'Cannot parse from file because `ase` is not installed.'
            ) from exception

        try:
            filepath = click.Path(exists=True,
                                  dir_okay=False,
                                  resolve_path=True).convert(
                                      filepath, param, ctx)
        except click.BadParameter as exception:
            raise click.BadParameter(
                f'failed to load a structure with identifier `{value}` and it can also not be resolved as a file.'
            ) from exception

        try:
            structure = StructureData(ase=ase.io.read(filepath))
        except Exception as exception:  # pylint: disable=broad-except
            raise click.BadParameter(
                f'file `{value}` could not be parsed into a `StructureData`: {exception}'
            ) from exception

        duplicate = QueryBuilder().append(StructureData,
                                          filters={
                                              'extras._aiida_hash':
                                              structure._get_hash()
                                          }).first()  # pylint: disable=protected-access

        if duplicate:
            return duplicate[0]

        return structure
Ejemplo n.º 2
0
    def convert(self, value, param, ctx):
        is_path = False
        # Alternative one could check if int or uuid
        # aiida allows also for shorten uuids
        from aiida.orm import StructureData, QueryBuilder

        try:
            structure = types.DataParamType(
                sub_classes=('aiida.data:structure', )).convert(
                    value, param, ctx)
        except (NotExistent, click.exceptions.BadParameter) as er:
            echo.echo(f'Tried to load node, could not fine one for {value}. '
                      'I will further check if it is a filepath.')
            is_path = True

        if is_path:
            # If it is a path to a file try to convert the structure
            pathtype = click.Path(exists=True,
                                  dir_okay=False,
                                  resolve_path=True)
            filename = pathtype.convert(value, param, ctx)
            try:
                import ase.io
            except ImportError:
                echo.echo_critical(
                    'You have not installed the package ase. \nYou can install it with: pip install ase'
                )

            try:
                asecell = ase.io.read(filename)
                structure = StructureData(ase=asecell)
            except ValueError as err:
                echo.echo_critical(str(err))
            # do not store structure, since this option is for calculation and workflow
            # input, which will store the structure anyway.

        # do not store again if structure is already there.
        duplicate = QueryBuilder().append(StructureData,
                                          filters={
                                              'extras._aiida_hash':
                                              structure._get_hash()
                                          }).first()  # pylint: disable=protected-access

        if duplicate:
            return duplicate[0]
        return structure