Beispiel #1
0
    def loadModel(self, aModel):
        # aModel : an EML instance, a file name (string) or a file object
        # return -> None
        # This method can thwor exceptions.

        # checks the type of aModel

        if isinstance(aModel, eml.Eml):
            # if the type is EML instance
            anEml = aModel
            aModelName = '<eml.Eml>'  # what should this be?
        elif isinstance(aModel, str) or isinstance(aModel, unicode):
            # if the type is string
            aFileObject = open(aModel)
            aModelName = aModel
            anEml = eml.Eml(aFileObject)
        elif isinstance(aModel, file):
            # change directory to file's home directory
            # if the type is file object
            aFileObject = aModel
            aModelName = aModel.name
            anEml = eml.Eml(aFileObject)
        else:
            # When the type doesn't match
            raise TypeError, "The type of aModel must be EML instance, string(file name) or file object "

        # calls load methods
        self.__loadStepper(anEml)
        self.__loadEntity(anEml)
        self.__loadAllProperty(anEml)
        self.theSimulator.initialize()

        # saves ModelName
        self.theModelName = aModelName
Beispiel #2
0
    def setModel(self, aModelStr, aModelName='a_model'):
        # aModelStr : an EM or EML string
        # return -> None
        # This method can thwor exceptions.

        # checks the type of aModel

        if isinstance(aModelStr, str):
            aModel_StringIO = StringIO.StringIO(aModelStr)
            if aModelStr.strip()[0:5] == "<?xml":
                anEml = eml.Eml(aModel_StringIO)
            else:
                aPreprocessor = Preprocessor(aModel_StringIO, aModelName)
                anEmFile = aPreprocessor.preprocess()
                anEmFile.seek(0)
                anEml = convertEm2Eml(anEmFile, False)
                aPreprocessor.shutdown()
            aModel_StringIO.close()
        elif isinstance(aModelStr, unicode):
            # When aModelStr is unicode
            raise TypeError, "setModel() can't process unicode string at present."
        else:
            # When the type doesn't match
            raise TypeError, "The type of a model string must be a string object."

        # calls load methods
        self.__loadStepper(anEml)
        self.__loadEntity(anEml)
        self.__loadAllProperty(anEml)
        self.theSimulator.initialize()

        # saves ModelName
        self.theModelName = aModelName
Beispiel #3
0
    def loadModel(self, aModel):
        # aModel : an EML instance, a file name (string) or a file object
        # return -> None
        # This method can thwor exceptions.

        # checks the type of aModel

        if isinstance(aModel, eml.Eml):
            # if the type is EML instance
            anEml = aModel
            aModelName = '<eml.Eml>'  # what should this be?
        elif isinstance(aModel, str) or isinstance(aModel, unicode):
            # if the type is string
            _, ext = os.path.splitext(aModel)
            ext = ext.lower()
            if ext == '.eml':
                aFileObject = open(aModel)
                anEml = eml.Eml(aFileObject)
            elif ext == '.em':
                aPreprocessor = Preprocessor(open(aModel), aModel)
                anEmFile = aPreprocessor.preprocess()
                anEmFile.seek(0)
                anEml = convertEm2Eml(anEmFile, False)
                aPreprocessor.shutdown()
            else:
                raise Exception("Unsupported file type: %s" % ext)
            aModelName = aModel
        elif isinstance(aModel, file):
            # change directory to file's home directory
            # if the type is file object
            aFileObject = aModel
            aModelName = aModel.name
            anEml = eml.Eml(aFileObject)
        else:
            # When the type doesn't match
            raise TypeError, "The type of aModel must be EML instance, string(file name) or file object "

        # calls load methods
        self.__loadStepper(anEml)
        self.__loadEntity(anEml)
        self.__loadAllProperty(anEml)
        self.theSimulator.initialize()

        # saves ModelName
        self.theModelName = aModelName
Beispiel #4
0
    def saveModel(self, aModel):
        # aModel : a file name (string) or a file object
        # return -> None
        # This method can thwor exceptions.

        # creates ana seve an EML instance
        anEml = eml.Eml()

        # calls save methods
        self.__saveAllStepper(anEml)
        self.__saveEntity(anEml, 'System::/')
        self.__saveAllEntity(anEml)
        self.__saveProperty(anEml)

        # if the type is string
        if type(aModel) == str:

            # add comment
            aCurrentInfo = '''<!-- created by ecell.Session.saveModel
 date: %s
 currenttime: %s
-->
<eml>
''' % (time.asctime(time.localtime()), self.getCurrentTime())
            aString = anEml.asString()
            aBuffer = aCurrentInfo.join(aString.split('<eml>\n'))
            aFileObject = open(aModel, 'w')
            aFileObject.write(aBuffer)
            aFileObject.close()

        # if the type is file object
        elif type(aModel) == file:
            aString = anEml.asString()
            aFileObject = aModel
            aFileObject.write(aString)
            aFileObject.close()

        # When the type doesn't match
        else:
            raise TypeError, "The type of aModel must be string(file name) or file object "