# -*- 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.#fromtypingimportOptional,Union"""Base writer object from which all the writer of Muscat"""fromMuscat.Helpers.TextFormatHelperimportTFormatasTFormatfromMuscat.Helpers.LoggerimportInfo
[docs]defOpenInParaView(self):ifself._isOpen:raiseRuntimeError("The Writer must be close")fromMuscat.Actions.OpenInParaViewimportOpenInParaViewfromMuscat.Helpers.IO.PathControllerimportPathControllerOpenInParaView(filename=PathController.GetFullFilenameCurrentDirectory(self.fileName))
[docs]defSetAppendMode(self,mode=True):ifself.isOpen():print(TFormat.InRed("SetAppendMode before opening"))raiseException("SetAppendMode before opening")ifself.canHandleAppendisFalse:print(TFormat.InRed("This type of writer"+str(type(self))+" cant handle appendMode"))raise(Exception("This type of writer"+str(type(self))+" cant handle appendMode"))self.appendMode=mode
[docs]defSetTemporal(self,val=True):ifself.isOpen():print(TFormat.InRed("SetTemporal before opening"))raiseException("SetTemporal before opening")ifself.canHandleTemporalisFalse:print(TFormat.InRed("This type of writer"+str(type(self))+" cant handle Temporal Data"))raise(Exception("This type of writer"+str(type(self))+" cant handle Temporal Data"))self.__isTemporalOutput=bool(val)
[docs]defSetMultidomain(self,val=True):ifself.isOpen():print(TFormat.InRed("SetMultidomain before opening"))raiseException("SetMultidomain before opening")ifself.canHandleMultidomainisFalse:print(TFormat.InRed("This type of writer"+str(type(self))+" cant handle Mutli Domain Data"))raise(Exception("This type of writer"+str(type(self))+" cant handle Mutli Domain Data"))self.__isMultidomainOutput=bool(val)
[docs]defSetBinary(self,val=True):"""Sets the binary status of the file to read Parameters ---------- val : bool, optional if True, sets the file to read as binary, by default True """ifself._isOpen:print(TFormat.InRed("Please SetBinary before opening"))raiseException("Please SetBinary before opening")ifself.canHandleBinaryChange:self._isBinary=valelse:print('cant change the binary mode.')
[docs]defSetFileName(self,fileName:Union[str,None]=None):"""Sets the name of file to read Parameters ---------- fileName : str file name to set """self.fileName=str(fileName)
[docs]defOpen(self,filename=None):ifself._isOpen:# pragma: no coverprint(TFormat.InRed("The file is already open !!!!!"))raiseExceptioniffilenameisnotNone:self.SetFileName(filename)## we use unbuffered so we can repaire broken files easilytry:ifself._isBinary:mode="wb"else:ifself.InAppendMode():mode="a"else:# in python 3 the binary mode must be used to use the numpy.savetxtmode="w"# unbuffered text I/O are not allowed in python 3# bug http://bugs.python.org/issue17404#import io#import sys#binstdout = io.open(self.fileName, 'wb', 0)#self.filePointer = io.TextIOWrapper(binstdout, encoding=sys.stdout.encoding)#self.filePointer=open(self.fileName,mode)# to make the append workself.filePointer.seek(0,2)except:# pragma: no coverprint(TFormat.InRed("Error File Not Open"))# pragma: no coverraiseself._isOpen=True