def test01_WriteToFilename(self): # write contents of datafname to h5 testfile filenode.save_to_filenode(self.testh5fname, self.datafname, "/test1") # make sure writing to an existing node doesn't work ... self.assertRaises(IOError, filenode.save_to_filenode, self.testh5fname, self.datafname, "/test1") # ... except if overwrite is True filenode.save_to_filenode(self.testh5fname, self.datafname, "/test1", overwrite=True) # write again, this time specifying a name filenode.save_to_filenode(self.testh5fname, self.datafname, "/", name="test2") # read from test h5file filenode.read_from_filenode(self.testh5fname, self.testfname, "/test1") # and compare result to what it should be with open(self.testfname, "rb") as fd: self.assertEqual(fd.read(), self.data) # make sure extracting to an existing file doesn't work ... self.assertRaises(IOError, filenode.read_from_filenode, self.testh5fname, self.testfname, "/test1") # except overwrite is True. And try reading with a name filenode.read_from_filenode(self.testh5fname, self.testfname, "/", name="test2", overwrite=True) # and compare to what it should be with open(self.testfname, "rb") as fd: self.assertEqual(fd.read(), self.data) # cleanup os.remove(self.testfname) os.remove(self.testh5fname)
def filenodeToFile(leaf): """Write to disk a filenode as a regular text file.""" # Warning: this is SLOW for large files fd, temp_filename = tempfile.mkstemp('.txt', 'filenode_') os.close(fd) h5file = leaf._v_file where = leaf._v_parent._v_pathname name = leaf._v_name filenode.read_from_filenode(h5file, temp_filename, where, name, overwrite=True) return vtutils.forwardPath(temp_filename)
def test03_AutomaticNameGuessing(self): # write using the filename as node name filenode.save_to_filenode(self.testh5fname, self.datafname, "/") # and read again datafname = os.path.split(self.datafname)[1] filenode.read_from_filenode(self.testh5fname, self.testdir, "/", name=datafname) # test if the output file really has the expected name self.assertEqual(os.access(os.path.join(self.testdir, datafname), os.R_OK), True) # and compare result to what it should be with open(os.path.join(self.testdir, datafname), "rb") as fd: self.assertEqual(fd.read(), self.data)
def test04_AutomaticNameGuessingWithFilenameAttribute(self): # write using the filename as node name filenode.save_to_filenode(self.testh5fname, self.datafname, "/") # and read again datafname = os.path.split(self.datafname)[1] filenode.read_from_filenode(self.testh5fname, self.testdir, "/", name=datafname) # test if the output file really has the expected name self.assertEqual(os.access(os.path.join(self.testdir, datafname), os.R_OK), True) # and compare result to what it should be with open(os.path.join(self.testdir, datafname), "rb") as fd: self.assertEqual(fd.read(), self.data)
def test02_WriteToHDF5File(self): # write contents of datafname to h5 testfile filenode.save_to_filenode(self.h5file, self.datafname, "/test1") # make sure writing to an existing node doesn't work ... self.assertRaises(IOError, filenode.save_to_filenode, self.h5file, self.datafname, "/test1") # ... except if overwrite is True filenode.save_to_filenode(self.h5file, self.datafname, "/test1", overwrite=True) # read from test h5file filenode.read_from_filenode(self.h5file, self.testfname, "/test1") # and compare result to what it should be with open(self.testfname, "rb") as fd: self.assertEqual(fd.read(), self.data) # make sure extracting to an existing file doesn't work ... self.assertRaises(IOError, filenode.read_from_filenode, self.h5file, self.testfname, "/test1") # make sure the original h5file is still alive and kicking self.assertEqual(isinstance(self.h5file, tables.file.File), True) self.assertEqual(self.h5file.mode, "w")