def _readMoreXML(self, xmlNode): """ Function to read the portion of the xml input that belongs to this specialized class and initialize variables based on the input received. @ In, xmlNode, xml.etree.ElementTree.Element, XML element node that represents the portion of the input that belongs to this class @ Out, None """ DateBase._readMoreXML(self, xmlNode) # Check if database directory exist, otherwise create it if '~' in self.databaseDir: self.databaseDir = copy.copy(os.path.expanduser(self.databaseDir)) # Determine RELATIVE location for HDF5. # - if a full path is given, accept it as given, else ... if not os.path.isabs(self.databaseDir): # use working dir as base self.databaseDir = os.path.join(self.workingDir, self.databaseDir) self.databaseDir = os.path.normpath(self.databaseDir) utils.makeDir(self.databaseDir) self.raiseADebug('Database Directory is:', self.databaseDir) # Check if a filename has been provided # if yes, we assume the user wants to load the data from there # or update it #try: self.filename = xmlNode.attrib.get('filename', self.name + '.h5') if 'readMode' not in xmlNode.attrib.keys(): self.raiseAnError( IOError, 'No "readMode" attribute was specified for hdf5 database', self.name) self.readMode = xmlNode.attrib['readMode'].strip().lower() readModes = ['read', 'overwrite'] if self.readMode not in readModes: self.raiseAnError(IOError, 'readMode attribute for hdf5 database', self.name, 'is not recognized:', self.readMode, '. Options are:', readModes) self.raiseADebug('HDF5 Read Mode is "' + self.readMode + '".') fullpath = os.path.join(self.databaseDir, self.filename) if os.path.isfile(fullpath): if self.readMode == 'read': self.exist = True elif self.readMode == 'overwrite': self.exist = False self.database = h5Data(self.name, self.databaseDir, self.messageHandler, self.filename, self.exist) else: #file does not exist in path if self.readMode == 'read': self.raiseAWarning( 'Requested to read from database, but it does not exist at:', fullpath, '; continuing without reading...') self.exist = False self.database = h5Data(self.name, self.databaseDir, self.messageHandler, self.filename, self.exist) self.raiseAMessage('Database is located at:', fullpath)
def _handleInput(self, paramInput): """ Function to handle the common parts of the database parameter input. @ In, paramInput, ParameterInput, the already parsed input. @ Out, None """ if 'directory' in paramInput.parameterValues: self.databaseDir = copy.copy( paramInput.parameterValues['directory']) # if not absolute path, join with working directory if not os.path.isabs(self.databaseDir): self.databaseDir = os.path.abspath( os.path.join(self.workingDir, self.databaseDir)) else: self.databaseDir = os.path.join(self.workingDir, 'DatabaseStorage') if 'filename' in paramInput.parameterValues: self.filename = copy.copy(paramInput.parameterValues['filename']) else: self.filename = self.name + '.h5' # read the variables varNode = paramInput.findFirst("variables") if varNode is not None: self.variables = varNode.value # read mode self.readMode = paramInput.parameterValues['readMode'].strip().lower() self.raiseADebug('HDF5 Read Mode is "' + self.readMode + '".') if self.readMode == 'overwrite': # check if self.databaseDir exists or create in case not if not os.path.isdir(self.databaseDir): os.makedirs(self.databaseDir, exist_ok=True) # get full path fullpath = os.path.join(self.databaseDir, self.filename) if os.path.isfile(fullpath): if self.readMode == 'read': self.exist = True elif self.readMode == 'overwrite': self.exist = False self.database = h5Data(self.name, self.databaseDir, self.messageHandler, self.filename, self.exist, self.variables) else: #file does not exist in path if self.readMode == 'read': self.raiseAnError( IOError, 'Requested to read from database, but it does not exist at:', fullpath, '; The path to the database must be either absolute or relative to <workingDir>!' ) self.exist = False self.database = h5Data(self.name, self.databaseDir, self.messageHandler, self.filename, self.exist, self.variables) self.raiseAMessage('Database is located at:', fullpath)
def initializeDatabase(self): """ Initialize underlying database object. @ In, None @ Out, None """ super(HDF5, self).initializeDatabase() self.database = h5Data(self.name,self.databaseDir,self.messageHandler,self.filename,self.exist,self.variables)
def __setstate__(self, newstate): """ Set the state (for pickling) we do not pickle the HDF5 (C++) instance but only the info to reload it @ In, newstate, dict, the namespace state @ Out, None """ self.__dict__.update(newstate) self.exist = True self.database = h5Data(self.name,self.databaseDir,self.messageHandler,self.filename,self.exist)