# -*- coding: utf-8 -*-## This file is subject to the terms and conditions defined in# file 'LICENSE.txt', which is part of this source code package.#fromtypingimportAnyfromMuscat.IO.ReaderBaseimportReaderBasefromMuscat.IO.IOFactoryimportRegisterWriterClass,RegisterReaderClass
[docs]classIOHelper:"""helper Class that represent the data from a file"""def__init__(self,data)->None:self.unnamed=data[0]self.named=data[1]def__str__(self)->str:res=" named : "+str(self.named)+"\n"res+=" unnamed : "+str(self.unnamed)+"\n"returnres
[docs]defSaveData(filename:str,*argv,**kwargs)->int:"""Save the variables into the disk and return 0 if all ok Save variables into the disk, you can use unnamed or named variables (keyword) Parameters ---------- filename : str File name to Returns ------- int 0 if everything ok """withopen(filename,"wb")asdill_file:importdilldill_file=dill.Pickler(dill_file)dill_file.dump([argv,kwargs])return0return1# pragma: no cover
[docs]defLoadData(filename:str)->Any:"""Load data from disk using Dill format Load data saved with the 'saveData' from file Parameters ---------- filename : str File name to use Returns ------- IOHelper | None return an instance of IOHelper if ok return None if not ok """withopen(filename,"rb")asdill_file:importdillunpickler_data=dill.Unpickler(dill_file,encoding="latin1")data=unpickler_data.load()returnIOHelper(data)returnNone# pragma: no cover
[docs]classDillReader(ReaderBase):"""Class handling the reading of data using Dill"""def__init__(self,fileName:str=""):super().__init__(fileName=fileName)self.canHandleTemporal=False
[docs]defRead(self):"""Reads data using Dill Returns ------- any read data """internalReader=LoadData(self.fileName)returninternalReader.named["mesh"]
[docs]classDillWriter(object):"""Class handling the writing of data using Dill"""def__init__(self):super().__init__()self.filename=""self.canHandleBinaryChange=False
[docs]defWrite(self,mesh,PointFields=None,CellFields=None,GridFields=None,PointFieldsNames=None,CellFieldsNames=None,GridFieldsNames=None):"""Writes data using Dill"""ifPointFieldsNamesisnotNone:nodeFields={k:vfork,vinzip(PointFieldsNames,PointFields)}else:nodeFields={}ifCellFieldsNamesisnotNone:elemFields={k:vfork,vinzip(CellFieldsNames,CellFields)}else:elemFields={}importcopycmesh=copy.copy(mesh)cmesh.nodeFields=nodeFieldscmesh.elemFields=elemFieldsSaveData(self.filename,mesh=cmesh)
[docs]defCheckIntegrity(GUI:bool=False):"""AutoTest routine"""fromMuscat.Helpers.IO.TemporaryDirectoryimportTemporaryDirectory# create a temp filetempdir=TemporaryDirectory.GetTempPath()try:# Save dataSaveData(tempdir+"testFile.data","two",3,(3,5),toto=10)# load datab=LoadData(tempdir+"testFile.data")# test correct dataifb.unnamed[0]!="two":raiseException()# pragma: no coverifb.unnamed[1]!=3:raiseException()# pragma: no coverifb.unnamed[2]!=(3,5):raiseException()# pragma: no coverifb.named["toto"]!=10:raiseException()# pragma: no coveroutput=b.__str__()print(b)except:# pragma: no coverraisefromMuscat.Containers.MeshCreationToolsimportCreateUniformMeshOfBarsbarmesh=CreateUniformMeshOfBars(0,8,10)importnumpyasnpPointFields=[np.arange(barmesh.GetNumberOfNodes())]PointFieldsNames=["PointData"]CellFields=[np.arange(barmesh.GetNumberOfElements())]CellFieldsNames=["PointData"]print(barmesh)pw=DillWriter()pw.SetBinary()# this has no effectpw.Open(tempdir+"testFile.Dill")pw.Write(barmesh)pw.Write(barmesh,PointFieldsNames=PointFieldsNames,PointFields=PointFields,CellFields=CellFields,CellFieldsNames=CellFieldsNames)pw.Close()# this has no effectpr=DillReader()pr.SetFileName(tempdir+"testFile.Dill")barmeshII=pr.Read()fromMuscat.Containers.MeshToolsimportIsCloseIsClose(barmesh,barmeshII)print(barmeshII)return"Ok"
if__name__=="__main__":print(CheckIntegrity())# pragma: no cover