"""Parsers provided by aiida_gromacs.This parser adds the ability to parse the outputs of the 'gmx genion' executable."""importosfrompathlibimportPathfromaiida.commonimportexceptionsfromaiida.engineimportExitCodefromaiida.ormimportSinglefileDatafromaiida.parsers.parserimportParserfromaiida.pluginsimportCalculationFactoryGenionCalculation=CalculationFactory("gromacs.genion")
[docs]classGenionParser(Parser):""" Parser class for parsing output of calculation. """
[docs]def__init__(self,node):""" Initialize Parser instance Checks that the ProcessNode being passed was produced by a GenionCalculation. :param node: ProcessNode of calculation :param type node: :class:`aiida.orm.nodes.process.process.ProcessNode` """super().__init__(node)ifnotissubclass(node.process_class,GenionCalculation):raiseexceptions.ParsingError("Can only parse GenionCalculation")
[docs]defparse(self,**kwargs):""" Parse outputs, store results in database. :returns: an exit code, if parsing fails (or nothing if parsing succeeds) """# the directory for storing parsed output filesoutput_dir=Path(self.node.get_option("output_dir"))outputs=["stdout","grofile","topfile"]# Check that folder content is as expectedfiles_retrieved=self.retrieved.base.repository.list_object_names()files_expected=[self.node.get_option("output_filename"),self.node.inputs.parameters["o"],self.node.inputs.topfile.filename,]# Note: set(A) <= set(B) checks whether A is a subset of Bifnotset(files_expected)<=set(files_retrieved):self.logger.error(f"Found files '{files_retrieved}', expected to find '{files_expected}'")returnself.exit_codes.ERROR_MISSING_OUTPUT_FILES# add outputsforindex,thinginenumerate(files_expected):self.logger.info(f"Parsing '{thing}'")withself.retrieved.base.repository.open(thing,"rb")ashandle:output_node=SinglefileData(filename=thing,file=handle)self.out(outputs[index],output_node)# If not in testing mode, then copy back the files.if"PYTEST_CURRENT_TEST"notinos.environ:self.retrieved.copy_tree(output_dir)returnExitCode(0)