allow to use '~' in path

not sure how to test, i.e. how to create a fixture to have a fake home
This commit is contained in:
Martin Diehl 2022-02-21 07:13:19 +01:00
parent fe2ab947ec
commit 9fe51686e5
3 changed files with 5 additions and 5 deletions

View File

@ -328,7 +328,7 @@ class Colormap(mpl.colors.ListedColormap):
if fname is None: if fname is None:
return open(self.name.replace(' ','_')+suffix, 'w', newline='\n') return open(self.name.replace(' ','_')+suffix, 'w', newline='\n')
elif isinstance(fname, (str, Path)): elif isinstance(fname, (str, Path)):
return open(fname, 'w', newline='\n') return open(Path(fname).expanduser(), 'w', newline='\n')
else: else:
return fname return fname

View File

@ -154,7 +154,7 @@ class Result:
'fields': self.fields, 'fields': self.fields,
} }
self.fname = Path(fname).absolute() self.fname = Path(fname).expanduser().absolute()
self._protected = True self._protected = True

View File

@ -208,7 +208,7 @@ class VTK:
raise FileNotFoundError(f'No such file: {fname}') raise FileNotFoundError(f'No such file: {fname}')
if (ext := Path(fname).suffix) == '.vtk' or dataset_type is not None: if (ext := Path(fname).suffix) == '.vtk' or dataset_type is not None:
reader = vtk.vtkGenericDataObjectReader() reader = vtk.vtkGenericDataObjectReader()
reader.SetFileName(str(fname)) reader.SetFileName(str(Path(fname).expanduser()))
if dataset_type is None: if dataset_type is None:
raise TypeError('Dataset type for *.vtk file not given') raise TypeError('Dataset type for *.vtk file not given')
elif dataset_type.lower().endswith(('imagedata','image_data')): elif dataset_type.lower().endswith(('imagedata','image_data')):
@ -237,7 +237,7 @@ class VTK:
else: else:
raise TypeError(f'Unknown file extension "{ext}"') raise TypeError(f'Unknown file extension "{ext}"')
reader.SetFileName(str(fname)) reader.SetFileName(str(Path(fname).expanduser()))
reader.Update() reader.Update()
vtk_data = reader.GetOutput() vtk_data = reader.GetOutput()
@ -277,7 +277,7 @@ class VTK:
default_ext = '.'+writer.GetDefaultFileExtension() default_ext = '.'+writer.GetDefaultFileExtension()
ext = Path(fname).suffix ext = Path(fname).suffix
writer.SetFileName(str(fname)+(default_ext if default_ext != ext else '')) writer.SetFileName(str(Path(fname).expanduser())+(default_ext if default_ext != ext else ''))
if compress: if compress:
writer.SetCompressorTypeToZLib() writer.SetCompressorTypeToZLib()