Source code for Muscat.IO.WriterBase

# -*- 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.
#
from typing import Optional, Union

"""Base writer object from which all the writer of Muscat
"""


from Muscat.Helpers.TextFormatHelper import TFormat as TFormat
from Muscat.Helpers.Logger import Info


[docs]class WriterBase(): """ WriterBase class""" def __init__(self, fileName:Union[str,None] = None): super().__init__() self.fileName = None self.SetFileName(fileName) self._isOpen = False self._isBinary = False self.canHandleTemporal = False self.canHandleMultidomain = False self.canHandleBinaryChange = True self.__isTemporalOutput = False self.__isMultidomainOutput = False self.canHandleAppend = False self._inAppendMode = False self.filePointer: TextIOWrapper = None
[docs] def OpenInParaView(self): if self._isOpen: raise RuntimeError("The Writer must be close") from Muscat.Actions.OpenInParaView import OpenInParaView from Muscat.Helpers.IO.PathController import PathController OpenInParaView(filename=PathController.GetFullFilenameCurrentDirectory(self.fileName))
[docs] def SetAppendMode(self,mode = True): if self.isOpen() : print(TFormat.InRed("SetAppendMode before opening")) raise Exception("SetAppendMode before opening") if self.canHandleAppend is False: 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] def InAppendMode(self): return self._inAppendMode
[docs] def SetTemporal(self, val = True): if self.isOpen() : print(TFormat.InRed("SetTemporal before opening")) raise Exception("SetTemporal before opening") if self.canHandleTemporal is False: 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] def IsTemporalOutput(self): return self.__isTemporalOutput
[docs] def SetMultidomain(self,val = True): if self.isOpen() : print(TFormat.InRed("SetMultidomain before opening")) raise Exception("SetMultidomain before opening") if self.canHandleMultidomain is False: 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] def IsMultidomainOutput(self): return self.__isMultidomainOutput
[docs] def SetBinary(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 """ if self._isOpen : print(TFormat.InRed("Please SetBinary before opening")) raise Exception("Please SetBinary before opening") if self.canHandleBinaryChange: self._isBinary = val else: print('cant change the binary mode.')
[docs] def isBinary(self): return self._isBinary
[docs] def isOpen(self): return self._isOpen
[docs] def SetFileName(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] def Open(self, filename = None): if self._isOpen :# pragma: no cover print(TFormat.InRed("The file is already open !!!!!")) raise Exception if filename is not None: self.SetFileName(filename) ## we use unbuffered so we can repaire broken files easily try : if self._isBinary : mode = "wb" else: if self.InAppendMode(): mode = "a" else: # in python 3 the binary mode must be used to use the numpy.savetxt mode = "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 work self.filePointer.seek(0,2) except:# pragma: no cover print(TFormat.InRed("Error File Not Open"))# pragma: no cover raise self._isOpen = True
[docs] def writeText(self,text): if self._isBinary: self.filePointer.write(text.encode('utf8')) else: self.filePointer.write(text)
[docs] def Close(self): if self._isOpen: self.filePointer.close() self._isOpen = False else : Info(TFormat.InRed("File Not Open")) raise
[docs]def CheckIntegrity(): obj = WriterBase() return "ok"
if __name__ == '__main__': print(CheckIntegrity())# pragma: no cover