distinguish 'file not found' and 'invalid file'

This commit is contained in:
Martin Diehl 2020-11-19 10:39:41 +01:00
parent c74e57f225
commit 903c185ee6
4 changed files with 16 additions and 9 deletions

View File

@ -139,6 +139,8 @@ class VTK:
vtkUnstructuredGrid, and vtkPolyData. vtkUnstructuredGrid, and vtkPolyData.
""" """
if not os.path.isfile(fname): # vtk has a strange error handling
raise FileNotFoundError(f'No such file: {fname}')
ext = Path(fname).suffix ext = Path(fname).suffix
if ext == '.vtk' or dataset_type is not None: if ext == '.vtk' or dataset_type is not None:
reader = vtk.vtkGenericDataObjectReader() reader = vtk.vtkGenericDataObjectReader()
@ -166,9 +168,6 @@ class VTK:
else: else:
raise TypeError(f'Unknown file extension {ext}') raise TypeError(f'Unknown file extension {ext}')
if not os.path.isfile(fname):
raise FileNotFoundError(f'No such file: {fname}')
reader.SetFileName(str(fname)) reader.SetFileName(str(fname))
reader.Update() reader.Update()
vtk_data = reader.GetOutput() vtk_data = reader.GetOutput()

View File

@ -299,6 +299,9 @@ def _polar_decomposition(T,requested):
if 'U' in requested: if 'U' in requested:
output.append(_np.einsum('...ji,...jk',R,T)) output.append(_np.einsum('...ji,...jk',R,T))
if len(output) == 0:
raise ValueError('Output needs to be out of V,R,U')
return tuple(output) return tuple(output)

View File

@ -55,7 +55,7 @@ class TestGeom:
def test_invalid_vtr(self,tmp_path): def test_invalid_vtr(self,tmp_path):
v = VTK.from_rectilinear_grid(np.random.randint(5,10,3)*2,np.random.random(3) + 1.0) v = VTK.from_rectilinear_grid(np.random.randint(5,10,3)*2,np.random.random(3) + 1.0)
v.save(tmp_path/'no_materialpoint.vtr') v.save(tmp_path/'no_materialpoint.vtr',parallel=False)
with pytest.raises(ValueError): with pytest.raises(ValueError):
Geom.load(tmp_path/'no_materialpoint.vtr') Geom.load(tmp_path/'no_materialpoint.vtr')

View File

@ -100,12 +100,17 @@ class TestVTK:
v = VTK.from_poly_data(points) v = VTK.from_poly_data(points)
v.save(tmp_path/fname) v.save(tmp_path/fname)
@pytest.mark.parametrize('name,dataset_type',[('this_file_does_not_exist.vtk', None), @pytest.mark.parametrize('fname,dataset_type',[('a_file.vtk', None),
('this_file_does_not_exist.vtk','vtk'), ('a_file.vtk','vtk'),
('this_file_does_not_exist.vtx', None)]) ('a_file.vtx', None)])
def test_invalid_dataset_type(self,name,dataset_type): def test_invalid_dataset_type(self,tmp_path,fname,dataset_type):
open(tmp_path/fname,'a').close()
with pytest.raises(TypeError): with pytest.raises(TypeError):
VTK.load(name,dataset_type) VTK.load(tmp_path/fname,dataset_type)
def test_file_not_found(self):
with pytest.raises(FileNotFoundError):
VTK.load('/dev/null')
def test_add_extension(self,tmp_path,default): def test_add_extension(self,tmp_path,default):
default.save(tmp_path/'default.txt',parallel=False) default.save(tmp_path/'default.txt',parallel=False)